Skip to main content

Decorators

Decorators are crucial for modelling entities. Dynamode uses them to bind model attributes with DynamoDB tables.

info

Dynamode uses experimental decorators, it is a language feature which hasn’t yet been fully ratified into the JavaScript specification - source.

Add following lines in tsconfig.json in order to use decorators with Typescript (even if you are using Typescript 5.0+):

{
"compilerOptions": {
...
"experimentalDecorators": true,
...
}
}

To see examples of decorators in use - check out modeling page.

entity.customName(entityName)

Description

This decorator is used to change the name of the entity. By default, the name of the entity is the name of the class. This is especially useful when you use minification or obfuscation tools.

Arguments

entityName: string - String that will be used as the new name for the entity.

Examples

import {entity} from 'dynamode/decorators';

@entity.customName("CustomName")
class YourModel extends Entity {
...
}

console.log(YourModel.constructor.name); // CustomName

attribute.partitionKey.string(options?)

Description

This decorator is used to tag the partition key attribute of type string.

info

attribute.partitionKey.string(options?) or attribute.partitionKey.number() is required in every entity.

Arguments

You can add optional options parameter that is an object. The table below represents options that you can pass in:

NameDescriptionTypeDefault
prefixShortcut for prefix(value) decoratorstringundefined
suffixShortcut for suffix(value) decoratorstringundefined

Examples

class YourModel extends Entity { 
...
@attribute.partitionKey.string()
PK: string;
...
}

attribute.partitionKey.number()

Description

This decorator is used to tag the partition key attribute of type number.

info

attribute.partitionKey.string(options?) or attribute.partitionKey.number() is required in every entity.

Examples

class YourModel extends Entity { 
...
@attribute.partitionKey.number()
PK: number;
...
}

attribute.sortKey.string(options?)

Description

This decorator is used to tag the sort key attribute of type string.

Arguments

You can add optional options parameter that is an object. The table below represents options that you can pass in:

NameDescriptionTypeDefault
prefixShortcut for prefix(value) decoratorstringundefined
suffixShortcut for suffix(value) decoratorstringundefined

Examples

class YourModel extends Entity { 
...
@attribute.sortKey.string()
SK: string;
...
}

attribute.sortKey.number()

Description

This decorator is used to tag the sort key attribute of type number.

Examples

class YourModel extends Entity { 
...
@attribute.sortKey.number()
SK: number;
...
}

attribute.gsi.partitionKey.string(options)

Description

This decorator is used to tag a global secondary index partition key attribute of type string.

Arguments

options - Options that configure the index. The table below represents options that you can pass in:

NameDescriptionTypeDefault
indexNameName of the index for the partition key.stringN/A
prefixShortcut for prefix(value) decoratorstringundefined
suffixShortcut for suffix(value) decoratorstringundefined

Examples

class YourModel extends Entity { 
...
@attribute.gsi.partitionKey.string({ indexName: 'GSI1' })
GSI_PK: string;
...
}

attribute.gsi.partitionKey.number(options)

Description

This decorator is used to tag a global secondary index partition key attribute of type number.

Arguments

options - Options that configure the index. The table below represents options that you can pass in:

NameDescriptionTypeDefault
indexNameName of the index for the partition key.stringN/A

Examples

class YourModel extends Entity { 
...
@attribute.gsi.partitionKey.number({ indexName: 'GSI1' })
GSI_PK: number;
...
}

attribute.gsi.sortKey.string(options)

Description

This decorator is used to tag a global secondary index sort key attribute of type string.

Arguments

options - Options that configure the index. The table below represents options that you can pass in:

NameDescriptionTypeDefault
indexNameName of the index for the sort key.stringN/A
prefixShortcut for prefix(value) decoratorstringundefined
suffixShortcut for suffix(value) decoratorstringundefined

Examples

class YourModel extends Entity { 
...
@attribute.gsi.sortKey.string({ indexName: 'GSI1' })
GSI_SK: string;
...
}

attribute.gsi.sortKey.number(options)

Description

This decorator is used to tag a global secondary index sort key attribute of type number.

Arguments

options - Options that configure the index. The table below represents options that you can pass in:

NameDescriptionTypeDefault
indexNameName of the index for the sort key.stringN/A

Examples

class YourModel extends Entity { 
...
@attribute.gsi.sortKey.number({ indexName: 'GSI1' })
GSI_SK: number;
...
}

attribute.lsi.sortKey.string(options)

Description

This decorator is used to tag a local secondary index sort key attribute of type string.

Arguments

options - Options that configure the index. The table below represents options that you can pass in:

NameDescriptionTypeDefault
indexNameName of the index for the sort key.stringN/A
prefixShortcut for prefix(value) decoratorstringundefined
suffixShortcut for suffix(value) decoratorstringundefined

Examples

class YourModel extends Entity { 
...
@attribute.gsi.sortKey.string({ indexName: 'GSI1' })
GSI_SK: string;
...
}

attribute.lsi.sortKey.number(options)

Description

This decorator is used to tag a local secondary index sort key attribute of type number.

Arguments

options - Options that configure the index. The table below represents options that you can pass in:

NameDescriptionTypeDefault
indexNameName of the index for the sort key.stringN/A

Examples

class YourModel extends Entity { 
...
@attribute.gsi.sortKey.number({ indexName: 'GSI1' })
GSI_SK: number;
...
}

attribute.string(options?)

Description

This decorator is used to tag an attribute of type string.

Arguments

You can add optional options parameter that is an object. The table below represents options that you can pass in:

NameDescriptionTypeDefault
prefixShortcut for prefix(value) decoratorstringundefined
suffixShortcut for suffix(value) decoratorstringundefined

Examples

class YourModel extends Entity { 
...
@attribute.string()
key: string;
...
}

attribute.number()

Description

This decorator is used to tag an attribute of type number.

Examples

class YourModel extends Entity { 
...
@attribute.number()
key: number;
...
}

attribute.boolean()

Description

This decorator is used to tag an attribute of type boolean.

Examples

class YourModel extends Entity { 
...
@attribute.boolean()
key: boolean;
...
}

attribute.binary()

Description

This decorator is used to tag an attribute of type UInt8Array (binary data).

Examples

class YourModel extends Entity { 
...
@attribute.binary()
key: UInt8Array;
...
}

attribute.object()

Description

This decorator is used to tag an attribute of type Record<string, unknown> / { [key: string]: unknown } / { [key]: unknown, ... }.

Examples

class YourModel extends Entity { 
...
@attribute.object()
key: Record<string, number>;

@attribute.object()
key2: { [key: string]: unknown };
...
}

attribute.array()

Description

This decorator is used to tag an attribute of type Array<T>.

Examples

class YourModel extends Entity { 
...
@attribute.array()
key: Array<unknown>;
...
}

attribute.set()

Description

This decorator is used to tag an attribute of type Set<string | number>.

Examples

class YourModel extends Entity { 
...
@attribute.set()
key: Set<string>;
...
}

attribute.map()

Description

This decorator is used to tag an attribute of type Map<string, unknown>.

Examples

class YourModel extends Entity { 
...
@attribute.map()
key: Map<string, unknown>;
...
}

attribute.date.string(options?)

Description

This decorator is used to tag an attribute that holds a Date, the attribute is saved in DynamoDB in ISO 8601 format.

info

attribute.date.string(options?) has to decorate attribute of Date type.

Arguments

You can pass optional options parameter that is an object. The table below represents options that you can pass in:

NameDescriptionTypeDefault
prefixShortcut for prefix(value) decoratorstringundefined
suffixShortcut for suffix(value) decoratorstringundefined

Examples

class YourModel extends Entity { 
...
@attribute.date.string()
CREATED_AT: Date; // saved as ISO 8601: '2022-10-18T20:36:20.511Z'
...
}

attribute.date.number()

Description

This decorator is used to tag an attribute that holds a Date, the attribute is saved in DynamoDB in Unix timestamp format.

info

attribute.date.number(options?) has to decorate attribute of Date type.

Examples

class YourModel extends Entity { 
...
@attribute.date.number()
updatedAt: Date; // saved as Unix timestamp: 1688307056
...
}

attribute.prefix(value)

Description

This decorator is used to add a static prefix to the value (only for attributes of string type). You can customize a separator that is added between prefix and actual value with Dynamode class.

info

Prefix is added whenever you interact with a attribute.

  • All EntityManager static methods (e.g. EntityManager.get(), EntityManager.update())
  • Condition, Query and Scan builders
  • transactionGet and transactionWrite

Arguments

value: string - String that will be prepended to the value

Examples

class YourModel extends Entity { 
...
@attribute.prefix("attr_prefix")
@attribute.string()
attr: string; // for 'attr_value' -> 'attr_prefix#attr_value'
...
}

attribute.suffix(value)

Description

This decorator is used to add a static suffix to the value (only for attributes of string type). You can customize a separator that is added between suffix and actual value with Dynamode class.

info

Prefix is added whenever you interact with a attribute.

  • All EntityManager static methods (e.g. EntityManager.get(), EntityManager.update())
  • Condition, Query and Scan builders
  • transactionGet and transactionWrite

Arguments

value: string - String that will be prepended to the value

Examples

class YourModel extends Entity { 
...
@attribute.suffix("attr_suffix")
@attribute.string()
attr: string; // for 'attr_value' -> 'attr_value#attr_suffix'
...
}