noppoMan / npdynamodb

A Node.js Simple Query Builder and ORM for AWS DynamoDB
112 stars 19 forks source link

How to get multiple items at once? #23

Closed yanakend closed 9 years ago

yanakend commented 9 years ago

I'm using this driver recently, it's very nice. I wanna get multiple items using multiple primary keys at once. How can I get it?

Currently, I wrote like below..(of course slow)

Promise.all(_.map(items, function (item) {
  return npd().table('users').where("id", item.id).fetch();
}))
.then(function (getItems) {

});
noppoMan commented 9 years ago

Npdynamodb does not support batchGetItem recently but I'm considering new interface for it. And it may be included next release. So now, using raw dynamodb client through rawClient method is better.

npd().rawClient().batchGetItem(opts).then(function(result){
  console.log(result);
});

Considering syntax both of QueryBuilder and Orm are following.

// Querybuilder
npd().table('foo_table').whereIn('hash_key', [1, 2, 3]).then(function(result){
  console.log(result); // result is raw response from dynamodb.
});

// Orm
Foo.query(function(qb){
  qb.whereIn('hash_key', [1, 2, 3]);
}).fetch().then(function(fooCollection){
  fooCollection.each(function(fooModel){
    console.log(fooModel.get(name));
  });
});

Original batchGetItem operation can take items from multiple tables, but Npdynamodb does not take items from multiple tables cause it will be occurred impedance mismatch.

Thanks.

noppoMan commented 9 years ago

Hey, We've supported batchGetItem at version 0.2.7 on npm. The api doc is here. We are glad, If you like that :)