@nuskin/dynamodb-utils
v2.1.2
Published
This package contains helper functionality into DynamoDb
Downloads
233
Keywords
Readme
@nuskin/dynamodb-utils
This module is a wrapper for DynamoDB calls for getting/writting/deleting records from a DynamoDB table.
Installing
Usng npm:
npm add @nuskin/dynamodb-utils
Usng yarn:
yarn add @nuskin/dynamodb-utils
Example usage
const {
batchGet,
batchWrite,
get,
query,
scan,
put,
update,
transactWrite,
remove
} = require('@nuskin/dynamodb-utils');
// batchGet
const tablename = 'your_table';
const keys = [{pKey: 'pkey1', skey: 'skey1'}, {pKey: 'pkey2', skey: 'skey2'}];
const records = await batchGet(keys, tableName);
// batchWrite
const records = [
{pKey: 'pkeyValue', skey: 'skeyValue', field: 'fieldValue'},
{pKey: 'pkeyValue', skey: 'skeyValue', field: 'fieldValue'}
];
await batchWrite(keys, tableName);
// get
const record = await get({Key: {pKey: 'pKeyValue', sKey: 'sKeyValue'}}, tableName);
// query - calls DynamoDBClient.query once passing back its response
const queryResponse = await query({
KeyConditionExpression: 'pKey = :pKey',
ExpressionAttributeValues: {':pKey': 'keyValue'},
Limit: 50
}, tableName);
// query - calls DynamoDBClient.query calling multiple times if necessary to get a complete response
const queryRecords = await queryAll({
KeyConditionExpression: 'pKey = :pKey',
ExpressionAttributeValues: {':pKey': 'keyValue'},
Limit: 50
}, tableName);
// scan - only calls DynamoDBClient once passing back its response
const scanRespons = await scan({ // leave empty if no filtering required
FilterExpression: 'field = :fieldValue',
ExpressionAttributeValues: {
":fieldValue": "test"
}
}, tableName);
// scan - calls DynamoDBClient.scan until the whole table has been scanned.
const scanRecords = await scanTable({ // leave empty if no filtering required
FilterExpression: 'field = :fieldValue',
ExpressionAttributeValues: {
":fieldValue": "test"
}
}, tableName);
// put - puts the given Item to the dynamoDB table
const putResponse = await put({
Item: {
pKey: 'pKeyValue', sKey: 'sKeyValue'}
}, tableName);
// update
const updateResponse = await update(params, tableName);
// transactWrite
const transactWriteResponse = await transactWrite(params, tableName);
// remove
const removeResponse = await remove({Key: {
pKey: 'pKeyValue', sKey: 'sKeyValue'
}}, tableName);
Resources
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/dynamodb/