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
import { Model } from '@shiftcoders/dynamo-easy'
@Model({ tableName: 'my-model-table-name' })
export class MyModel {
}
Key Decorators
Primary Key
PartitionKey
PartitionKeyUUID
which generates an id using the uuid package (peer dependency)SortKey
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
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
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.
Mapping Conceptfurther 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})
.
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.
import { Model, Transient } from '@shiftcoders/dynamo-easy'
@Model()
class MyModel {
@Transient()
myPropertyToIgnore: any
}
Last updated
Was this helpful?