From 992670d93da005b637bbc41d329aa5392455f7e2 Mon Sep 17 00:00:00 2001 From: jlmessenger Date: Mon, 14 Mar 2016 15:59:16 -0400 Subject: [PATCH] Add 'B' binary/buffer type. resolves #64 --- lib/ddb.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/ddb.js b/lib/ddb.js index 4cae9fc..363a268 100644 --- a/lib/ddb.js +++ b/lib/ddb.js @@ -757,9 +757,9 @@ var ddb = function(spec, my) { /** - * converts a string, string array, number or number array (scalar) - * JSON object to an amazon DynamoDB compatible JSON object - * @param json the JSON scalar object + * converts a string, string array, number, number array or Buffer (scalar) + * JS object to an amazon DynamoDB compatible JSON object + * @param json the JS scalar object * @throws an error if input object is not compatible * @return res the converted object */ @@ -785,13 +785,16 @@ var ddb = function(spec, my) { } return isSS ? {"SS": arr} : {"NS": arr}; } - throw new Error('Non Compatible Field [not string|number|string array|number array]: ' + value); + if (Buffer.isBuffer(value)) { + return { "B": value.toString('base64') }; + } + throw new Error('Non Compatible Field [not string|number|string array|number array|Buffer]: ' + value); } /** * converts a DynamoDB compatible JSON object into - * a native JSON object + * a native JS object * @param ddb the ddb JSON object * @throws an error if input object is not compatible * @return res the converted object @@ -812,6 +815,8 @@ var ddb = function(spec, my) { for(var j = 0; j < ddb[i]['NS'].length; j ++) { res[i][j] = parseFloat(ddb[i]['NS'][j]); } + } else if(ddb[i]['B']) { + res[i] = new Buffer(ddb[i]['B'], 'base64'); } else if(ddb[i]['L']) { res[i] = []; ddb[i]['L'].forEach(function(item) { @@ -822,7 +827,7 @@ var ddb = function(spec, my) { res[i] = objFromDDB(ddb[i]['M']); } else - throw new Error('Non Compatible Field [not "S"|"N"|"NS"|"SS"]: ' + i); + throw new Error('Non Compatible Field [not "S"|"N"|"NS"|"SS"|"B"]: ' + i); } } return res;