Giter Club home page Giter Club logo

dynamodb-q's Introduction

dynamodb-q

A lightweight Q/Promise-based adapter of the cumbersome node.js dynamodb API

Installation

npm install dynamodb-q --save

Basic Usage

var dq = require('dynamodb-q')
dq.GetItem(dq.Table('employees').Key('name', 'JohnDoe').Attribs(['department', 'title','salary']))
.then(function(data){
    console.log(data);
}, function(reason){
    console.log(reason);
}
);
//If any data is retrieved:
//{ department: 'IT', title: 'Engineer', salary: 120000 }
//Otherwise it will look like this:
//[Error: Empty data]

Region

Default region is set to us-west-2 but you can change this by calling:

dq.SetRegion('your-dynamodb-region');

Table Builder

This constructs a table object to be converted into dynamodb params.
From above example:

dq.Table('employees').Key('name', 'JohnDoe').Attribs(['department', 'title','salary']

In plain English:

  1. For Table named 'employees'
  2. Where the key called 'name' is equal to 'JohnDoe'
  3. Get only these attributes : 'department', 'title','salary'

GetItem

dq.GetItem(dq.Table('employees').Key('name', 'JohnDoe').Attribs(['department', 'title','salary'])).then(...);

UpdateItem

dq.UpdateItem(dq.Table('employees').Key('name', 'JohnDoe').AttribUpdates({salary:140000, title:'Senior Engineer'})).then(...);

PutItem

dq.PutItem(dq.Table('employees').Item({name:'JaneRoe', department:'HR', title:'Recruiter', salary:70000})).then(...);

DeleteItem

dq.DeleteItem(dq.Table('employees').Key('name', 'JohnDoe')).then(...);

Promise Chaining

Since all operations are promises, you can chain them together (callback flattening) like so:

dq.GetItem(dq.Table('Purchases').Key('ID', 1).Attribs(['ProductName', 'Amount']))
.then(function(data){
    return [data, dq.GetItem(dq.Table('Stock').Key('ProductName', data.ProductName).Attribs(['Remaining']))];
})
.spread(function(purchaseData, stockData){
    return dq.UpdateItem(dq.Table('Stock').Key('ProductName', purchaseData.ProductName).AttribUpdates({Remaining: stockData.Remaining - purchaseData.Amount}));
});
.then(null, function(reason){
    console.log('Failed to update stock because ' + reason);
});

For learning how promises can be used you can refer to the Q Library

dynamodb-q's People

Contributors

talentjp avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.