Transaction(optionsopt)

Transaction context.

new Transaction(optionsopt)

Parameters:
Name Type Attributes Description
options TransactionOptions <optional>
Options for the transaction

Members

defaultOptions

Returns the default options for a transaction.

EVENTS

All events transactions may emit. POST_COMMIT: When a transaction is committed. Do clean up, summery, post process here. TX_FAILED: When a transaction failed permanently (either by failing all retries, or getting a non-retryable error). Handler has the signature of (error) => {}.

Methods

async, static run(optionsopt, func)

Runs a function in transaction, using specified parameters. If a non-retryable error is thrown while running the transaction, it will be re-raised.
Parameters:
Name Type Attributes Description
options TransactionOptions <optional>
func function the closure to run.
Example
// Can be called in 2 ways:
Transaction.run(async (tx) => {
  // Do something
})

// Or
Transaction.run({ retryCount: 2 }, async (tx) => {
  // Do something
})

async __batchGetItems(keys, params)

Gets multiple items using DynamoDB's batchGetItems API.
Parameters:
Name Type Description
keys Array.<Key> A list of keys to get.
params GetParams Params used to get items, all items will be fetched using the same params.

async __getItem(key, params)

Get an item using DynamoDB's getItem API.
Parameters:
Name Type Description
key Key A key for the item
params GetParams Params for how to get the item

async __transactGetItems(keys, params)

Gets multiple items using DynamoDB's transactGetItems API.
Parameters:
Name Type Description
keys Array.<Key> A list of keys to get.
params GetParams Params used to get items, all items will be fetched using the same params.

create(Cls, data)

Creates a model without accessing DB. Write will make sure the item does not exist.
Parameters:
Name Type Description
Cls Model A Model class.
data CompositeID | Object A superset of CompositeID of the model, plus any data for Fields on the Model.

createOrPut(Cls, original, updated)

Creates or puts an item without reading from DB. It differs from update in that: a) If item doesn't exists, a new item is created in DB b) If item does exists, fields present locally will overwrite values in DB, fields absent locally will be removed from DB.
Parameters:
Name Type Description
Cls Class The model's class.
original CompositeID | Object A superset of CompositeID, field's values. Non-key values are used for conditional locking
updated Object Final values for the model. Values for every field in the model must be provided. Fields with `undefined` value will be removed from DB.

delete(…args)

Deletes model(s) from database. If a model is read from database, but it did not exist when deleting the item, an exception is raised.
Parameters:
Name Type Attributes Description
args List.<(Key|Model)> <repeatable>
Keys and Models

enableModelCache()

Enables model cache

async get(Cls, key, paramsopt)

Fetches model(s) from database. This method supports 3 different signatures. get(Cls, keyOrDataValues, params) get(Key|Data, params) get([Key|Data], params) When only one items is fetched, DynamoDB's getItem API is called. Must use a Key when createIfMissing is not true, and Data otherwise. When a list of items is fetched: If inconsistentRead is false (the default), DynamoDB's transactGetItems API is called for a strongly consistent read. Transactional reads will be slower than batched reads. If inconsistentRead is true, DynamoDB's batchGetItems API is called. Batched fetches are more efficient than calling get with 1 key many times, since there is less HTTP request overhead. Batched fetches is faster than transactional fetches, but provides a weaker consistency.
Parameters:
Name Type Attributes Description
Cls Class a Model class.
key String | CompositeID Key or keyValues
params GetParams <optional>
Returns:
- Model(s) associated with provided key

getModelDiffs()

Return before and after snapshots of all relevant models.

makeReadOnly()

Marks a transaction as read-only.

query(ModelCls)

Create a handle for applications to query DB items.
Parameters:
Name Type Description
ModelCls Model A model class.
params.options IteratorOptions Iterator options
Returns:
- Query handle. See __DBIterator for details.

scan(ModelCls)

Create a handle for applications to scan DB items.
Parameters:
Name Type Description
ModelCls Model A model class.
params.options IteratorOptions Iterator options
Returns:
- Scan handle. See __DBIterator for details.

update(Cls, original, updated)

Updates an item without reading from DB. If the item doesn't exist in DB, ConditionCheckFailure will be thrown.
Parameters:
Name Type Description
Cls Class The model's class.
original CompositeID | Object A superset of CompositeID, field's values. Used as conditions for the update
updated Object Updated fields for the item, without CompositeID fields.