npdynamodb
A Node.js Simple Query Builder and ORM for AWS DynamoDB.
Motivation
When I visited here for the first time, I closed it in a moment. Because it is too long and hard to see to understand. So I decided to make client to handle DynamoDB more easier and it doesn't take waste of time to read documentation for it.
Services that are used in Production
Supported DynamoDB Api Versions
- 2012-08-10
Installation
npm install npdynamodb
Why is the Pure AWS-SDK in Node.js NOT good?
Parameters are like Chant of the magic. http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html
Overview
Npdynamodb has modern interfaces to handle AWS DynamoDB. We release you redundancy codes and see simple syntax. Of course, will not see callback hell!
Npdynamodb has the following functions
List of npdynamodb apis
Usage of QueryBuilder
Initialization
var npdynamodb = ;var AWS = ; var dynamodb = apiVersion: '2012-08-10'; var npd = npdynamodb; // Or can take optionsvar npd = npdynamodb;
Get by hash key (getItem operation)
;
Get rows with where (query operation)
//hash key;
Get multiple rows with where, filter and descending order
// hash key // range key // non index key;
whereIn
whereIn call batchGetItem instead of query operation.
Single Key Usage
;
Multiple Kyes Usage
;
Limit and offset
;
Count
;
Extra options
You can set extra options in callback of feature
method. All options are transformed from property to method, But its name (camelized) and arguments are same as pure AWS-SDK for node.js.
;
create (Make Overwrite all of values, if key[s] have already existed.)
;
Update
;
Update with expressions
;
Usage of ORM
Initialization
var npdynamodb = ;var AWS = ; var npd = npdynamodb; var Chat = npdynamodb;
Fast get with hash_key
Chat;
fetch with multiple conditions
Chat// complex conditions;
Save
// As StaticChat; // As Instancevar chat = room_id: 'room1' user_id: 1;chat; chat;
Destroy
chat;
Custom Methods and Properties
var Chat = npdynamodb; // prototypeChat; // staticconsole; Chat;
Migration
We support schema migration for Dynamodb.
First, initialize your project to run migration.
npm install -g npdynamodb# cd /path/to/your/project npd init# created npdfile.js
npdfile.js
'use strict'; var AWS = ; var dynamodb = apiVersion: '2012-08-10' accessKeyId: "AWS_KEY" secretAccessKey: "AWS_SECRET" region: "ap-northeast-1"; moduleexports = // Specify migration file path. Default is `./migrations` // migration: { // migrationFilePath: './npdynamodb_migrations' // }, development: dynamoClient: dynamodb migrations: ProvisionedThroughput: 10 10 tableName: 'npd_migrations' staging: dynamoClient: dynamodb migrations: ProvisionedThroughput: 10 10 tableName: 'npd_migrations' production: dynamoClient: dynamodb migrations: ProvisionedThroughput: 10 10 tableName: 'npd_migrations' ;
Generate migration file.
npd migrate:generate create_users# => /migrations/20150406083039_create_users.js
Edit migration file
exports{ return ;}; exports{ return ;};
UpdateTable Usage
exports{ return ;};
Run latest migration.
npd migrate:run
Rollback latest migration.
npd migrate:rollback
Command Line Interfaces
npd
required global install and type Commands
init
: Create a fresh npdfile.js.migrate:generate <name>
Create a named migration file.migrate:run
Run all migrations that have not yet been run.migrate:rollback
Rollback the last set of migrations performed.listTables
List existing tables.dump <table>
: Dump amount of records in specified table to stdout.desc <table>
: Show result of the describe operationget <table> <hashKey> [rangeKey]
: Show results of the query operation by given conditions.dropTable <table>
: Drop the specified table.
Global Options
-h
-V
--env
How to test?
npm test
QueryBuilder Callbacks
You can be hooked several events and their can be taken promise.
Mechanism of Callbacks and Events
operation called.
↓
callbacks: beforeQuery
↓
events: beforeQuery
↓
Sending Request to Dynamodb
↓
Getting Response from Dynamodb
↓
callbacks: afterQuery
↓
events: afterQuery
// Register callbacks globallyvar npd = npdynamodb; // Register callbacks at only this time.;
Plugin and Extending
Npdynamodb can be extended via plugins.
npdynamodb;
Available Plugins
- npdynamodb-typecast For casting hash and range key to actual attribution type
Browser Support
Npdynamodb can be built using browserify or webpack, and pre-built or pre-built with uglified version can be found in the build directory.
For Browserify or Webpack
var AWS = ;var npdynamodb = ; var dynamodb = apiVersion: '2012-08-10' accessKeyId: "here is key" secretAccessKey: "here is secret key" region: "ap-northeast-1" sslEnabled: true; var npd = npdynamodb;;
For HTML
Upgrading and Release Note
Upgrading 0.1x -> 0.2x
QueryBuilder
There should be a minor change for QueryBuilder. 0.2x QueryBuilder can take options as second argument of createClient.
- 0.2.0:
timeout
option supported. - 0.2.6:
initialize
option and callbacks supported. - 0.2.7: whereIn method supported.
ORM
There should be a major change for ORM. 0.2x ORM constructor need to pass the npdynamodb instance instead of pure dynamodb instance.
- 0.2.7: Supported to parse
whereIn
results.
License
(The MIT License)
Copyright (c) 2015 Yuki Takei(Noppoman) yuki@miketokyo.com
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 marthis 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.