Skip to content

Commit

Permalink
Add 'B' binary/buffer type. resolves #64
Browse files Browse the repository at this point in the history
  • Loading branch information
jlmessenger committed Apr 28, 2016
1 parent 15998a7 commit 992670d
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/ddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 992670d

Please sign in to comment.