Simple Node.js DynamoDB driver to perform basic CRUD operations, it is meant to be as simple as possible.
npm i dynamodb-driver
var DDbDriver = require("dynamodb-driver"),
table = DDbDriver(awsConfigObject, DynamoDbConfigObject);
var table = DDbDriver({
region: "eu-west-1"
// --- your AWS config object if need be (AWS.config.update(awsconfig));
}, {
dynamodb: '2012-08-10'
});
Creates a row in the specified table, and id will be automatically generated using shorId if non exists in the document Returns a promise resolving a document that has just been created
somePromise = table.create(tableName, documentToCrate);
table.create("Users", {
firstName: "Marilyn",
lastName: "Manson",
active: true
}).then(function(user) {
// user is something like
{
id : "S1KtimR6"
firstName: "Marilyn",
lastName: "Manson",
active: true
}
});
somePromise = table.update(tableName, documentToUpdate [, conditions] [, keys]);
conditions: An object with a ConditionExpression and a ExpressionAttributeValues (See: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html)
keys: Should you table is not using id as primary key, you can specify your primary and sort key here such as ["organisationId", "documentId"]
Specify a value within brackets with a exclamation mark to perform an update only if the value doesn't exist
{
id: "S1KtimR6",
creationDate: "[!" + new Date().getTime() + "]"
}
Specify a value within brackets with ++ to perform an increment by one of the existing value
{
id: "S1KtimR6",
count: "[++]"
}
Specify a value within brackets with -- to perform an decrement by one of the existing value
{
id: "S1KtimR6",
count: "[--]"
}
table.update("Users", {
id: "S1KtimR6",
isPowerUser: true
}, {
ConditionExpression: "active = :active",
ExpressionAttributeValues: {
":active": true
}
}).then(function(user) {
// user would be something like that
{
id : "S1KtimR6"
firstName: "Marilyn",
lastName: "Manson",
active: true,
isPowerUser: true
}
});
somePromise = table.query(tableName, query [, indexName] [, options]);
query:
- could be an array for the query with the key, ComparisonOperator and AttributeValueList for legacy KeyConditions.
- could be an object with a KeyConditionExpression and ExpressionAttributeNames
// Using the legacy KeyConditions mode
table.query("Users", [{
key: "email",
operator: "EQ",
value: email
},{
key: "password",
operator: "EQ",
value: password
}], "EmailPasswordIndex").then(function(users) {
// users is an Array of users
[{
id: "S1KtimR6",
firstName: "Marilyn"
lasName: "Manson",
active: true,
isPowerUser: true
}, {
id: "Z1et9rR5",
firstName: "Amaia"
lasName: "Albistur",
active: true,
isPowerUser: false
}]
});
somePromise = table.get(tableName, id);
table.get("Users", "Z1et9rR5").then(function(user) {
// user is something like that
{
id: "Z1et9rR5",
firstName: "Amaia"
lasName: "Albistur",
active: true,
isPowerUser: false
}
});
somePromise = table.getItems(tableName, arrayOfIds [, options]);
arrayOfIds: An array of ids to retrieve or an array of objects containing the partition and sort key of the items to retrieve
options: is an optional object
options.consistentRead: true or false, will apply strongly consistent reads instead of the default setting (eventually consistent reads)
options.keys: an array of 1 or 2 strings defining the partition and sort keys
table.getItems("Users", ["Z1et9rR5", "S1KtimR6"], {consistentRead: true}).then(function(users) {
// users is an Array of users
[{
id: "S1KtimR6",
firstName: "Marilyn"
lasName: "Manson",
active: true,
isPowerUser: true
}, {
id: "Z1et9rR5",
firstName: "Amaia"
lasName: "Albistur",
active: true,
isPowerUser: false
}]
});
somePromise = table.remove(tableName, id);
table.remove("Users", "Z1et9rR5").then(function(user) {
// user is
{
id: "Z1et9rR5",
firstName: "Amaia"
lasName: "Albistur",
active: true,
isPowerUser: false
}
});
somePromise = table.removeItems(tableName, arrayOfObject[, keys]);
keys: when not using id as a primary, you must specify the HASH and optionally RANGE key
table.removeItems("Users", [{ id: "S1KtimR6"}, {id : "Z1et9rR5"}]).then(function(users) {
// users is an Array of users
[{
id: "S1KtimR6",
firstName: "Marilyn"
lasName: "Manson",
active: true,
isPowerUser: true
}, {
id: "Z1et9rR5",
firstName: "Amaia"
lasName: "Albistur",
active: true,
isPowerUser: false
}]
});
somePromise = table.list(tableName[, query, options]);
Will perform a scan operation on the selected table
query: A specific query array to filter data against options: An object with a paginate property as number. It will recursively fetch batches of paginate value until all data is fetched for this query (note, no exponential backing off)
table.list("Users", [{
key: "isPowerUser",
operator: "EQ",
value: true
}], {
paginate: 10 // Will retieve all data by batches of 10
}).then(function(users) {
// users is an Array of users
[{
id: "S1KtimR6",
firstName: "Marilyn"
lasName: "Manson",
active: true,
isPowerUser: true
}, {
id: "X1ttUU45",
firstName: "Francis"
lasName: "Kuntz",
active: true,
isPowerUser: true
}]
});