Code coverage report for dynamoDb-marshaler/index.js

Statements: 100% (17 / 17)      Branches: 100% (2 / 2)      Functions: 100% (6 / 6)      Lines: 100% (17 / 17)      Ignored: none     

All files » dynamoDb-marshaler/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80    1 1               1 2 6 4   2                   1 2 2                 1 1                   1 2                   1 1                         1            
'use strict';
 
var _ = require('lodash');
var marshalService = require('./lib/marshalService');
 
/**
 * Function decorator, validates item is a javascript object.
 *
 * @param callback
 * @returns {Function}
 */
var ensureItemIsObject = function(callback) {
  return function(item) {
    if (_.isPlainObject(item)) {
      return callback(item);
    }
    throw new TypeError('Item must be plain object literal');
  };
};
 
/**
 * Translates a javascript object into a format ready for DynamoDb.
 *
 * @param {Object} item Plain javascript object.
 * @returns {Object} The marshaled item dynamoDb compliant item.
 */
var marshalItem = ensureItemIsObject(function(item) {
  var marshaledItem = marshalService.marshal(item);
  return marshaledItem.M;
});
 
/**
 * Translates a JSON string into an object in DynamoDb format.
 *
 * @param {String} json A JSON representation of a javascript object.
 * @returns {Object} The marshaled DynamoDb compliant item.
 */
var marshalJson = function(json) {
  return marshalItem(JSON.parse(json));
};
 
/**
 * Translates a DynamoDb formatted object (a response from DynamoDb sdk) into
 * a plain javascript object with DynamoDb AttributeValue objects.
 *
 * @param {Object} item DynamoDb formatted object.
 * @returns {Object} A javascript object in normal form.
 */
var unmarshalItem = ensureItemIsObject(function(item) {
  return marshalService.unmarshal({M: item});
});
 
/**
 * Translates a DynamoDb formatted object, into a normally formatted object
 * represented as a JSON string.
 *
 * @param {Object} item DynamoDb formatted object.
 * @returns {String} JSON representation of a javascript object.
 */
var unmarshalJson = function(item) {
  return JSON.stringify(unmarshalItem(item));
};
 
/**
 * The dynamodb-marshaler
 *
 * @type {{
 *  marshalJson: Function,
 *  marshalItem: Function,
 *  unmarshalItem: Function,
 *  unmarshalJson: Function
 * }}
 */
module.exports = {
  marshalJson: marshalJson,
  marshalItem: marshalItem,
  unmarshalItem: unmarshalItem,
  unmarshalJson: unmarshalJson
};