Skip to content

Commit

Permalink
Add .putItems call. Refs #6
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Jul 30, 2014
1 parent 404033c commit 4ee37d7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -8,5 +8,6 @@ module.exports = function(c) {
_(dyno).extend(require('./lib/query'));
_(dyno).extend(require('./lib/scan'));
_(dyno).extend(require('./lib/table'));
_(dyno).extend(require('./lib/batch'));
return dyno;
}
19 changes: 19 additions & 0 deletions lib/batch.js
@@ -0,0 +1,19 @@
var util = require('util');
var _ = require('underscore');
var items = module.exports = {};
var types = require('./types');
var config = require('./config')();

items.putItems = function(items, opts, cb) {
if(!cb) { cb = opts; opts = {}; }
var table = opts.table || config.table;
var params = { RequestItems: {} };
params.RequestItems[table] = items.map(function(item) {
return {
PutRequest: {
Item: types.toDynamoTypes(item)
}
};
});
config.dynamo.batchWriteItem(params, cb);
};
28 changes: 14 additions & 14 deletions test/fixtures.js
@@ -1,15 +1,15 @@
module.exports.test = {
'TableName': 'test',
'AttributeDefinitions': [
{'AttributeName': 'id', 'AttributeType': 'S'},
{'AttributeName': 'range', 'AttributeType': 'N'}
],
'KeySchema': [
{'AttributeName': 'id', 'KeyType': 'HASH'},
{'AttributeName': 'range', 'KeyType': 'RANGE'}
],
'ProvisionedThroughput': {
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
};
'TableName': 'test',
'AttributeDefinitions': [
{'AttributeName': 'id', 'AttributeType': 'S'},
{'AttributeName': 'range', 'AttributeType': 'N'}
],
'KeySchema': [
{'AttributeName': 'id', 'KeyType': 'HASH'},
{'AttributeName': 'range', 'KeyType': 'RANGE'}
],
'ProvisionedThroughput': {
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
};
38 changes: 38 additions & 0 deletions test/test.batch.js
@@ -0,0 +1,38 @@
var test = require('tap').test;
var s = require('./setup');
var es = require('event-stream');
var dyno = s.dyno;

test('setup', s.setup());
test('setup table', s.setupTable);
test('putItems', function(t) {
var items = randomItems(10);

dyno.putItems(items, itemResp);
function itemResp(err, resp) {
t.equal(err, null);
t.deepEqual(resp, {
UnprocessedItems: {}
});
t.end();
// dyno.getItem(item, getItem);
}
function getItem(err, resp) {
// t.equal(err, null);
// t.deepEqual(resp, {Item:{id: 'yo', range: 5}});
// t.end();
}
});
test('teardown', s.teardown);

function randomItems(n) {
var items = [];
for (var i = 0; i < n; i++) {
items.push({
id: 'id:' + i.toString(),
range: i,
data: (new Array(100)).join(' '),
});
}
return items;
}

0 comments on commit 4ee37d7

Please sign in to comment.