@uoa/dynamo-utils
v1.0.5
Published
This library contains a helper class which assists with transformations commonly used in projects typescript projects using DynamoDB in University of Auckland projects. This library currently only supports transformation syntax for the dynamoDB document c
Downloads
4
Readme
UOA DynamoHelper
This library contains a helper class which assists with transformations commonly used in projects typescript projects using DynamoDB in University of Auckland projects. This library currently only supports transformation syntax for the dynamoDB document client.
Usage
First initialize the class after import as follows
const DynamoHelper = require("@uoa/dynamo-utils")
let dbHelper = new DynamoHelper("A table","PK", "SK");
This initializes an instance of the dynamoHelper where the partition key of the table is defined to be 'PK', while the sort key for the table is defined as 'SK' and the table name, in this case being "A table". Therefore, the Keys in all transformations will be using the Hash Key provided in the constructor.
Now you can use the helper functions below attached to the class.
transformItemsToDeleteRequests(databaseItems: any[], hashKey: string, sortKey?: string)
transformItemsToUpdateRequests(databaseItems: any[], hashKey: string, sortKey?: string)
Both above methods will transform a list of items to either the syntax required for batch delete requests or batch update requests to be used with the BacthWriteItem operation. The hashKey and sortKey will be the keys used to get the parameter value on the current object in the list of databaseItems to be used as the table hashKey & sortKey values ref: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html
transformItemsToBatchGetRequests(
databaseItems: any[],
hashKey?: string,
sortkey?: string,
numItems?: number
)
This operation will transform the array of objects to be used with the BatchGetItem operation. Like above the hashKey and sortKey will be called on the current object inside of the array of objects. If the keys are undefined, the hashKey and sortKey passed in the constructor will be used. The numItems parameter can be passed in without providing a hashKey or sortKey in the scenario where the keys passed in the constructor should be used on the object. The defualt array size for the Keys parameter is 25. ref: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html
setDynamicUpdateParameters(objectToUpdate: any, operation: string, hashKey?:string, sortKey?:string)
takes in an object and operation (set,remove,add,delete) and returns the respective parameters to perform the operation. hashKey and sortKey are the keys to be called on the objectToUpdate if the object does not use the same hashKey and sortKey as the initialized class, if these parameters are not provided, the initialized hashKey and sortKey (if used) will be used as the keys on the objectToUpdate. If the hashKeys and sortKeys are undefined on the objectToUpdate, an empty Key property will be returned and will need to be set for the operation to be successful to dynamoDB. ref: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html
setDynamicCreateParameters(objectToCreate: any,hashKey?: string,sortKey?: string)
takes an object which is to be used with the PutItem operation on the document client. If hashKey and sortKey is provided, those keys will be used on the objectToCreate to get the values of the object if the hashKey and sortKey passed in the constructor are undefined on the objectToCreate. The default is to use the same hashKey and sortKey as passed in the constructor.
createBatchSlices(databaseItems: any[], tableName?: string)
splits an array into slices of size 25 and below nested in a {RequestItems : 'TABLE_NAME' : []}, this is for batch operations. If a table name is not supplied to the method, it will use the tableName passed into the constructor.