dynamodb-simplify
v1.0.1
Published
A simple utility to interact with AWS DynamoDB using the AWS SDK. This package simplifies common DynamoDB operations such as inserting, retrieving, updating, deleting, and querying items. It also supports batch operations and transactional operations.
Readme
DynamoDBHelper
A simple utility to interact with AWS DynamoDB using the AWS SDK. This package simplifies common DynamoDB operations such as inserting, retrieving, updating, deleting, and querying items. It also supports batch operations and transactional operations.
Installation
To install the package, run: "npm install dynamodb-helper"
Usage
1. Initialize DynamoDBHelper
To initialize DynamoDBHelper, you need to provide AWS credentials and the region. Here’s how you can do it:
const DynamoDBHelper = require('dynamodb-helper');
"const dynamoHelper = new DynamoDBHelper({ accessKeyId: 'YOUR_ACCESS_KEY_ID', secretAccessKey: 'YOUR_SECRET_ACCESS_KEY', region: 'YOUR_REGION', });"
2. Available Methods
putItem(tableName, item)
Adds an item to a DynamoDB table.
tableName: Name of the DynamoDB table. item: The item to add. await dynamoHelper.putItem('YourTableName', { id: '1', itemName: 'TestItem' });
getItem(tableName, key)
Fetches an item from a DynamoDB table using the specified key. tableName: Name of the DynamoDB table. key: The key of the item to retrieve. const result = await dynamoHelper.getItem('YourTableName', { id: '1' }); console.log(result);
query(tableName, queryParams)
Queries items in a DynamoDB table based on the specified query parameters.
tableName: Name of the DynamoDB table. queryParams: Query parameters (e.g., KeyConditionExpression, ExpressionAttributeValues). const result = await dynamoHelper.query('YourTableName', { KeyConditionExpression: 'id = :id', ExpressionAttributeValues: { ':id': '1', }, }); console.log(result);
updateItem(tableName, key, updateExpression, expressionAttributeValues)
Updates an item in DynamoDB.
tableName: Name of the DynamoDB table. key: The key of the item to update. updateExpression: The update expression. expressionAttributeValues: Values for the update expression. await dynamoHelper.updateItem('YourTableName', { id: '1' }, 'SET itemName = :newName', { ':newName': 'UpdatedTestItem', });
deleteItem(tableName, key)
Deletes an item from DynamoDB.
tableName: Name of the DynamoDB table. key: The key of the item to delete. await dynamoHelper.deleteItem('YourTableName', { id: '1' });
batchWrite(requestItems)
Performs a batch write to DynamoDB.
requestItems: A map of request items with PutRequest or DeleteRequest. const batchWriteParams = { 'YourTableName': [ { PutRequest: { Item: { id: '2', name: 'BatchItem1' } } }, { PutRequest: { Item: { id: '3', name: 'BatchItem2' } } }, ], }; await dynamoHelper.batchWrite(batchWriteParams);
batchGet(requestItems)
Performs a batch get from DynamoDB.
requestItems: A map of keys for the items you want to retrieve. const batchGetParams = { 'YourTableName': { Keys: [{ id: '2' }, { id: '3' }], }, }; const batchGetResult = await dynamoHelper.batchGet(batchGetParams); console.log(batchGetResult);
transactWrite(transactItems)
Performs transactional write operations to DynamoDB.
transactItems: A list of transaction items (Put, Delete, etc.). const transactWriteParams = [ { Put: { TableName: 'YourTableName', Item: { id: '4', name: 'TransactItem1' }, }, }, { Put: { TableName: 'YourTableName', Item: { id: '5', name: 'TransactItem2' }, }, }, ]; await dynamoHelper.transactWrite(transactWriteParams);
transactGet(transactItems)
Performs transactional get operations from DynamoDB.
transactItems: A list of transaction items (Get). const transactGetParams = [ { Get: { TableName: 'YourTableName', Key: { id: '4' }, }, }, { Get: { TableName: 'YourTableName', Key: { id: '5' }, }, }, ]; const transactGetResult = await dynamoHelper.transactGet(transactGetParams); console.log(transactGetResult);
scan(tableName, filterParams)
Scans a DynamoDB table.
tableName: Name of the DynamoDB table. filterParams: Optional filter parameters for the scan operation. const scanResult = await dynamoHelper.scan('YourTableName'); console.log(scanResult);
3. Error Handling
If there is an error while interacting with DynamoDB, the methods will throw an error. Make sure to use try-catch blocks to handle errors.
try { await dynamoHelper.putItem('YourTableName', { id: '1', itemName: 'TestItem' }); } catch (error) { console.error('Error occurred:', error.message); }
This content provides a full overview of how to use your DynamoDBHelper package, including the installation, code examples, and error handling practices.
