Decorators

Decorators are used to add some metadata to our model classes, relevant to our javascript-to-dynamo mapper.

To get started with defining a model add the @Model()arrow-up-right decorator to your typescript class.

technical api docs

Model Decorators

@Model

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

circle-info

To use different table names on different stages the tableNameResolver is the right choice.

circle-info

If you need to read the metadata by hand for some purpose, use the metadataForModel function to read the informations.

Key Decorators

Primary Key

Global Secondary Index

We provide two decorators to work with global secondary indexes:

  • GSIPartitionKey

  • GSISortKey

circle-info

You can use multiple GSI on the same model and also use the same property for different Indexes

Local Secondary Index

Type Decorators

@Property(options)

options

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)

@CollectionProperty(options)

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.

options

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.

circle-info

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:

Mapping Conceptchevron-right

@DateProperty(options)

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}).

circle-info

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()}` }

options

name: string define a different name (than the property name) for DynamoDB.

Other

@Transient

The @Transient decorator can be used to ignore a property when the object is mapped.

circle-info

We map all properties (Object.getOwnPropertyNames(objectToMap)) by default. Use the @Transientarrow-up-right decorator to prevent a property from being mapped.

Last updated

Was this helpful?