From 044d9657d517a5c394fb69c1c0d35e88e1b58293 Mon Sep 17 00:00:00 2001 From: Evangelos Vlachogiannis Date: Mon, 23 Nov 2015 18:46:41 +0100 Subject: [PATCH] feat(jpa): Add persistBunch --- lib/CriteriaQuery.js | 30 ++++++++++++++++++++++++++++++ lib/EntityManager.js | 20 +++++++++++++++++--- test/EntityManager.test.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/lib/CriteriaQuery.js b/lib/CriteriaQuery.js index b270339..93eec8d 100644 --- a/lib/CriteriaQuery.js +++ b/lib/CriteriaQuery.js @@ -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() + " ( "; diff --git a/lib/EntityManager.js b/lib/EntityManager.js index bfedf2e..bb61ffb 100644 --- a/lib/EntityManager.js +++ b/lib/EntityManager.js @@ -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; @@ -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)); diff --git a/test/EntityManager.test.js b/test/EntityManager.test.js index a18e425..a69a943 100644 --- a/test/EntityManager.test.js +++ b/test/EntityManager.test.js @@ -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(); + }); + }); }); \ No newline at end of file