Skip to content

Instantly share code, notes, and snippets.

@aevans-mms
Created October 9, 2024 13:50
Show Gist options
  • Save aevans-mms/9a02d09ab2600da284c9989ec1147ca3 to your computer and use it in GitHub Desktop.
Save aevans-mms/9a02d09ab2600da284c9989ec1147ca3 to your computer and use it in GitHub Desktop.
AWS DynamoDB TypeScript Commands Wrapper and Simple Class
import * as dynamodb from "@aws-sdk/client-dynamodb"
const commands = {
"client" : (params:dynamodb.DynamoDBClientConfig) => new dynamodb.DynamoDBClient(params),
"listTables" : (params:dynamodb.ListTablesCommandInput) => new dynamodb.ListTablesCommand(params),
"describeTable" : (params:dynamodb.DescribeTableCommandInput)=>new dynamodb.DescribeTableCommand(params),
"query" : (params:dynamodb.QueryCommandInput) => new dynamodb.QueryCommand(params),
"scan" : (params:dynamodb.ScanCommandInput) => new dynamodb.ScanCommand(params),
"getItem" : (params:dynamodb.GetItemCommandInput) => new dynamodb.GetItemCommand(params),
"putItem" : (params:dynamodb.PutItemCommandInput) => new dynamodb.PutItemCommand(params),
"updateItem" : (params:dynamodb.UpdateItemCommandInput) => new dynamodb.UpdateItemCommand(params),
"deleteItem" : (params:dynamodb.DeleteItemCommandInput) => new dynamodb.DeleteItemCommand(params),
"batchGetItem" : (params:dynamodb.BatchGetItemCommandInput) => new dynamodb.BatchGetItemCommand(params),
"batchWriteItem" : (params:dynamodb.BatchWriteItemInput) => new dynamodb.BatchWriteItemCommand(params),
"executeStatement" : (params:dynamodb.ExecuteStatementCommandInput) => new dynamodb.ExecuteStatementCommand(params),
"executeTransaction" : (params:dynamodb.ExecuteTransactionCommandInput) => new dynamodb.ExecuteTransactionCommand(params),
"transactGetItems" : (params:dynamodb.TransactGetItemsCommandInput) => new dynamodb.TransactGetItemsCommand(params),
"transactWriteItems" : (params:dynamodb.TransactWriteItemsCommandInput) => new dynamodb.TransactWriteItemsCommand(params),
// ... more commands
}
class DynamoDB {
private client:dynamodb.DynamoDBClient
constructor(params?:dynamodb.DynamoDBClientConfig) {
params = params || {}
this.client = new dynamodb.DynamoDBClient(params)
}
getClient():dynamodb.DynamoDBClient {
return this.client
}
setClient(client:dynamodb.DynamoDBClient):void {
this.client = client
}
async listTables(params?:dynamodb.ListTablesCommandInput):Promise<dynamodb.ListTablesCommandOutput> {
params = params || {}
return await this.client.send(commands.listTables(params))
}
async describeTable(params:dynamodb.DescribeTableCommandInput):Promise<dynamodb.DescribeTableCommandOutput> {
return await this.client.send(commands.describeTable(params))
}
async query(params:dynamodb.QueryCommandInput):Promise<dynamodb.QueryCommandOutput> {
return await this.client.send(commands.query(params))
}
async scan(params:any) {
return await this.client.send(commands.scan(params))
}
async getItem(params:dynamodb.GetItemCommandInput):Promise<dynamodb.GetItemCommandOutput> {
return await this.client.send(commands.getItem(params))
}
async putItem(params:dynamodb.PutItemCommandInput):Promise<dynamodb.PutItemCommandOutput> {
return await this.client.send(commands.putItem(params))
}
async updateItem(params:dynamodb.UpdateItemCommandInput):Promise<dynamodb.UpdateItemCommandOutput> {
return await this.client.send(commands.updateItem(params))
}
async deleteItem(params:dynamodb.DeleteItemCommandInput):Promise<dynamodb.DeleteItemCommandOutput> {
return await this.client.send(commands.deleteItem(params))
}
async batchGetItem(params:dynamodb.BatchGetItemCommandInput):Promise<dynamodb.BatchGetItemCommandOutput> {
return await this.client.send(commands.batchGetItem(params))
}
async batchWriteItem(params:dynamodb.BatchWriteItemCommandInput):Promise<dynamodb.BatchWriteItemCommandOutput> {
return await this.client.send(commands.batchWriteItem(params))
}
async executeStatement(params:dynamodb.ExecuteStatementCommandInput):Promise<dynamodb.ExecuteStatementCommandOutput> {
return await this.client.send(commands.executeStatement(params))
}
async executeTransaction(params:dynamodb.ExecuteTransactionCommandInput):Promise<dynamodb.ExecuteTransactionCommandOutput> {
return await this.client.send(commands.executeTransaction(params))
}
async transactGetItems(params:dynamodb.TransactGetItemsCommandInput):Promise<dynamodb.TransactGetItemsCommandOutput> {
return await this.client.send(commands.transactGetItems(params))
}
async transactWriteItems(params:dynamodb.TransactWriteItemsCommandInput):Promise<dynamodb.TransactWriteItemsCommandOutput> {
return await this.client.send(commands.transactWriteItems(params))
}
//...more commands
}
export default { commands, DynamoDB }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment