1.6.6 • Published 10 years ago

node-simple-dynamo v1.6.6

Weekly downloads
28
License
ISC
Repository
-
Last release
10 years ago

Simple AWS DynamoDB driver for nodejs

Simple Node.js DynamoDB driver to perform basic CRUD operations, it is meant to be as simple as possible.

Installation

npm i dynamodb-driver

Initialisation

  var SimpleDynamo = require("node-simple-dynamo"),
      table = SimpleDynamo(awsConfigObject, DynamoDbConfigObject);

Example

  var table = SimpleDynamo({
      region: "eu-west-1"
      // --- your AWS config object if need be (AWS.config.update(awsconfig));
  }, {
    dynamodb: '2012-08-10'
  });

Create row

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

Usage

  somePromise = table.create(tableName, documentToCrate);

Example

  table.create("Users", {
    firstName: "Marilyn",
    lastName: "Manson"
  }).then(function(user) {

    // user is something like
    {
      id : "S1KtimR6"
      firstName: "Marilyn",
      lastName: "Manson"
    }

  });

Update row

Usage

  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"

Example

  database.update("Users", {
    id: "S1KtimR6",
    isPowerUser: true
  }, {
    ConditionExpression: "active = :active",
    ExpressionAttributeValues: {
        ":active": true
    }
  }).then(function(user) {

  });;

Query rows

Usage

  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

Example

  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"
     }]

  });

Get row by Key

Usage

  Promise = database.get(tableName, id).then(function(user) {

Example

  database.get("Users", "Z1et9rR5").then(function(user) {

    // user is something like that
    {
      id: "Z1et9rR5",
      firstName: "Xabi"
      lasName: "Alonso"
    }

  });

Get multiple rows by Key

Usage

  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)

Example

  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"
     }]

  });

Delete row

Usage

  Promise = database.remove(tableName, id)

Example

  database.remove("Users", "Z1et9rR5").then(function(user) {
  // user is
    {
      id: "Z1et9rR5",
      firstName: "Xabi"
        lasName: "Alonso"
    }
  });

List rows

Usage

  Promise = database.list(tableName, query);

Wil perform a scan operation on the selected table

Example

  database.list("Users", [{
    key: "email",
    operator: "EQ",
    value: "some@email.com"
  }]);