Skip to content
This repository has been archived by the owner on Oct 25, 2018. It is now read-only.

Commit

Permalink
feat(jpa): Add persistBunch
Browse files Browse the repository at this point in the history
  • Loading branch information
evlach committed Nov 23, 2015
1 parent 07277ef commit 044d965
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
30 changes: 30 additions & 0 deletions lib/CriteriaQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,36 @@ module.exports = class CriteriaQuery {
return (queryObject);
}

insertOrUpdate(row)
{
let params = [];
let keys = [];
let qq = [];
for (let key of this.metaModel.fields.keys())
{
if (row.hasOwnProperty(key))
{
var value = row[key];
qq.push("?");
keys.push(key);
params.push(value);
}
}
let paramsKeys = JSON.stringify(keys).toString().substring(1);
paramsKeys = " (" + paramsKeys.substring(0, paramsKeys.length - 1) + ") ";
let paramsQ = JSON.stringify(qq).toString().replace(/"/g, '').substring(1);
paramsQ = " (" + paramsQ.substring(0, paramsQ.length - 1) + ") ";
let ttlQ = "";
if(this.metaModel.ttl){
ttlQ = " USING TTL " + this.metaModel.ttl + " ";
}
let queryObject = {
query: "INSERT INTO " + this.getFullTableName() + paramsKeys + " VALUES " + paramsQ + ttlQ + ";",
params: params
};
return (queryObject);
}

create()
{
let q = "CREATE TABLE IF NOT EXISTS " + this.getFullTableName() + " ( ";
Expand Down
20 changes: 17 additions & 3 deletions lib/EntityManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ module.exports = class EntityManager {
});
}

persistBunch(entities, callback, metaModel)
{
this.metaModel = metaModel || this.metaModel;
let queryObjects = [];
entities.forEach((entity)=>
{
let row = this.metaModel.toRow(entity);
let q = this.getCriteriaQuery().insertOrUpdate(row);
queryObjects.push(q);
});
this.queryBatch(queryObjects, callback);
}

updateByCriteria(entity, callback, criteriaQuery, metaModel)
{
this.metaModel = metaModel || this.metaModel;
Expand Down Expand Up @@ -126,18 +139,19 @@ module.exports = class EntityManager {

queryBatch(queryObjects, callback)
{
let self = this;
const MAX_QUERY_LENGTH = 16;
let splicedQ = [];
while (queryObjects.length)
{
splicedQ.push(queryObjects.splice(0, MAX_QUERY_LENGTH));
}
async.eachSeries(splicedQ, function (query, cb)
async.eachSeries(splicedQ, function (queryObjectsPart, cb)
{
self.client.batch(query, self.client.options.queryOptions,
self.client.batch(queryObjectsPart, self.client.options.queryOptions,
function (error, result)
{
self.logQueryObject(queryObject);
self.logQueryObject(queryObjectsPart);
if (error)
{
debug(JSON.stringify(error, null, 0));
Expand Down
33 changes: 33 additions & 0 deletions test/EntityManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,37 @@ describe("cassandra-jpa::EntityManager", function ()
return done();
});
});
it("should persist All Foo as bunch", function (done)
{
async.series([function (callback)
{
let foos = [];
for (let i = 0; i < 300; i++)
{
let f = new m.tests.Foo({
name: "manyBunchFoos"
});
foos.push(f);
}
entityManager.persistBunch(foos, function (error, res)
{
assert.equal(error, null);
return callback(error, res);
});
}, function (callback)
{
let op = cb.equal("name", "manyBunchFoos");
let criteriaQuery = cq.where(cb.and([op]));
entityManager.findAll(function (error, res)
{
assert.equal(error, null);
assert.equal(res.length, 300);
return callback(error, res);
}, criteriaQuery);
}], function (err, results)
{
assert.equal(err, null);
return done();
});
});
});

0 comments on commit 044d965

Please sign in to comment.