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() decorator to your typescript class.

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

my-model.snippet.ts
import { Model } from '@shiftcoders/dynamo-easy'

@Model({ tableName: 'my-model-table-name' })
export class MyModel {

}

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.

Key Decorators

Primary Key

  • PartitionKey

  • PartitionKeyUUIDwhich generates an id using the uuid package (peer dependency)

  • SortKey

my-model-with-sort-key.snippet.ts
import { Model, PartitionKey, SortKey } from '@shiftcoders/dynamo-easy'

@Model()
export class MyModel {
  @PartitionKey()
  myPartitionKey: string

  @SortKey()
  mySortKey: number
}

Global Secondary Index

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

my-model-with-gsi.snippet.ts
import { Model, GSIPartitionKey, GSISortKey } from '@shiftcoders/dynamo-easy'

const MY_MODEL_GSI = 'NameOfGSI' 

@Model()
class MyModel {
  @GSIPartitionKey(MY_MODEL_GSI)
  myGsiPartitionKey: string

  @GSISortKey(MY_MODEL_GSI)
  myGsiSortKey: number
}

Local Secondary Index

my-model-with-lsi.snippet.ts
import { Model, LSISortKey, PartitionKey, SortKey } from '@shiftcoders/dynamo-easy'

@Model()
class MyModel {
  @PartitionKey()
  myPartitionKey: string

  @SortKey()
  mySortKey: number

  @LSISortKey('NameOfLSI')
  myLsiSortKey: number
}

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.

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:

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

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.

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

my-model-with-transient.snippet.ts
import { Model, Transient } from '@shiftcoders/dynamo-easy'

@Model()
class MyModel {
    @Transient()
    myPropertyToIgnore: any
}

Last updated