Skip to main content
Version: v2

Item

Describes items with a finite list of attributes, i.e. key-schema pairs. Items differ from maps as they don't have any property and are not meant to be nested within other schemas:

import { item } from 'dynamodb-toolbox/schema/item'

const fullNameSchema = item({
firstName: string(),
lastName: string()
})

type FullName = FormattedValue<typeof fullNameSchema>
// => {
// firstName: string
// lastName: string
// }

Methods

Item schemas can be used to build new schemas with the following methods:

and(...)

(attr: NEW_ATTR | (MapSchema<OLD_ATTR> => NEW_ATTR)) => MapSchema<OLD_ATTR & NEW_ATTR>

Produces a new item schema by extending the original schema with new attributes:

const extendedSchema = baseSchema.and({
newAttribute: string(),
...
})
info

In case of naming conflicts, new attributes override the previous ones.

The method also accepts functions that return new attributes. In this case, the previous schema is provided as an argument (which is particularly useful for building Links):

const extendedSchema = mySchema.and(prevSchema => ({
newAttribute: string(),
...
}))

pick(...)

(...attrNames: ATTR_NAMES[]) => MapSchema<Pick<ATTR, ATTR_NAMES>>

Produces a new item schema by picking only certain attributes from the original schema:

const picked = pokemonSchema.pick('name', 'pokemonLevel')

Due to the potential disruptive nature of this method on links, they are reset in the process:

const nameSchema = item({
firstName: string(),
lastName: string(),
completeName: string().link(({ firstName, lastName }) =>
[firstName, lastName].join(' ')
)
})

const picked = nameSchema.pick('lastName', 'completeName')

picked.attributes.completeName.props.putLink
// => undefined

omit(...)

(...attrNames: ATTR_NAMES[]) => MapSchema<Omit<ATTR, ATTR_NAMES>>

Produces a new item schema by omitting certain attributes out of the original schema:

const omitted = pokemonSchema.omit('name', 'pokemonLevel')

Due to the potential disruptive nature of this method on links, they are reset in the process:

const nameSchema = item({
firstName: string(),
lastName: string(),
completeName: string().link(({ firstName, lastName }) =>
[firstName, lastName].join(' ')
)
})

const omitted = nameSchema.omit('firstName')

omitted.attributes.completeName.props.putLink
// => undefined