Simple Node.js DynamoDB driver to perform basic CRUD operations, it is meant to be as simple as possible.
npm i dynamodb-driver
var SimpleDynamo = require("node-simple-dynamo"),
table = SimpleDynamo(awsConfigObject, DynamoDbConfigObject);
var table = SimpleDynamo({
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"
}).then(function(user) {
// user is something like
{
id : "S1KtimR6"
firstName: "Marilyn",
lastName: "Manson"
}
});
somePromise = table.update(tableName, documentToUpdate [, conditions] [, keys]);
conditions: An object with a ConditionExpression and a ExpressionAttributeValues (See: http://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"]
database.update("Users", {
id: "S1KtimR6",
isPowerUser: true
}, {
ConditionExpression: "active = :active",
ExpressionAttributeValues: {
":active": true
}
}).then(function(user) {
});;
Promise = database.query(tableName, query [, indexName] [, options])
query:
- could be an array for the query with the key, ComparisonOperator and AttributeValueList for legacy.
- could be an object with a KeyConditionExpression and ExpressionAttributeNames
database.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"
}, {
id: "Z1et9rR5",
firstName: "Xabi"
lasName: "Alonso"
}]
});
Promise = database.get(tableName, id).then(function(user) {
database.get("Users", "Z1et9rR5").then(function(user) {
// user is something like that
{
id: "Z1et9rR5",
firstName: "Xabi"
lasName: "Alonso"
}
});
Promise = database.getItems(tableName, arrayOfIds [, options])
options: is an optional object with consistentRead attribute. it will apply strongly consistent reads instead of the default setting (eventually consistent reads)
database.getItems("Users", ["Z1et9rR5", "S1KtimR6"], {consistentRead: true}).then(function(users) {
// users is an Array of users
[{
id: "S1KtimR6",
firstName: "Marilyn"
lasName: "Manson"
}, {
id: "Z1et9rR5",
firstName: "Xabi"
lasName: "Alonso"
}]
});
Promise = database.remove(tableName, id)
database.remove("Users", "Z1et9rR5").then(function(user) {
// user is
{
id: "Z1et9rR5",
firstName: "Xabi"
lasName: "Alonso"
}
});
Promise = database.list(tableName, query);
Wil perform a scan operation on the selected table
database.list("Users", [{
key: "email",
operator: "EQ",
value: "some@email.com"
}]);