json-schema-dynamo
Advanced tools
Comparing version 0.4.1 to 0.5.0
32
index.js
@@ -8,3 +8,6 @@ var Validator = require('jsonschema').Validator | ||
}, | ||
N: function (value) { | ||
N: function (value, itemSchema) { | ||
if (itemSchema.type === 'date') { | ||
return new Date(+value) | ||
} | ||
return +value | ||
@@ -31,6 +34,7 @@ }, | ||
}, | ||
M: function (value) { | ||
return _.mapValues(value, function (value) { | ||
M: function (value, schema) { | ||
return _.mapValues(value, function (value, key) { | ||
var type = Object.keys(value)[0] | ||
return toModel[type](value[type]) | ||
var itemSchema = schema && schema.properties && schema.properties[key] | ||
return toModel[type](value[type], itemSchema) | ||
}) | ||
@@ -41,4 +45,6 @@ }, | ||
}, | ||
NS: function (value) { | ||
return value.map(toModel.N) | ||
NS: function (value, schema) { | ||
return value.map(function (num) { | ||
return toModel.N(num, schema.items) | ||
}) | ||
} | ||
@@ -48,3 +54,3 @@ } | ||
exports.fromDynamoItemToModel = function (schema, item) { | ||
var model = toModel.M(item) | ||
var model = toModel.M(item, schema) | ||
@@ -68,8 +74,16 @@ var v = new Validator() | ||
}, | ||
'boolean': function (schema, value) { | ||
boolean: function (schema, value) { | ||
return { BOOL: value } | ||
}, | ||
date: function (schema, value) { | ||
return { N: String(+value) } | ||
}, | ||
array: function (schema, value) { | ||
if (!Array.isArray(value)) { return null } | ||
var isDate = schema.items && schema.items.type === 'date' | ||
if (isDate) { | ||
return { NS: value.map(function(date) { return String(+date) })} | ||
} | ||
var isNum = (schema.items && ( | ||
@@ -108,2 +122,4 @@ schema.items.type === 'number' || schema.items.type === 'integer') | ||
return toItem.number(schema, value) | ||
case 'date': | ||
return toItem.date(schema, value) | ||
case 'boolean': | ||
@@ -110,0 +126,0 @@ return toItem['boolean'](schema, value) |
{ | ||
"name": "json-schema-dynamo", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Transform objects based on their JSON Schema definition back and forth between DynamoDB items", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,3 +13,3 @@ #json-schema-dynamo# | ||
createDate: { | ||
type: 'number' | ||
type: 'date' | ||
}, | ||
@@ -22,2 +22,5 @@ name: { | ||
}, | ||
likes: { | ||
type: 'number' | ||
}, | ||
types: { | ||
@@ -40,4 +43,5 @@ type: 'array', | ||
id: 'asdf', | ||
createDate: 1928383, | ||
createDate: new Date(), | ||
name: 'asdffdas', | ||
likes: 1, | ||
active: true, | ||
@@ -61,2 +65,5 @@ types: ['qwerty', 'ytrewq'], | ||
}, | ||
likes: { | ||
N: '1' | ||
}, | ||
active: { | ||
@@ -79,5 +86,6 @@ B: 'true' | ||
id: 'asdf', | ||
createDate: 1928383, | ||
createDate: <Date>, | ||
name: 'asdffdas', | ||
active: true, | ||
likes: 1 | ||
types: ['qwerty', 'ytrewq'], | ||
@@ -91,2 +99,4 @@ userIds: [1, 2, 3, 4, 5, 6, 7] | ||
There is also custom support for dates. You can define an attribute as a date type which will be transformed into an `N` in Dynamo and then back into a date when retrieved. | ||
Both transforms will also validate your model against your schema as well |
@@ -25,2 +25,5 @@ var assert = require('assert') | ||
}, | ||
date: { | ||
type: 'date' | ||
}, | ||
arrayString: { | ||
@@ -38,2 +41,8 @@ type: 'array', | ||
}, | ||
arrayDate: { | ||
type: 'array', | ||
items: { | ||
type: 'date' | ||
} | ||
}, | ||
arrayObject: { | ||
@@ -94,2 +103,12 @@ type: 'array', | ||
it('N that is a date', function () { | ||
var item = { | ||
date: { | ||
N: +Date.now() | ||
} | ||
} | ||
var model = transformer.fromDynamoItemToModel(schema, item) | ||
assert(+model.date === parseInt(item.date.N, 10)) | ||
}) | ||
it('BOOL', function () { | ||
@@ -130,2 +149,13 @@ var item = { | ||
it('NS of dates', function () { | ||
var item = { | ||
arrayDate: { | ||
NS: ['1441127720385', '1441127720385'] | ||
} | ||
} | ||
var model = transformer.fromDynamoItemToModel(schema, item) | ||
assert(String(+model.arrayDate[0]) === item.arrayDate.NS[0]) | ||
assert(String(+model.arrayDate[1]) === item.arrayDate.NS[1]) | ||
}) | ||
it('L', function () { | ||
@@ -132,0 +162,0 @@ var item = { |
@@ -117,2 +117,5 @@ var assert = require('assert') | ||
}, | ||
date: { | ||
type: 'date' | ||
}, | ||
arrayString: { | ||
@@ -130,2 +133,8 @@ type: 'array', | ||
}, | ||
arrayDate: { | ||
type: 'array', | ||
items: { | ||
type: 'date' | ||
} | ||
}, | ||
arrayObject: { | ||
@@ -186,2 +195,10 @@ type: 'array', | ||
it('dates', function () { | ||
var model = { | ||
date: new Date() | ||
} | ||
var item = transformer.fromModelToDynamoItem(schema, model) | ||
assert(item.date.N === String(+model.date)) | ||
}) | ||
it('array of strings', function () { | ||
@@ -205,2 +222,11 @@ var model = { | ||
it('array of dates', function () { | ||
var model = { | ||
arrayDate: [new Date(), new Date()] | ||
} | ||
var item = transformer.fromModelToDynamoItem(schema, model) | ||
assert(item.arrayDate.NS[0] === String(+model.arrayDate[0])) | ||
assert(item.arrayDate.NS[1] === String(+model.arrayDate[1])) | ||
}) | ||
it('array of objects', function () { | ||
@@ -207,0 +233,0 @@ var model = { |
17183
548
97