Server side Backbone.js sync implementation for DynamoDB.
Execute the following command at the root of your project:
npm install backbone-dynamodb
You can set the AWS credentials and region using any of the following methods:
-
Manually:
Backbone.DynamoDB.setup('accessKeyID', 'secretAccessKey', 'awsRegion');
-
Setting the following environment variables. There's no need to call the
setup()
method, it would get those values automatically:AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_REGION
-
Using an IAM Role. To use IAM Role credentials just assign a role to the EC2 instance when you launch it. backbone-dynamodb will automatically get the credentials from the EC2 metadata service. There's no need to call the
setup()
method, it would get those values automatically, however, if you don't want to use the default AWS region (us-east-1
), you can call thesetup()
method to specify the region you want to use, just set the first two arguments tonull
:Backbone.DynamoDB.setup(null, null, 'us-west-1');
If the a model
is new (isNew()
), meaning that an id
has not been assigned, an UUID string value is generated and set as the id
when calling the save()
method.
idAttribute
: Specifies the name of the attribute that is theHashKeyElement
. The default value isid
.rangeAttribute
: Specifies the name of the attribute that is theRangeKeyElement
. Only needed if the table has aRangeKeyElement
.tableName
: The name of the table to use.urlRoot
: If notableName
is given, the value ofurlRoot
is used to determine the name of the table. First, the'/'
at the beginning, if any, is removed, then the first character is switched to upper case. For instance: ifurlRoot
is'/users'
, the table name is'Users'
tableName
: The name of the table to use.url
: If notableName
is given, the value ofurl
is used to determine the name of the table. First, the'/'
at the beginning, if any, is removed, then the first character is switched to upper case. For instance: ifurl
is'/users'
, the table name is'Users'
When fetching a collection you can use a DynamoDB Query or Scan operation. To use a Query operation set the body of the DynamoDB request in options.query
. To use a Scan operation set the body of the DynamoDB request in options.scan
. You don't need to set the TableName
in either one, it is automatically added to the request body.
The following applies to both Backbone.DynamoDB.Model
and Backbone.DynamoDB.Collection
When calling save(attributes, options)
, destroy(options)
, or fetch(options)
the DynamoDB request body is automatically generated. You can extend the request body using the options.dynamodb
. For instance, you can set the DynamoDB ConsistentRead
option:
model.fetch({
dynamodb: {
ConsistentRead: true
}
// Other options here
});
The DynamoDB response is provided to the success(model, response)
, error(model, response)
, and complete(model, response)
callbacks in response.dynamodb
.
Similar to the context
setting in jQuery.ajax, setting the options.context
when calling save(attributes, options)
, destroy(options)
, or fetch(options)
, will make all callback functions to be called within the given context. In other words, the value of this
, within the callbacks, will be the given options.context
.
The options.complete
callback, if specified, is called after either options.success
or options.error
has been called.
var Backbone = require('backbone-dynamodb');
var fs = require('fs');
var Book = Backbone.DynamoDB.Model.extend({
idAttribute: 'isbn', // The HashKeyElement
urlRoot: '/books' // Table name: 'Books'
});
var Books = Backbone.DynamoDB.Collection.extend({
model: Book,
url: '/books'
});
var Comment = Backbone.DynamoDB.Model.extend({
idAttribute: 'isbn', // The HashKeyElement
rangeAttribute: 'date', // The RangeKeyElement
tableName: 'BookComments', // Table name: 'BookComments'
urlRoot: '/bookcomments'
});
var Comments = Backbone.DynamoDB.Collection.extend({
model: Comment,
tableName: 'BookComments', // Table name: 'BookComments'
url: '/bookcomments'
});
var book1 = new Book({
isbn: 9780641723445,
category: ['book','hardcover'],
title: 'The Lightning Thief',
author: 'Rick Riordan',
genre: 'fantasy',
inStock: true,
price: 12.50,
pages: 384,
publishedDate: new Date(2012, 0, 1) // Date instances are converted into ISO8601 date strings
});
book1.save({}, {
// The original DynamoDB response is available in response.dynamodb
success: function(book, response) {
// response.dynamodb would be something like: {ConsumedCapacityUnits: 1}
},
error: function(book, response) {},
complete: function(book, response) {}
});
var book2 = new Book({isbn: 9781857995879});
book2.fetch({
dynamodb: {
ConsistentRead: true
},
success: function(book, response) {
// Do something here
},
error: function(book, response) {
// response = {code: 'NotFound'} if the book was not found
}
});
var lastYearBooks = new Books();
// fetch all books published in 2011
lastYearBooks.fetch({
scan: { // Use a DynamoDB 'Scan' operation
// No need to specify TableName
ScanFilter: {
publishedDate: {
AttributeValueList: [
{S: new Date(2011, 0, 1)},
{S: new Date(2011, 11, 31)}
],
ComparisonOperator: 'BETWEEN'
}
},
Limit: 100
},
success: function(books, response) {},
error: function(books, response) {}
});
var comments = new Comments();
// Fetch all comments posted after January 31th for the book with ISBN: 9781857995879
comments.fetch({
query: { // Use a DynamoDB 'Query' operation
// No need to specify TableName
HashKeyValue: {N: '9781857995879'},
RangeKeyCondition: {
AttributeValueList: [{S: new Date(2012, 0, 31)}],
ComparisonOperator: 'GT'
},
ConsistentRead: true
},
success: function(comments, response) {},
error: function(comments, response) {}
});
// Use a `Buffer` instance to store binary data
fs.readFile(__dirname + '/cover.png', function(error, data) { // data is an instance of `Buffer`
new Book({
isbn: 9781933988177,
cat: ['book','paperback'],
name: 'Lucene in Action, Second Edition',
author: 'Michael McCandless',
sequence_i: 1,
genre_s: 'IT',
inStock: true,
price: 30.50,
pages_i: 475,
published_date: new Date(2012, 0, 4),
coverImage: data
}).save({}, {
success: function(book, response) {},
error: function(book, response) {},
complete: function(book, response) {}
});
// Note: Binary sets are also supported. Just set an attribute's value to an array of `Buffer` instances.
});