JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 15394
  • Score
    100M100P100Q175287F
  • License MIT

A utility for Amazon DynamoDb Data Types for AWS SDK for Node.js.

Package Exports

  • dynamodb-data-types

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dynamodb-data-types) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

DynamoDb-Data-Types

A utility for Amazon DynamoDB Data Types for AWS SDK for Node.js.

This utility is designed to be used in complement with the Amazon SDK for Node.js. It helps to represent Amazon DybamoDb data types.

How is it useful?

For example, following are some key-value pairs:

var data = { 
    name: 'Java Script',
    age: 18, 
    engines: [ 'Rhino', 'v8', 'SpiderMonkey', 'Carakan', 'JavaScriptCore' ]
}

In order to put the above data into DynamoDB, the AWS SDK requires it to be represented as:

{ 
    name: { S: 'Java Script' },
    age: { N: '18' },
    engines: { SS: [ 'Rhino','v8','SpiderMonkey','Carakan','JavaScriptCore' ] }
}

This utility helps to construct such representations required by the AWS SDK for Node.js

var attr = require('dynamodb-data-types').AttributeValue;
attr.wrap(data);

Features

The current version supports the following data types:

  • AttributeValue
  • AttributeValueUpdate

DynamoDB data types: docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Types.html

Quick Examples

var attr = require('dynamodb-data-types').AttributeValue;
var attrUpdate = require('dynamodb-data-types').AttributeValueUpdate;

var infoAttrValue = attr.wrap({ name: "Foo", age: 50 });
console.log(JSON.stringify(infoAttrValue));
// {"name":{"S":"Foo"},"age":{"N":"50"}}

var experience = {
  count: {"N": "4" },
  languages: { "SS": ["Java Script", "Ruby", "GLSL", "C" ] }
}
console.log(JSON.stringify(wrap(experience)));
// {"count":4,"languages":["Java Script","Ruby","GLSL","C"]}

More examples

Download

The source is available for download from github.com/kayomarz/dynamodb-data-types.

To install using Node Package Manager (npm):

npm install dynamodb-data-types

Untested feature

The current version of this library has not been tested with binary types B and BS. These will be done sometime soon.

PS: Please feel free to share any working code you might have to put/get binary data - it would help me complete these tests.

Documentation

AttributeValue

For more on AttributeValue see the AWS API Reference

AttributeValueUpdate

For more on AttributeValueUpdate see the AWS API Reference

AttributeValue

### wrap(item)

Wrap object properties into DynamoDB's AttributeValue data type.

Arguments

  • @param {Object} item The object to wrap.
  • @return {Object} A DynamoDb AttributeValue.

Example

var attr = require('dynamodb-data-types').AttributeValue;
attr.wrap({name: "Foo", age: 50});
// {"name":{"S":"Foo"},"age":{"N":"50"}}

unwrap(attributeValue)

Unwrap DynamoDB AttributeValues to values of the appropriate types.

Arguments

  • @param {Object} attributeValue The DynamoDb AttributeValue to unwrap.
  • @return {Object} Unwrapped object with properties.

Example

var attr = require('dynamodb-data-types').AttributeValue;
attr.unwrap({"name":{"S":"Foo"},"age":{"N":"50"}});
// {name: "Foo", age: 50}

wrap1(value)

Wrap a single value into DynamoDB's AttributeValue.

Arguments

  • @param {String|Number|Array}
  • @return {Object} DynamoDB AttributeValue.

Example

var attr = require('dynamodb-data-types').AttributeValue;
attr.wrap1(50);
// {"N":"50"}

unwrap1(attributeValue)

Unwrap a single DynamoDB's AttributeValue to a value of the appropriate javascript type.

Arguments

@param {Object} attributeValue The DynamoDB AttributeValue. @return {String|Number|Array} The javascript value.

Example

var attr = require('dynamodb-data-types').AttributeValue;
attr.unwrap1({"N":"50"});
// 50

AttributeValue

add(attrs)

Append attributes to be updated with action "ADD". This function can be chained with further calls to add', put' or `delete'.

Arguments

  • @param {Object} attrs Object with attributes to be updated.
  • @return {Updates} Object with all update attributes in the chain.

Example - put, add, delete.

put(attrs)

Append attributes to be updated with action "PUT". This function can be chained with further calls to add', put' or `delete'.

Arguments

  • @param {Object} attrs Object with attributes to be updated.
  • @return {Updates} Object with all update attributes in the chain.

Example - put, add, delete.

delete(attrs)

Append attributes to be updated with action "DELETE". This function can be chained with further calls to add', put' or `delete'.

Arguments

  • @param {Object|String|Array} attrs If this argument is an an Object,the Object's property values must be an array, containing elements to be removed, as required by DynamoDb SDK. If this argument is a String, it should contain comma seperated names of properties to be deleted. If its an Array, each array element should be a property name to be deleted.

  • @return {Updates} Object with all update attributes in the chain.

__Example - put, add, delete__
var attrUpdate = require('dynamodb-data-types').AttributeValueUpdate;

var dataUpdate = attrUpdate
    .put({name: "foo"})
    .add({age: 1})
    .delete("height, nickname")
    .add({favColors: ["red"]})
    .delete({favNumbers: [3]});

console.log(JSON.stringify(dataUpdate));
// {
//   "name": { "Action": "PUT", "Value": { "S": "foo" } },
//   "age": { "Action": "ADD", "Value": { "N": "1" } },
//   "height": { "Action": "DELETE" },
//   "nickname": { "Action": "DELETE" },
//   "favColors": { "Action": "ADD", "Value": { "SS": ["red" ] } },
//   "favNumbers": { "Action": "DELETE", "Value": { "NS": ["3"] } }
// }

The library does not perform checks.

It is upto the application to ensure that the application follows the SDK requirements. This utility does not perform any checks.

For example, DynamoDB attribute value NS is meant to represents a set of numbers. If, by mistake, the application creates the structure {"NS": [1, 3, "string"]}, this utility will not detect that the third element is an invalid element (string). Such checks are left to the application.

Platform

This utility is designed for node.js. For use in the browser, it will need to be adapted. This is in lieu of the recent (October 2013) Developer Preview - AWS SDK for JavaScript in the Browser

To adapt this utility for the browser, following are few todos. (This list is not exhaustive)

  • Possible use of lodash or underscore to ensure browser compatibility of functions which iterate over objects. Currently lib/util.js has a function to iterate over object properties which is sufficient for use with Node.js.

License

License