All model requests are created by using the corresponding method on a DynamoStore instance. A new request instance for the related model/table will be returned.
Request Params
Every request instance contains the public params property where all params for the request are written to. You might want to use it for debugging purpose but are also able to write on it (eg. non-covered features).
Consumed Capacity
You might want to receive the consumed capacity which can be achieved by applying the returnConsumedCapacity('INDEXES' | 'TOTAL') method and the usage of execFullResponse() instead of exec().
Execution
All requests provide at least two execution methods:
exec() returns a promise which will resolve to the requested items or void if no items were requested
execFullResponse() returns a promise which will resolve to the full response DynamoDB returns, but response.Items are still mapped for you.
Get
get.snippet.ts
import { DynamoStore } from'@shiftcoders/dynamo-easy'import { Person } from'../models'newDynamoStore(Person).get('wernerv') // returns an instance of GetRequest.consistentRead(true) // sets params.ConsistentRead = true.exec() // returns a Promise<Person|null>.then(obj =>console.log(obj))
Put
put.snippet.ts
import { DynamoStore } from'@shiftcoders/dynamo-easy'import { Person } from'../models'constobjectToPut:Person= { id:'vogelsw', name:'Werner Hans Peter Vogels', yearOfBirth:1958,} // object literal or new Person(...)newDynamoStore(Person).put(objectToPut).ifNotExists().exec().then(() =>console.log('done'))
import { attribute, DynamoStore, or, update } from'@shiftcoders/dynamo-easy'import { AnotherModel } from'../models'constindex=3constoneHourAgo=newDate(Date.now() -1000*60*60)newDynamoStore(AnotherModel).update('myPartitionKey','mySortKey').operations(update(`myNestedList[${index}].propertyX`).set('value'),update('updated').set(newDate()), ).onlyIf(or(attribute('id').attributeNotExists(),// item not existingattribute('updated').lt(oneHourAgo),// or was not updated in the last hour ), ).returnValues('ALL_OLD').exec().then(oldVal =>console.log('old value was:', oldVal))
Delete
delete.snippet.ts
import { DynamoStore } from'@shiftcoders/dynamo-easy'import { Person } from'../models'newDynamoStore(Person).delete('vogelsw').onlyIfAttribute('yearOfBirth').lte(1958).exec().then(() =>console.log('done'))
This is a special implementation from BatchWriteRequest which only allows to write to a single table
batch-write.snippet.ts
import { DynamoStore } from'@shiftcoders/dynamo-easy'import { Person } from'../models'newDynamoStore(Person).batchWrite().delete([{ id:'a' }, { id:'b' }]).put([{ id:'vogelsw', name:'Werner Hans Peter Vogels', yearOfBirth:1958 }]).exec().then(() =>console.log('item a, b deleted; werner vogels added'))
TransactGet
This is a special implementation from TransactGetRequest which only allows to read from a single table.
transact-get.snippet.ts
import { DynamoStore } from'@shiftcoders/dynamo-easy'import { Person } from'../models'newDynamoStore(Person).transactGet([{ id:'a' }, { id:'b' }]).exec().then(() =>console.log('transactionally read a and b'))