Decorators
Decorators are used to add some metadata to our model classes, relevant to our javascript-to-dynamo mapper.
Last updated
Decorators are used to add some metadata to our model classes, relevant to our javascript-to-dynamo mapper.
Last updated
To get started with defining a model add the @Model() decorator to your typescript class.
Use the @Model
decorator to make it 'mappable' for the dynamo-easy mapper.
You can optionally pass an object containing the table name if you don't want the default table name.
The default table name is built with ${kebabCase(modelName)}s
To use different table names on different stages the tableNameResolver is the right choice.
If you need to read the metadata by hand for some purpose, use the metadataForModel
function to read the informations.
PartitionKey
PartitionKeyUUID
which generates an id using the uuid package (peer dependency)
SortKey
We provide two decorators to work with global secondary indexes:
GSIPartitionKey
GSISortKey
You can use multiple GSI on the same model and also use the same property for different Indexes
name: string
define a different name (than the property name) for DynamoDB.
mapper: MapperForType
define a custom mapper (e.g if you want to use a complex object as PartitionKey or SortKey)
The CollectionProperty decorator is used for arrays and sets. It defines if the values should be mapped to [L]ist or [(N|S|B)S]et and stores the information how the Attributes should be parsed.
itemType: ModelConstructor
provide the class of the items inside the collection if they have decorators (this ItemClass also needs the @Model
decorator)
itemMapper: MapperForType
provide a custom mapper to map the complex items of your collection to [S]tring, [N]umber or [B]inary. This is mainly useful if you want to store them in a [S]et.
sorted: boolean
the collection will be stored as [L]ist ([S]et does not preserve the order) no matter if the javascript type is a Set
or an Array
.
name: string
define a different name (than the property name) for DynamoDB.
it does not make sense to provide both, the itemType and an itemMapper. An Error would be thrown.
further information how arrays and sets are mapped:
The DateProperty decorator is just syntactic sugar for @Property({mapper: dateMapper})
all properties decorated with it will be mapped with the default dateMapper or the one you define with updateDynamoEasyConfig({dateMapper: MapperForType})
.
dynamo-easy provides two date mappers:
- dateToStringMapper (default) which maps to ISO string{ S: date.toISOString() }
- dateToNumberMapper which maps to unix timestamp { N: `${date.toTime()}` }
name: string
define a different name (than the property name) for DynamoDB.
The @Transient
decorator can be used to ignore a property when the object is mapped.
We map all properties (Object.getOwnPropertyNames(objectToMap)
) by default. Use the @Transient decorator to prevent a property from being mapped.