Skip to main content

Entity Manager

Entity class represents a model for your application.

Instance of a class represents an item for a given model in DynamoDB.

Entity Manager class is used to perform operations on a given model.

  • Exposes static methods that you can use to read, insert, update, delete items in DynamoDB. This page describes in detail all available methods.
  • To create an instance of Entity Manager use TableManager.entityManager(entity?) method.

new Entity(props)

Description

Entity constructor is used to initialize an item for a given model. When you construct an item using new Entity(props) the item is only created locally, meaning that it is not yet saved in DynamoDB. If you want to save it to the table use for example EntityManager.put(item, options?) method.

Arguments

props - Props are defined based on your needs when you define the model.

Examples

User model is defined here. to initialize it, simply use a constructor.

const user = new User({
partitionKey: '1',
sortKey: 'blazej',
username: 'blazej',
email: 'blazej@gmail.com',
age: 18,
friends: ['tomas', 'david'],
config: {
isAdmin: true,
},
}); // Item is not saved in DynamoDB at this point

await UserManager.put(user); // Item is saved in DynamoDB

EntityManager.condition()

You can construct a conditional by using the EntityManager.condition() method. This initializes Condition class that acts as a condition builder to construct your conditional that you can pass to Entity Manager methods, Query and Scan classes. To learn more check out Condition page.

EntityManager.query()

You can query items by using the EntityManager.query() method. This initializes Query class that acts as a builder to construct your DynamoDB query with the appropriate filters. To learn more check out Query page.

EntityManager.scan()

You can scan items by using the EntityManager.scan() method. This initializes Scan class that acts as a builder to construct your DynamoDB scan with the appropriate filters. To learn more check out Scan page.

EntityManager.get(primaryKey, options?)

Description

This method is used to retrieve an item from DynamoDB. It uses the GetItem DynamoDB operation.

Arguments

primaryKey: { [partitionKey]: string | number; [sortKey]?: string | number } - Primary key of the item you want to retrieve. [partitionKey] and [sortKey] are inferred from the new TableManager(tableEntity, tableMetadata) function.

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

NameDescriptionTypeDefault
returnWhat the method should return. For 'default' method returns initialized class with retrieved data. For 'input' method returns prepared DynamoDB input command and no request is made to DynamoDB (method no longer returns a promise). For 'output' method returns the bare output from DynamoDB operation.'default' | 'input' | 'output''default'
attributesWhat item attributes should be retrieved and returned. This uses DynamoDB's ProjectionExpression. If this value is undefined, then all attributes will be returned.string[]undefined
consistentWhether to perform a strongly consistent read or not.booleanfalse
extraInputExtra input that is passed to GetItem DynamoDB operation. Use it only in case that you know what are you are doing as it may override other properties passed to DynamoDB operation.GetItemCommandInputundefined

Examples

const user = await UserManager.get({ partitionKey: '1', sortKey: 'blazej' });
const user = await UserManager.get({ partitionKey: '1', sortKey: 'blazej' }, { return: 'default' }); // the same

Output:

User {
dynamodeEntity: 'User',
partitionKey: '1',
sortKey: 'blazej',
username: 'blazej',
email: 'blazej@gmail.com',
age: 18,
friends: [ 'tomas', 'david' ],
config: { isAdmin: true }
}

EntityManager.put(item, options?)

Description

This method is used to insert an item to DynamoDB. It uses the PutItem DynamoDB operation.

Arguments

item: Entity - Instance of a model that you want to insert.

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

NameDescriptionTypeDefault
returnWhat the method should return. For 'default' method returns initialized class. For 'input' method returns prepared DynamoDB input command and no request is made to DynamoDB (method no longer returns a promise). For 'output' method returns the bare output from DynamoDB operation.'default' | 'input' | 'output''default'
overwritePutItem DynamoDB operation overwrites items on insertion by default. If you want to prevent overwrites use EntityManager.create(item, options?) instead, or set overwrite prop to false.booleantrue
conditionThis is an optional instance of a Condition for the save. Item won't be inserted in case condition fails.Conditionundefined
extraInputExtra input that is passed to PutItem DynamoDB operation. Use it only in case that you know what are you are doing as it may override other properties passed to DynamoDB operation.PutItemCommandInputundefined

Examples

const userInstance = new User({
partitionKey: '1',
sortKey: 'blazej',
username: 'blazej',
email: 'blazej@gmail.com',
age: 18,
friends: ['tomas', 'david'],
config: {
isAdmin: true,
},
});

const user = await UserManager.put(userInstance);
// OR
const user = await UserManager.put(userInstance, { return: 'default' });

Output:

User {
dynamodeEntity: 'User',
partitionKey: '1',
sortKey: 'blazej',
username: 'blazej',
email: 'blazej@gmail.com',
age: 18,
friends: [ 'tomas', 'david' ],
config: { isAdmin: true }
}

EntityManager.create(item, options?)

Description

This method is almost identical to EntityManager.put(item, options?). The only difference being that EntityManager.create(item, options?) won't overwrite existing item in DynamoDB (overwrite is set to false as default).

EntityManager.update(primaryKey, props, options?)

Description

This method is used to update an item in DynamoDB. It uses the UpdateItem DynamoDB operation.

Arguments

primaryKey: { [partitionKey]: string | number; [sortKey]?: string | number } - Primary key of the item you want to update. [partitionKey] and [sortKey] are inferred from the metadata(tableMetadata) function.

props: UpdateProps - An object with DynamoDB update operations. For every operation you can pass properties that you want to update.

NameAcceptable property typesDescription
addnumber | Set<number> | Set<string>Adds a value to sets or increments/decrements number property.
setAllSets a value on property name.
setIfNotExistsAllSets a value on property name (unless it exists on the item).
listAppendArray<any>Appends a value to an array.
incrementnumberIncrements property with passed value.
decrementnumberDecrements property with passed value.
deleteSet<number> | Set<string>Removes value from a set.
removeAllRemoves property from the item.

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

NameDescriptionTypeDefault
returnWhat the method should return. For 'default' method returns initialized class with retrieved data. For 'input' method returns prepared DynamoDB input command and no request is made to DynamoDB (method no longer returns a promise). For 'output' method returns the bare output from DynamoDB operation.'default' | 'input' | 'output''default'
returnValuesIn which state to return the item after update. You can get the item attributes as they appear before or after update. This uses DynamoDB's ReturnValues.'none' | 'allOld' | 'allNew' | 'updatedOld' | 'updatedNew''allNew'
conditionThis is an optional instance of a Condition for the update. Item won't be updated in case condition fails.Conditionundefined
extraInputExtra input that is passed to UpdateItem DynamoDB operation. Use it only in case that you know what are you are doing as it may override other properties passed to DynamoDB operation.UpdateItemCommandInputundefined

Examples

const item = await AllPossiblePropertiesManager.update(
{ partitionKey: 'pk1', sortKey: 'sk1' },
{
add: {
set: new Set(['5']),
},
set: {
string: 'string',
},
setIfNotExists: {
'object.optional': 'optional',
},
},
);
// OR
const item = await AllPossiblePropertiesManager.update(
{ partitionKey: 'pk1', sortKey: 'sk1' },
{
add: {
set: new Set(['5']),
},
set: {
string: 'string',
},
setIfNotExists: {
'object.optional': 'optional',
},
},
{ return: 'default' }
);

Output:

AllPossibleProperties {
dynamodeEntity: 'AllPossibleProperties',
partitionKey: 'pk1',
sortKey: 'sk1',
GSI_1_PK: undefined,
GSI_1_SK: undefined,
LSI_1_SK: undefined,
createdAt: 2022-10-16T07:08:21.168Z,
updatedAt: 2022-10-16T07:08:21.168Z,
string: 'string',
object: { optional: 'test', required: 2 },
array: [ '1' ],
map: Map(1) { '1' => 'test' },
set: Set(4) { '1', '2', '3', '5' },
number: 10,
boolean: true,
unsaved: 'unsaved'
}

EntityManager.delete(primaryKey, options?)

Description

This method is used to delete an item from DynamoDB. It uses the DeleteItem DynamoDB operation.

Arguments

primaryKey: { [partitionKey]: string | number; [sortKey]?: string | number } - Primary key of the item you want to delete. [partitionKey] and [sortKey] are inferred from the new TableManager(tableEntity, tableMetadata) function.

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

NameDescriptionTypeDefault
returnWhat the method should return. For 'default' method returns nothing. For 'input' method returns prepared DynamoDB input command and no request is made to DynamoDB (method no longer returns a promise). For 'output' method returns the bare output from DynamoDB operation.'default' | 'input' | 'output''default'
conditionInstance of Condition class with conditionals that has to pass to delete the item.Conditionundefined
throwErrorIfNotExistsDeleteItem DynamoDB operation does not throw an error when deleting an item that doesn't exist. If you want to prevent deleting non existent item set throwErrorIfNotExists prop to true.booleanfalse
returnValuesWhether or not to return the item after deletion. You can get the item attributes as they appear before deletion or none at all. This uses DynamoDB's ReturnValues.'none' | 'allOld''allOld'
extraInputExtra input that is passed to DeleteItem DynamoDB operation. Use it only in case that you know what are you are doing as it may override other properties passed to DynamoDB operation.DeleteItemCommandInputundefined

Examples

const deletedUser = await UserManager.delete({ partitionKey: '1', sortKey: 'blazej' });
// OR
const deletedUser = await UserManager.delete({ partitionKey: '1', sortKey: 'blazej' }, { return: 'default' });

Output:

User {
dynamodeEntity: 'User',
partitionKey: '1',
sortKey: 'blazej',
username: 'blazej',
email: 'blazej@gmail.com',
age: 18,
friends: [ 'tomas', 'david' ],
config: { isAdmin: true }
}

EntityManager.batchGet(primaryKeys, options?)

Description

This method is used to retrieve multiple items from DynamoDB. It uses the BatchGetItem DynamoDB operation.

Arguments

primaryKeys: Array<{ [partitionKey]: string | number; [sortKey]?: string | number }> - Primary keys of the items you want to retrieve. [partitionKey] and [sortKey] are inferred from the new TableManager(tableEntity, tableMetadata) function.

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

NameDescriptionTypeDefault
returnWhat the method should return. For 'default' method returns array of initialized classes with retrieved data. For 'input' method returns prepared DynamoDB input command and no request is made to DynamoDB (method no longer returns a promise). For 'output' method returns the bare output from DynamoDB operation.'default' | 'input' | 'output''default'
attributesWhat items attributes should be retrieved and returned. This uses DynamoDB's ProjectionExpression. If this value is undefined, then all attributes will be returned.string[]undefined
consistentWhether to perform a strongly consistent reads or not.booleanfalse
extraInputExtra input that is passed to BatchGetItem DynamoDB operation. Use it only in case that you know what are you are doing as it may override other properties passed to DynamoDB operation.BatchGetItemCommandInputundefined

Examples

const input = await UserManager.batchGet([
{ partitionKey: '1', sortKey: 'blazej' },
{ partitionKey: '2', sortKey: 'blazej' },
]);
// OR
const input = await UserManager.batchGet(
[
{ partitionKey: '1', sortKey: 'blazej' },
{ partitionKey: '2', sortKey: 'blazej' },
],
{
return: 'default',
},
);

Output:

{
items: [
User {
dynamodeEntity: 'User',
partitionKey: '2',
sortKey: 'blazej',
username: 'blazej',
email: 'blazej@gmail.com',
age: 18,
friends: [Array],
config: [Object]
},
User {
dynamodeEntity: 'User',
partitionKey: '1',
sortKey: 'blazej',
username: 'blazej',
email: 'blazej@gmail.com',
age: 18,
friends: [Array],
config: [Object]
}
],
unprocessedKeys: []
}

EntityManager.batchPut(items, options?)

Description

This method is used to insert multiple items to DynamoDB. It uses the BatchWriteItem DynamoDB operation.

This method is overwriting, so it will overwrite the data you currently have in place for the existing key in your table.

Arguments

items: Array<Entity> - Instances of entity that you want to insert.

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

NameDescriptionTypeDefault
returnWhat the method should return. For 'default' method returns initialized classes. For 'input' method returns prepared DynamoDB input command and no request is made to DynamoDB (method no longer returns a promise). For 'output' method returns the bare output from DynamoDB operation.'default' | 'input' | 'output''default'
extraInputExtra input that is passed to BatchWriteItem DynamoDB operation. Use it only in case that you know what are you are doing as it may override other properties passed to DynamoDB operation.BatchWriteItemInputundefined

Examples

const users = [
new User({
partitionKey: '1',
sortKey: 'blazej',
username: 'blazej',
email: 'blazej@gmail.com',
age: 18,
friends: ['tomas', 'david'],
config: {
isAdmin: true,
},
}),
new User({
partitionKey: '2',
sortKey: 'blazej',
username: 'blazej',
email: 'blazej@gmail.com',
age: 18,
friends: ['tomas', 'david'],
config: {
isAdmin: true,
},
}),
];

const result = await UserManager.batchPut(users);
// OR
const result = await UserManager.batchPut(users, {
return: 'default',
});

Output:

{
items: [
User {
dynamodeEntity: 'User',
partitionKey: '1',
sortKey: 'blazej',
username: 'blazej',
email: 'blazej@gmail.com',
age: 18,
friends: [Array],
config: [Object]
},
User {
dynamodeEntity: 'User',
partitionKey: '2',
sortKey: 'blazej',
username: 'blazej',
email: 'blazej@gmail.com',
age: 18,
friends: [Array],
config: [Object]
}
],
unprocessedItems: []
}

EntityManager.batchDelete(primaryKeys, options?)

Description

This method is used to delete multiple items from DynamoDB. It uses the BatchWriteItem DynamoDB operation.

Arguments

primaryKeys: Array<{ [partitionKey]: string | number; [sortKey]?: string | number }> - Primary keys of the items you want to delete. [partitionKey] and [sortKey] are inferred from the new TableManager(tableEntity, tableMetadata) function.

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

NameDescriptionTypeDefault
returnWhat the method should return. For 'default' method returns an object with unprocessedKeys. For 'input' method returns prepared DynamoDB input command and no request is made to DynamoDB (method no longer returns a promise). For 'output' method returns the bare output from DynamoDB operation.'default' | 'input' | 'output''default'
extraInputExtra input that is passed to BatchWriteItem DynamoDB operation. Use it only in case that you know what are you are doing as it may override other properties passed to DynamoDB operation.BatchWriteItemInputundefined

Examples

const result = await UserManager.batchDelete(
[
{ partitionKey: '1', sortKey: 'blazej' },
{ partitionKey: '2', sortKey: 'blazej' },
],
);
// OR
const result = await UserManager.batchDelete(
[
{ partitionKey: '1', sortKey: 'blazej' },
{ partitionKey: '2', sortKey: 'blazej' },
],
{ return: 'default' },
);

Output:

{ unprocessedItems: [] }

EntityManager.transactionGet(primaryKey, options?)

Description

This method is used to build a retrieve item operation. EntityManager.transactionGet(primaryKey, options?) used separately won't make a DynamoDB API call. You need to pass it to transactionGet(transactions, options?) function. This allows to retrieve multiple items in a single transaction.

Arguments

primaryKey: { [partitionKey]: string | number; [sortKey]?: string | number } - Primary key of the item you want to retrieve. [partitionKey] and [sortKey] are inferred from the new TableManager(tableEntity, tableMetadata) function.

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

NameDescriptionTypeDefault
attributesWhat item attributes should be retrieved and returned. This uses DynamoDB's ProjectionExpression. If this value is undefined, then all attributes will be returned.string[]undefined
extraInputExtra input that is passed to Get DynamoDB operation. Use it only in case that you know what are you are doing as it may override other properties passed to DynamoDB operation.Getundefined

Examples

Check out transactionGet(transactions, options?) function to see example usage.

EntityManager.transaction.put(item, options?)

Description

This method is used to build an insert item operation. EntityManager.transaction.put(item, options?) used separately won't make a DynamoDB API call. You need to pass it to transactionWrite(transactions, options?) function. This allows to insert multiple items in a single transaction

Arguments

item: Entity - Instance of a model that you want to insert.

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

NameDescriptionTypeDefault
overwriteTransactItems.Put DynamoDB operation overwrites items on insertion by default. If you want to prevent overwrites use EntityManager.transaction.create(item, options?) instead, or set overwrite prop to false.booleantrue
conditionThis is an optional instance of a Condition for the save. Item won't be inserted in case condition fails.Conditionundefined
returnValuesOnFailureWhether or not to return the item if the Put condition fails. You can get the item attributes as they appear before insertion or none at all. This uses DynamoDB's ReturnValuesOnConditionCheckFailure.'none' | 'allOld''allOld'
extraInputExtra input that is passed to Put DynamoDB operation. Use it only in case that you know what are you are doing as it may override other properties passed to DynamoDB operation.TransactItems.Putundefined

Examples

Check out transactionWrite(transactions, options?) function to see example usage.

EntityManager.transaction.create(item, options?)

Description

This method is almost identical to EntityManager.transaction.put(item, options?). The only difference being that EntityManager.transaction.create(item, options?) won't overwrite existing item in DynamoDB (overwrite is set to false as default).

EntityManager.transaction.update(primaryKey, props, options?)

Description

This method is used to build an update item operation. EntityManager.transaction.update(primaryKey, props, options?) used separately won't make a DynamoDB API call. You need to pass it to transactionWrite(transactions, options?) function. This allows to update multiple items in a single transaction.

Arguments

primaryKey: { [partitionKey]: string | number; [sortKey]?: string | number } - Primary key of the item you want to update. [partitionKey] and [sortKey] are inferred from the new TableManager(tableEntity, tableMetadata) function.

props: UpdateProps - An object with DynamoDB update operations. For every operation you can pass properties that you want to update.

NameAcceptable property typesDescription
addnumber | Set<number> | Set<string>Adds a value to sets or increments/decrements number property.
setAllSets a value on property name.
setIfNotExistsAllSets a value on property name (unless it exists on the item).
listAppendArray<any>Appends a value to an array.
incrementnumberIncrements property with passed value.
decrementnumberDecrements property with passed value.
deleteSet<number> | Set<string>Removes value from a set.
removeAllRemoves property from the item.

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

NameDescriptionTypeDefault
conditionThis is an optional instance of a Condition for the update. Item won't be updated in case condition fails.Conditionundefined
returnValuesOnFailureIn which state to return the item if the Update condition fails. You can get the item attributes as they appear before or after update. This uses DynamoDB's ReturnValuesOnConditionCheckFailure.'none' | 'allOld' | 'allNew' | 'updatedOld' | 'updatedNew''allNew'
extraInputExtra input that is passed to Update DynamoDB operation. Use it only in case that you know what are you are doing as it may override other properties passed to DynamoDB operation.TransactItems.Updateundefined

Examples

Check out transactionWrite(transactions, options?) function to see example usage.

EntityManager.transaction.delete(primaryKey, options?)

Description

This method is used to build a delete item operation. EntityManager.transaction.delete(primaryKey, options?) used separately won't make a DynamoDB API call. You need to pass it to transactionWrite(transactions, options?) function. This allows to delete multiple items in a single transaction.

Arguments

primaryKey: { [partitionKey]: string | number; [sortKey]?: string | number } - Primary key of the item you want to delete. [partitionKey] and [sortKey] are inferred from the new TableManager(tableEntity, tableMetadata) function.

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

NameDescriptionTypeDefault
conditionInstance of Condition class with conditionals that has to pass to delete the item.Conditionundefined
extraInputExtra input that is passed to Delete DynamoDB operation. Use it only in case that you know what are you are doing as it may override other properties passed to DynamoDB operation.TransactItems.Deleteundefined

Examples

Check out transactionWrite(transactions, options?) function to see example usage.

EntityManager.transaction.condition(primaryKey, conditionInstance)

Description

This method is used to build a conditionCheck when running a transaction. EntityManager.transaction.condition(primaryKey, conditionInstance) used separately won't make a DynamoDB API call. You need to pass it to transactionWrite(transactions, options?) function. This allows add multiple condition to a single transaction.

Arguments

primaryKey: { [partitionKey]: string | number; [sortKey]?: string | number } - Primary key of the item you want to delete. [partitionKey] and [sortKey] are inferred from the new TableManager(tableEntity, tableMetadata) function.

conditionInstance: Condition - Instance of Condition class with conditionals that has to pass in order for transaction to succeed.

Examples

Check out transactionWrite(transactions, options?) function to see example usage.