@awspilot/dynamodb

Issue Star Fork

@Awspilot's DynamoDB

Speak fluent DynamoDB, write code with fashion, I Promise() 😃


@awspilot/dynamodb is a NodeJS and Browser utility to access Amazon DynamoDB databases
Main library goals are:

  • Compatible with all NodeJS versions ( no ES6+ )
  • Backword compatible with all previous versions
  • Lightweight ( depends only on aws-sdk and promise )
  • Good readability of the code

    Install in NodeJS

    npm install @awspilot/dynamodb // check for new versions npm outdated // upgrade if necessary npm update @awspilot/dynamodb

    Install in Browser

    Please use rawgit CDN, to get the latest version paste the url:
    https://github.com/awspilot/dynamodb-oop/blob/master/dist/dynamodbjs.js

    <!DOCTYPE html> <html> <head> <script src="https://sdk.amazonaws.com/js/aws-sdk-2.247.1.min.js"></script> <script src="https://rawgit.com/awspilot/dynamodb-oop/master/dist/dynamodbjs.js"></script> </head> <body> </body> </html>


    Include in NodeJS

    var credentials = { "accessKeyId": "XXXXXXXXXXXXXXXX", "secretAccessKey": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ", "region": "eu-west-1" } const DynamodbFactory = require('@awspilot/dynamodb') var DynamoDB = new DynamodbFactory(credentials) // Alternatively, use an existing instance of AWS.DynamoDB. var AWS = require('aws-sdk'); var $db = new AWS.DynamoDB(); const DynamodbFactory = require('@awspilot/dynamodb') var DynamoDB = new DynamodbFactory($db);

    Include in Browser

    var credentials = { "accessKeyId": "XXXXXXXXXXXXXXXX", "secretAccessKey": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ", "region": "eu-west-1" } var dynamo = new AWS.DynamoDB( credentials ) // aws-sdk var DynamoDB = new window['@awspilot/dynamodb'](dynamo)

    Define table schema

    Some operations like insert (putItem) need to add KeyConditions
    to the request and prevent item replacing.

    Normally an insert operation would do an extra describeTable call
    By providing key definitions ( 1.2.6+ ), describeTable calls can be skipped.

    DynamoDB.schema([ { TableName: 'tbl_name', KeySchema: [ { AttributeName: "partition_key", KeyType: "HASH" }, { AttributeName: "sort_key", KeyType: "RANGE" } ] }, { TableName: 'users', KeySchema: [ { AttributeName: "email", KeyType: "HASH" } ] }, ])

    Response

    If a callback function is supplied, the response will be returned as callback(error, data)

    If no callback function is supplied, Promise will be returned

    DynamoDB .table() .method(parameters, function( err, response ) { })


    DynamoDB .table() .method(parameters) .then( callback_if_success, callback_if_failed ) .catch( callback_if_failed )

    Consumed Capacity

    TOTAL consumed capacity is returned by default

    DynamoDB .table($tableName) .operation(parameters, function callback() { console.log(this.ConsumedCapacity) }) // you can override it using DynamoDB .table($tableName) .return_consumed_capacity('INDEXES') // 'TOTAL' | 'INDEXES' | NONE .operation(parameters, function (err,data) { console.log(this.ConsumedCapacity) })


    Global error feed

    // every call to Amazon DynamoDB that fails will // call this function before the operation's callback DynamoDB.on('error', function( operation, error, payload ) { // you could use this to log call fails to LogWatch or // insert into SQS and process it later })

    Raw Calls to aws sdk


    DynamoDB.client.listTables(function(err, data) { console.log(data.TableNames); }); console.log( DynamoDB.client )