Skip to content

Commit

Permalink
Merge pull request #284 from oddnetworks/dynamodb-serializer
Browse files Browse the repository at this point in the history
Implement a new marshaler for DynamoDB
  • Loading branch information
kixxauth authored May 10, 2017
2 parents 235dd61 + 2de088b commit cb1a980
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 13 deletions.
151 changes: 139 additions & 12 deletions lib/stores/dynamodb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const Promise = require('bluebird');
const _ = require('lodash');
const uuid = require('node-uuid');

const dynamodbMarshaler = require('dynamodb-marshaler');
// Namespace for helpers (see below).
const DynamoDB = {};
const hasOwn = Object.prototype.hasOwnProperty;

// bus - Oddcast Bus Object
// options.types - Array of String types *required*
Expand Down Expand Up @@ -41,13 +43,13 @@ module.exports = function (bus, options) {
if (_.isFunction(dynamodb.putItemAsync)) {
putItemAsync = object => {
return dynamodb
.putItemAsync({TableName: object.type, Item: dynamodbMarshaler.marshalItem(object)})
.putItemAsync({TableName: object.type, Item: DynamoDB.serializeItem(object)})
.then(_.constant(object));
};
} else {
putItemAsync = object => {
return new Promise((resolve, reject) => {
dynamodb.putItem({TableName: object.type, Item: dynamodbMarshaler.marshalItem(object)}, err => {
dynamodb.putItem({TableName: object.type, Item: DynamoDB.serializeItem(object)}, err => {
if (err) {
return reject(err);
}
Expand All @@ -68,20 +70,20 @@ module.exports = function (bus, options) {
getItemAsync = object => {
const key = (object.channel) ? {id: object.id, channel: object.channel} : {id: object.id};
return dynamodb
.getItemAsync({TableName: object.type, Key: dynamodbMarshaler.marshalItem(key)})
.getItemAsync({TableName: object.type, Key: DynamoDB.serializeKey(key)})
.then(res => {
return (res && res.Item) ? dynamodbMarshaler.unmarshalItem(res.Item) : null;
return (res && res.Item) ? DynamoDB.deserializeItem(res.Item) : null;
});
};
} else {
getItemAsync = object => {
return new Promise((resolve, reject) => {
const key = (object.channel) ? {id: object.id, channel: object.channel} : {id: object.id};
dynamodb.getItem({TableName: object.type, Key: dynamodbMarshaler.marshalItem(key)}, (err, res) => {
dynamodb.getItem({TableName: object.type, Key: DynamoDB.serializeKey(key)}, (err, res) => {
if (err) {
return reject(err);
}
resolve(res && res.Item ? dynamodbMarshaler.unmarshalItem(res.Item) : null);
resolve(res && res.Item ? DynamoDB.deserializeItem(res.Item) : null);
});
});
};
Expand All @@ -98,7 +100,7 @@ module.exports = function (bus, options) {
deleteItemAsync = object => {
const key = (object.channel) ? {id: object.id, channel: object.channel} : {id: object.id};
return dynamodb
.deleteItemAsync({TableName: object.type, Key: dynamodbMarshaler.marshalItem(key)})
.deleteItemAsync({TableName: object.type, Key: DynamoDB.serializeKey(key)})
.then(res => {
return Boolean(res);
});
Expand All @@ -107,7 +109,7 @@ module.exports = function (bus, options) {
deleteItemAsync = object => {
return new Promise((resolve, reject) => {
const key = (object.channel) ? {id: object.id, channel: object.channel} : {id: object.id};
dynamodb.deleteItem({TableName: object.type, Key: dynamodbMarshaler.marshalItem(key)}, (err, res) => {
dynamodb.deleteItem({TableName: object.type, Key: DynamoDB.serializeKey(key)}, (err, res) => {
if (err) {
return reject(err);
}
Expand Down Expand Up @@ -156,7 +158,7 @@ module.exports = function (bus, options) {
if (err) {
return reject(err);
}
resolve(res.Items.map(dynamodbMarshaler.unmarshalItem));
resolve(res.Items.map(DynamoDB.deserializeItem));
});
});

Expand All @@ -173,7 +175,7 @@ module.exports = function (bus, options) {
if (err) {
return reject(err);
}
resolve(res.Items.map(dynamodbMarshaler.unmarshalItem));
resolve(res.Items.map(DynamoDB.deserializeItem));
});
});
}
Expand Down Expand Up @@ -238,7 +240,7 @@ module.exports = function (bus, options) {
return Promise.reject(new Error('args.include must be an array.'));
}

const relationships = entity.relationships || Object.create(null);
const relationships = entity.relationships || {};

const commands = _
.flatten(include.map(key => {
Expand Down Expand Up @@ -456,3 +458,128 @@ module.exports = function (bus, options) {

return Promise.resolve(store);
};

function serializeItem(attrs) {
return Object.keys(attrs).reduce((rec, key) => {
const val = DynamoDB.typeCast(attrs[key]);
if (val) {
rec[key] = val;
}
return rec;
}, {});
}

DynamoDB.serializeItem = serializeItem;

function deserializeItem(obj) {
return Object.keys(obj).reduce((rv, key) => {
rv[key] = DynamoDB.deserializeAttribute(obj[key]);
return rv;
}, {});
}

DynamoDB.deserializeItem = deserializeItem;

function deserializeAttribute(val) {
if (hasOwn.call(val, 'S')) {
return val.S.toString();
} else if (hasOwn.call(val, 'N')) {
return parseFloat(val.N);
} else if (val.SS || val.NS) {
return val.SS || val.NS;
} else if (hasOwn.call(val, 'BOOL')) {
return Boolean(val.BOOL);
} else if (hasOwn.call(val, 'M')) {
return DynamoDB.deserializeItem(val.M);
} else if (hasOwn.call(val, 'L')) {
return val.L.map(DynamoDB.deserializeAttribute);
} else if (hasOwn.call(val, 'NULL')) {
return null;
}
}

DynamoDB.deserializeAttribute = deserializeAttribute;

function typeCast(obj) {
switch (typeof obj) {
case 'string':
if (obj.length === 0) {
return null;
}
return {S: obj};
case 'number':
if (isNaN(obj)) {
return null;
}
return {N: obj.toString()};
case 'boolean':
return {BOOL: obj};
case 'function':
case 'undefined':
return null;
default:
if (!obj) {
return {NULL: true};
}
return Array.isArray(obj) ? DynamoDB.typeCastArray(obj) : DynamoDB.typeCastObject(obj);
}
}

DynamoDB.typeCast = typeCast;

function typeCastArray(obj) {
return {L: obj.map(DynamoDB.typeCast).filter(item => {
return Boolean(item);
})};
}

DynamoDB.typeCastArray = typeCastArray;

function typeCastObject(obj) {
const keys = Object.keys(obj);
const rv = {M: {}};

if (keys.length === 0) {
return rv;
}

rv.M = keys.reduce((M, key) => {
const val = DynamoDB.typeCast(obj[key]);
if (val) {
M[key] = val;
}
return M;
}, rv.M);

return rv;
}

DynamoDB.typeCastObject = typeCastObject;

function serializeKey(obj) {
return Object.keys(obj).reduce((keys, key) => {
keys[key] = DynamoDB.typeCastKey(obj[key]);
return keys;
}, {});
}

DynamoDB.serializeKey = serializeKey;

function typeCastKey(obj) {
const type = typeof obj;

switch (type) {
case 'string':
return {S: obj};
case 'number':
return {N: obj.toString()};
case 'boolean':
return {BOOL: obj};
default:
throw new TypeError(
`Only String, Number or Boolean attributes (not ${type}) may be defined on keys in DynamoDB Engine.`
);
}
}

DynamoDB.typeCastKey = typeCastKey;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"body-parser": "~1.15.0",
"boom": "~4.2.0",
"debug": "~2.2.0",
"dynamodb-marshaler": "~2.0.0",
"express": "~4.14.0",
"header-parser": "~1.0.0",
"jsonwebtoken": "~7.1.0",
Expand Down

0 comments on commit cb1a980

Please sign in to comment.