Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'B' binary/buffer type #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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