dynamodb-model

Elegant DynamoDB object modeling influenced from MongoDB and the Mongoose API

dynamodb-model

A simple and lightweight object mapper for Amazon DynamoDB, influenced by MongoDB and the Mongoose API. This library allows mapping of DynamoDB tables to Javascript objects using schemas while providing a comfortable high-level API for maximum productivity. It also enables a smooth transition to DynamoDB if you already know MongoDB or Mongoose.

  • Support for the full DynamoDB feature set
  • Models use the official AWS SDK module
  • Independent schemas, free of dependencies
  • Transparent support for MongoDB operators, such as "$gt", "$set" or "$inc"
  • API conventions based on Mongoose
  • Good documentation
  • Good unit test coverage
  • Fast and lightweight
npm install dynamodb-model

To run the tests:

npm test

A schema describes attributes, keys, and indexes for a DynamoDB table. A schema instance allows mapping of Javascript objects to DynamoDB items and vice-versa.

var DynamoDBModel = require('dynamodb-model');
 
var productSchema = new DynamoDBModel.Schema({
  productId: {
    type: Number,
    key: true       // indicates a Hash key 
  },
  sku: String,
  inStock: Boolean, // will be stored as a "Y" or "N" string 
  properties: {},   // will be converted to a JSON string 
  created: Date     // will be converted to a number with Date.getTime 
});

Schemas support the following data types native to DynamoDB:

  • String
  • Number
  • Blob (via Node.js Buffer)
  • Array of strings
  • Array of numbers
  • Array of blobs (buffers)

In addition, schemas also support the following data types with some transformations:

  • Boolean (as a "Y" or "N" string)
  • Date (via Date.getTime as a number)
  • JSON or objects (via JSON.stringify and JSON.parse), defined using an empty object {}

It is also possible to implement you own mapping if necessary:

var DynamoDBModel = require('dynamodb-model');
 
var schema = new DynamoDBModel.Schema({
  /* some fields... */
  customField: {
    dynamoDbType: 'S', // the native DynamoDB type, either S, N, B, SS, NS or BS 
    mapFromDbfunction(value) {
      /* your implementation */
    },
    mapToDbfunction(value) {
      /* your implementation */
    }
  }
});

To specify a Hash or Range key, define a key attribute on the field.

  • Set to true or "hash" to specify a Hash key
  • Set to "range" to specify a Range key

Local secondary indexes are not yet supported.

The specify a default value, use default to specify a static value or a function returning a value.

var DynamoDBModel = require('dynamodb-model');
 
var schema = new DynamoDBModel.Schema({
  ...
  active: {
    type: Boolean,
    default: true
  }
});

Default values replace missing attributes when reading items from DynamoDB.

Schemas are independent from the AWS SDK and can be used with any other DynamoDB client. To map an object to a DynamoDB structure manually, use schema.mapToDb. Likewise, to map a DynamoDB structure to an object, use schema.mapFromDb.

var DynamoDBModel = require('dynamodb-model');
 
var schema = new DynamoDBModel.Schema({
  id: {
    type: Number,
    key: true
  },
  message: String
});
 
schema.mapToDb({ id: 1, message: 'some text' });
// returns { id: { N: '1' }, message: { 'S': 'some text' } }; 
 
schema.mapFromDb({ id: { N: '1' }, message: { 'S': 'some text' } });
// returns { id: 1, message: 'some text' }; 

The Model class provides the high-level API you use to interact with the table, such as reading and writing data. The model class uses the official AWS SDK which already implement most of the best practices, such as automatic retries on a "HTTP 400: Capacity Exceeded" error.

var DynamoDBModel = require('dynamodb-model');
 
var productSchema = new DynamoDBModel.Schema({
  productId: {
    type: Number,
    key: true
  },
  sku: String,
  inStock: Boolean,
  properties: {},
  created: Date
});
 
// create a model using the name of the DynamoDB table and a schema 
var productTable = new DynamoDBModel.Model('dynamo-products', productSchema);
 
// the model provides methods for all DynamoDB operations 
productTable.putItem(/* ... */);
productTable.getItem(/* ... */);
productTable.updateItem(/* ... */);
productTable.deleteItem(/* ... */);
 
// but some of them return intermediate objects in order to provide a better API 
var query = productTable.query(/* ... */);
query.select(/* ... */).limit(100).exec(callback);

The dynamodb-model module uses the official AWS SDK module for low-level operations. To properly connect to your DynamoDB table, make sure you configure the AWS SDK first. More details on the official AWS website here.

var AWS = require('aws-sdk');
 
// setup region and credentials 
AWS.config.update({
    accessKeyId: /* Your acess key */,
    secretAccessKey: /* Your secret key */,
    region: 'us-east-1'
});
 
// specify the API version (optional) 
AWS.config.apiVersions = {
    dynamodb: '2012-08-10'
};

Additionally, if you need to specify options for the AWS.DynamoDB constructor, pass them to the Model constructor like this:

var myTable = new DynamoDBModel.Model(tableName, schema, {
  maxRetries: 1000,
  sslEnabled: true
});

This method is not yet implemented. AWS Documentation for BatchGetItem

This method is not yet implemented. AWS Documentation for BatchWriteItem

Creates the DynamoDB table represented by the schema and returns the AWS service response.

var myTable = new DynamoDBModel.Model(tableName, schema);
myTable.createTable(function (errresponse) {
  // table creation started 
})

AWS Documentation for CreateTable

Deletes a single item in a table by primary key and returns the AWS service response.

var myTable = new DynamoDBModel.Model(tableName, schema);
myTable.deleteItem({ id: 1 }, function (errresponse) {
  // item removed 
})

AWS Documentation for DeleteItem

Note: conditional deletes are not yet implemented.

Removes the table represented by the schema, as well as all items in the table, then returns the AWS service response.

var myTable = new DynamoDBModel.Model(tableName, schema);
myTable.deleteTable(function (errresponse) {
  // table removal started 
})

AWS Documentation for DeleteTable

Returns information about the table represented by the schema, including the current status of the table, when it was created, the primary key schema, and any indexes on the table. The table description is the AWS service response.

var myTable = new DynamoDBModel.Model(tableName, schema);
myTable.describeTable(function (errresponse) {
  // response contains the table description, see AWS docs for more details 
})

AWS Documentation for DescribeTable

Retrieves a specific item based on its primary key, returning the mapped item as well as the AWS service response.

var myTable = new DynamoDBModel.Model(tableName, schema);
myTable.getItem({ id: 1 }, function (erritemresponse) {
  // item represents the DynamoDB item mapped to an object using the schema 
})

AWS Documentation for GetItem

This method is not yet implemented. AWS Documentation for ListTables

TBD AWS Documentation for PutItem

TBD AWS Documentation for Query

TBD AWS Documentation for Scan

TBD AWS Documentation for UpdateItem

TBD AWS Documentation for UpdateTable

TBD

This is the initial release of dynamodb-model and should not be considered production-ready yet. Some features are also missing (see TODO section).

  • Complete documentation
  • Implement local secondary indexes
  • Implement BatchGetItem operation
  • Implement BatchWriteItem operation
  • Add conditional support for DeleteItem operation
  • Add parallel support for Scan operation
  • Improve API to be closer to Mongoose, using aliases for common methods
  • 0.0.1: initial version
  • Tested with Node 0.10.x
  • Tested on Mac OS X 10.8

The MIT License (MIT)

Copyright (c) 2013, Nicolas Mercier

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.