Skip to content
/ dino Public

A simple DynamoDB object modeler for Node.js.

License

Notifications You must be signed in to change notification settings

koenbok/dino

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dino

A simple Amazon DynamoDB object modeler for Node.js. Dino is designed to leverage the fast (and inexpensive) parts of the DynamoDB API by default. The complete DynamoDB client is exposed for extensibility.

Build Status

Usage

var dino = require('dino');

dino.connect({
    accessKeyId: 'YOUR_KEY',
    secretAccessKey: 'YOUR_SECRET',
    region: 'us-east-1'
});

var Forum = dino.model({
    schema: dino.schema({
        table: 'forums',
        attributes: {
            name: dino.types.string,
            category: dino.types.string
        },
        key: {
            hash: 'name'
        }
    })
});

var forum = Forum.create({
    name: 'Amazon DynamoDB',
    category: 'Amazon Web Services'
});

forum.save();

Installation

$ npm install dino

API

connect(options)

Sets the default DynamoDB client for your application. Alternatively, you can omit this and use environment variables.

dino.connect({
    accessKeyId: 'YOUR_KEY',
    secretAccessKey: 'YOUR_SECRET',
    region: 'us-east-1'
});

options

  • accessKeyId (required)
  • secretAccessKey (required)
  • region (required)

connection.client

The default DynamoDB client.

connection.create(options)

Creates a DynamoDB client to be used at your will.

var client = dino.connection.create({
    accessKeyId: 'YOUR_OTHER_KEY',
    secretAccessKey: 'YOUR_OTHER_SECRET',
    region: 'us-east-1'
});

options

  • accessKeyId (required)
  • secretAccessKey (required)
  • region (required)

schema(options)

Creates a schema.

var forumSchema = dino.schema({
    table: 'forums',
    attributes: {
        name: dino.types.string,
        date_created: dino.types.date,
        author: dino.types.string
    },
    key: {
        hash: 'name',
        range: 'date_created',
        secondary: 'author'
    }
});

options

  • table (required)
  • attributes (required)
  • key (required)
  • keyDelimiter

types

  • boolean
  • date
  • id
  • number
  • object
  • string

schema.createTable(options);

Creates a table in DynamoDB.

forumSchema.createTable({
    readUnits: 5,
    writeUnits: 2
}, function(err){  });

options

  • client
  • readUnits
  • writeUnits

type(options)

Creates a schema type.

var myType = dino.type({
    defaultValue: null,
    serialize: function (val) { return val; },
    deserialize: function (val) { return val; },
    toJSON: function (val) { return val; }
});

options

  • defaultValue
  • serialize
  • deserialize
  • toJSON

model(options)

Creates a Model object. Use Model objects to create and query models.

var Forum = dino.model({
    schema: forumSchema
});

options

  • schema (required)
  • client

Model.create(attributes)

Creates a model.

var forum = Forum.create({
    name: 'Amazon DynamoDB',
    category: 'Amazon Web Services'
});

Model.findOne(match[, callback])

Queries DynamoDB for a single model.

Forum.findOne({
    name: 'Amazon DynamoDB'
}, function(err, forum, units){  });

Model.find(options[, callback])

Queries DynamoDB for a collection of models.

Reply.find({
    match: {
        forum_name: 'Amazon DynamoDB',
        thread_name: 'DynamoDB Thread 1'
    }
    take: 10
}, function(err, replies, units){  });

options

  • match (required)
  • skip
  • take
  • sortBy

Model.destroy(match[, callback])

Deletes a model from DynamoDB.

Forum.destroy({
    name: 'Amazon DynamoDB'
}, function(err, units){  });

model.set(attributes)

Sets the model's attributes.

forum.set({
    name: 'Amazon S3'
});

model.get(attribute)

Gets the model's attributes.

forum.get('name'); // 'Amazon S3'

model.save([callback])

Saves the model to DynamoDB.

forum.save(function(err, units){  });

model.destroy([callback])

Deletes the model from DynamoDB.

forum.destroy(function(err, units){  });

model.toJSON()

Returns the JSON serialized attributes of the model.

forum.toJSON();

collection.toJSON()

Returns an array where each model in the collection has been JSON serialized.

replies.toJSON();

collection.models

The raw array of models in the collection.

replies.models;

Tests

Install the dependencies and run.

$ npm install
$ npm test

Changelog

0.3.3 — May 9, 2013

  • Applied _.bindAll to Model and Schema objects for easier flow control with async.

0.3.2 — May 8, 2013

  • Fixed regression where Model.findOne() would fail silently on tables w/o a range key.

0.3.1 — May 8, 2013

  • Fixed regression where instance members weren't being assigned on Model.create().

0.3.0 — May 5, 2013

  • Migrated to DynamoDB API version 2012-08-10, which allows for secondary indexes. You can now query, sort and destroy by secondary index. Review the documentation on find(), findOne, and destroy() to see the changes to the API.

About

A simple DynamoDB object modeler for Node.js.

Resources

License

Stars

Watchers

Forks

Packages

No packages published