Set
Defines a set attribute. Sets can contain numbers
, strings
, or binaries
:
import { set } from 'dynamodb-toolbox/attributes/set';
import { string } from 'dynamodb-toolbox/attributes/string';
const pokeTypeSchema = string().enum('fire', ...)
const pokemonSchema = schema({
...
pokeTypes: set(pokeTypeSchema),
});
type FormattedPokemon = FormattedItem<typeof PokemonEntity>;
// => {
// ...
// pokeTypes: Set<'fire' | ...>
// }
Set elements must respect some constraints:
- They cannot be
optional
or always required - They cannot be
hidden
orkey
(tagging theset
itself askey
is enough) - They cannot have
default
orlinks
// ❌ Raises a type AND a run-time error
const strSet = set(string().optional())
const strSet = set(string().hidden())
const strSet = set(string().key())
const strSet = set(string().default('foo'))
Options
.required()
string | undefined
Tags the attribute as required (at root level or within Maps). Possible values are:
'atLeastOnce' (default)
: Required (starting value)'always'
: Always required (including updates)'never'
: Optional
// Equivalent
const pokeTypesSchema = set(pokeTypeSchema)
const pokeTypesSchema = set(pokeTypeSchema).required()
const pokeTypesSchema = set(
pokeTypeSchema,
// Options can be provided as 2nd argument
{ required: 'atLeastOnce' }
)
// shorthand for `.required('never')`
const pokeTypesSchema = set(pokeTypeSchema).optional()
const pokeTypesSchema = set(..., { required: 'never' })
.hidden()
boolean | undefined
Skips the attribute when formatting items:
const pokeTypesSchema = set(pokeTypeSchema).hidden()
const pokeTypesSchema = set(..., { hidden: true })
.key()
boolean | undefined
Tags the attribute as needed to compute the primary key:
// Note: The method also sets the `required` property to 'always'
// (it is often the case in practice, you can still use `.optional()` if needed)
const pokeTypesSchema = set(pokeTypeSchema).key()
const pokeTypesSchema = set(..., {
key: true,
required: 'always'
})
.savedAs(...)
string
Renames the attribute during the transformation step (at root level or within Maps):
const pokeTypesSchema = set(pokeTypeSchema).savedAs('pt')
const pokeTypesSchema = set(..., { savedAs: 'pt' })
.default(...)
ValueOrGetter<Set<ELEMENTS>>
Specifies default values for the attribute. See Defaults and Links for more details:
- Put/Update
- Key
const now = () => new Date().toISOString()
const timestampsSchema = set(string())
.default(() => new Set([now()]))
.updateDefault(() => $add(now()))
// 👇 Similar to
const timestampsSchema = set(string())
.putDefault(() => new Set([now()]))
.updateDefault(() => $add(now()))
// 👇 ...or
const timestampsSchema = set({
defaults: {
key: undefined,
put: () => new Set([now()]),
update: () => $add(now())
}
})
const defaultSpecifiers = new Set(['POKEMON'])
const specifiersSchema = set(string())
.key()
.default(defaultSpecifiers)
// 👇 Similar to
const specifiersSchema = set(string())
.key()
.keyDefault(defaultSpecifiers)
// 👇 ...or
const specifiersSchema = set({
defaults: {
key: defaultSpecifiers,
// put & update defaults are not useful in `key` attributes
put: undefined,
update: undefined
},
key: true,
required: 'always'
})
.link<Schema>(...)
Link<SCHEMA, Set<ELEMENTS>>
Similar to .default(...)
but allows deriving the default value from other attributes. See Defaults and Links for more details:
const pokemonSchema = schema({
pokeTypeList: list(pokeTypeSchema)
}).and(prevSchema => ({
pokeTypeSet: set(pokeTypeSchema).link<typeof prevSchema>(
// 🙌 Correctly typed!
item => new Set(item.pokeTypeList)
)
}))
.validate(...)
Validator<Set<ELEMENTS>>
Adds custom validation to the attribute. See Custom Validation for more details:
const nonEmptySetSchema = set(string()).validate(
input => input.size > 0
)
// 👇 Similar to
const nonEmptySetSchema = set(string()).putValidate(
input => input.size > 0
)
// 👇 ...or
const nonEmptySetSchema = set(string(), {
validators: {
key: undefined,
put: input => input.size > 0,
update: undefined
}
})