Type Inference
DynamoDB-Toolbox exposes several generic types to infer custom types from your entities.
Which one you should use depends on your usage context, for instance, whether itβs within a write or a read operation.
Writesβ
For write operations, DynamoDB-Toolbox exposes the following generic types:
ValidItem
: A valid entity itemInputItem
: Similar toValidItem
, but with defaulted and linked attributes optionalTransformedItem
: A valid entity item after transformation
import type {
InputItem,
ValidItem,
TransformedItem
} from 'dynamodb-toolbox/entity'
type Input = InputItem<typeof PokemonEntity>
type Valid = ValidItem<typeof PokemonEntity>
type Transformed = TransformedItem<typeof PokemonEntity>
By default, those generics use the put
write mode, but you can switch to the key
or update
modes with the mode
option. This impacts which the presence and requiredness of attributes:
type ValidKey = ValidItem<
typeof PokemonEntity,
{ mode: 'key' }
>
type ValidUpdate = ValidItem<
typeof PokemonEntity,
{ mode: 'update' }
>
Here are step-by-step examples:
βοΈ Entity
const PokemonEntity = new Entity({
table,
schema: schema({
// key attributes
pokemonClass: string()
.key()
.transform(prefix('POKEMON'))
.savedAs('partitionKey'),
pokemonId: string().key().savedAs('sortKey'),
// other attributes
name: string().optional(),
level: number().default(1)
}).and(prevSchema => ({
levelPlusOne: number().link<typeof prevSchema>(
({ level }) => level + 1
)
}))
// timestamps
timestamps: true
...
})
π 'put'
mode
- InputItem
- ValidItem
- TransformedItem
{
"pokemonClass": "pikachu",
"pokemonId": "123",
"name": "Pikachu"
}
{
"pokemonClass": "pikachu",
"pokemonId": "123",
+ "created": "2022-01-01T00:00:00.000Z",
+ "modified": "2022-01-01T00:00:00.000Z",
"name": "Pikachu",
+ "level": 1,
+ "levelPlusOne": 2,
}
{
- "pokemonClass": "pikachu",
+ "partitionKey": "POKEMON#pikachu",
- "pokemonId": "123",
+ "sortKey": "123",
"created": "2022-01-01T00:00:00.000Z",
"modified": "2022-01-01T00:00:00.000Z",
"name": "Pikachu",
"level": 1,
"levelPlusOne": 2,
}
π 'key'
mode
- InputItem
- ValidItem
- TransformedItem
{
"pokemonClass": "pikachu",
"pokemonId": "123",
}
+ (Only key attributes are required)
{
"pokemonClass": "pikachu",
"pokemonId": "123",
}
{
- "pokemonClass": "pikachu",
+ "partitionKey": "POKEMON#pikachu",
- "pokemonId": "123",
+ "sortKey": "123",
}
π 'update'
mode
- InputItem
- ValidItem
- TransformedItem
{
"pokemonClass": "bulbasaur",
"pokemonId": "123",
"name": "PlantyDino",
}
{
"pokemonClass": "bulbasaur",
"pokemonId": "123",
+ "modified": "2022-01-01T00:00:00.000Z",
"name": "PlantyDino",
}
{
- "pokemonClass": "bulbasaur",
+ "partitionKey": "POKEMON#bulbasaur",
- "pokemonId": "123",
+ "sortKey": "123",
"modified": "2022-01-01T00:00:00.000Z",
"name": "PlantyDino",
}
For convenience, DynamoDB-Toolbox also exposes the following generic types:
KeyInputItem
: Similar toInputItem
in thekey
mode.SavedItem
: Similar toTransformedItem
but adds thePrimaryKey
of the Entity's Table
import {
KeyInputItem,
SavedItem
} from 'dynamodb-toolbox/entity'
type KeyInput = KeyInputItem<typeof PokemonEntity>
type Saved = SavedItem<typeof PokemonEntity>
Readsβ
For read operations, DynamoDB-Toolbox exposes the following generic types:
ReadItem
: A valid entity item (differs fromValidItem
as options are different, see below)FormattedItem
: Similar toReadItem
, but withhidden
attributes omitted
import type {
ReadItem,
FormattedItem
} from 'dynamodb-toolbox/schema'
type Read = ReadItem<typeof PokemonEntity>
type Formatted = FormattedItem<typeof PokemonEntity>
By default, those generics return complete items, but you can filter attributes and/or apply Partial
(deeply) with the attributes
and partial
options:
type Filtered = FormattedItem<
typeof PokemonEntity,
{ attributes: 'level' | 'name' | 'deep.attr[0].path' }
>
type Partial = FormattedItem<
typeof PokemonEntity,
{ partial: true }
>