express-cassandra
Advanced tools
Comparing version 0.4.12 to 0.5.0
@@ -88,2 +88,17 @@ var fs = require('fs'); | ||
doBatch: function(queries, callback) { | ||
var randomModel = this.modelInstance[Object.keys(this.modelInstance)[0]]; | ||
var builtQueries = []; | ||
for(var i=0;i<queries.length;i++) { | ||
builtQueries.push({ | ||
query: queries[i], | ||
params: [] | ||
}); | ||
} | ||
randomModel.execute_batch(builtQueries, function(err){ | ||
if(err) callback(err); | ||
else callback(); | ||
}); | ||
}, | ||
get instance() { | ||
@@ -90,0 +105,0 @@ return this.modelInstance; |
@@ -819,4 +819,4 @@ var util = require('util'), | ||
} | ||
if(!callback) | ||
throw 'Callback needed!'; | ||
if(typeof callback != 'function') | ||
throw 'no valid callback function was provided'; | ||
@@ -869,4 +869,4 @@ var defaults = { | ||
} | ||
if(!callback) | ||
throw 'Callback needed!'; | ||
if(typeof callback != 'function') | ||
throw 'no valid callback function was provided'; | ||
@@ -890,8 +890,6 @@ this.find(query_ob, options, function(err, results){ | ||
BaseModel.update = function(query_ob, update_values, options, callback){ | ||
if(arguments.length == 3){ | ||
if(arguments.length == 3 && typeof options == 'function'){ | ||
callback = options; | ||
options = {}; | ||
} | ||
if(!callback) | ||
throw 'Callback needed!'; | ||
@@ -910,3 +908,8 @@ var defaults = { | ||
catch(e){ | ||
return callback(build_error('model.update.invalidvalue',update_values[key],key)); | ||
if(typeof callback == 'function') { | ||
return callback(build_error('model.update.invalidvalue',update_values[key],key)); | ||
} | ||
else { | ||
throw build_error('model.update.invalidvalue',update_values[key],key); | ||
} | ||
} | ||
@@ -923,3 +926,6 @@ } | ||
catch(e){ | ||
return callback(e); | ||
if(typeof callback == 'function') { | ||
return callback(e); | ||
} | ||
else throw e; | ||
} | ||
@@ -935,3 +941,8 @@ query = util.format(query, this._properties.table_name, update_clause_array.join(', '), where); | ||
catch(e){ | ||
return callback(build_error('model.update.invalidvalue',options.conditions[key],key)); | ||
if(typeof callback == 'function') { | ||
return callback(build_error('model.update.invalidvalue',options.conditions[key],key)); | ||
} | ||
else { | ||
throw build_error('model.update.invalidvalue',options.conditions[key],key); | ||
} | ||
} | ||
@@ -945,5 +956,12 @@ } | ||
if(options.return_query) return query; | ||
this._execute_table_query(query, null, {prepare: options.prepare}, function(err,results){ | ||
if(err) return callback(build_error('model.update.dberror',err)); | ||
callback(null, results); | ||
if(typeof callback == 'function') { | ||
if(err) return callback(build_error('model.update.dberror',err)); | ||
callback(null, results); | ||
} | ||
else if(err) { | ||
throw build_error('model.update.dberror',err); | ||
} | ||
}); | ||
@@ -960,8 +978,6 @@ | ||
BaseModel.delete = function(query_ob, options, callback){ | ||
if(arguments.length == 2){ | ||
if(arguments.length == 2 && typeof options == 'function'){ | ||
callback = options; | ||
options = {}; | ||
} | ||
if(!callback) | ||
throw 'Callback needed!'; | ||
@@ -980,8 +996,19 @@ var defaults = { | ||
catch(e){ | ||
return callback(e); | ||
if(typeof callback == 'function') { | ||
return callback(e); | ||
} | ||
else throw e; | ||
} | ||
query = util.format(query, this._properties.table_name, where); | ||
if(options.return_query) return query; | ||
this._execute_table_query(query, null, {prepare: options.prepare}, function(err,results){ | ||
if(err) return callback(build_error('model.delete.dberror',err)); | ||
callback(null, results); | ||
if(typeof callback == 'function') { | ||
if(err) return callback(build_error('model.delete.dberror',err)); | ||
callback(null, results); | ||
} | ||
else if(err) { | ||
throw build_error('model.delete.dberror',err); | ||
} | ||
}); | ||
@@ -1057,3 +1084,3 @@ | ||
BaseModel.prototype.save = function(options, callback){ | ||
if(arguments.length == 1){ | ||
if(arguments.length == 1 && typeof options == 'function'){ | ||
callback = options; | ||
@@ -1083,9 +1110,20 @@ options = {}; | ||
if(fieldvalue === undefined){ | ||
if(schema.key.indexOf(f) >= 0 || schema.key[0].indexOf(f) >= 0) | ||
return callback(build_error('model.save.unsetkey',f)); | ||
if(schema.key.indexOf(f) >= 0 || schema.key[0].indexOf(f) >= 0) { | ||
if(typeof callback == 'function') { | ||
return callback(build_error('model.save.unsetkey',f)); | ||
} | ||
else { | ||
throw build_error('model.save.unsetkey',f); | ||
} | ||
} | ||
else | ||
continue; | ||
} else if(!schema.fields[f].rule || !schema.fields[f].rule.ignore_default){ //did set a default value, ignore default is not set | ||
} else if(!schema.fields[f].rule || !schema.fields[f].rule.ignore_default) { //did set a default value, ignore default is not set | ||
if( this.validate( f, fieldvalue ) !== true ){ | ||
return callback(build_error('model.save.invaliddefaultvalue',fieldvalue,f,fieldtype)); | ||
if(typeof callback == 'function') { | ||
return callback(build_error('model.save.invaliddefaultvalue',fieldvalue,f,fieldtype)); | ||
} | ||
else { | ||
throw build_error('model.save.invaliddefaultvalue',fieldvalue,f,fieldtype); | ||
} | ||
} | ||
@@ -1096,4 +1134,10 @@ } | ||
if(fieldvalue === null){ | ||
if(schema.key.indexOf(f) >= 0 || schema.key[0].indexOf(f) >= 0) | ||
return callback(build_error('model.save.unsetkey',f)); | ||
if(schema.key.indexOf(f) >= 0 || schema.key[0].indexOf(f) >= 0) { | ||
if(typeof callback == 'function') { | ||
return callback(build_error('model.save.unsetkey',f)); | ||
} | ||
else { | ||
throw build_error('model.save.unsetkey',f); | ||
} | ||
} | ||
} | ||
@@ -1107,3 +1151,8 @@ | ||
catch(e){ | ||
return callback(build_error('model.save.invalidvalue',fieldvalue,f,fieldtype)); | ||
if(typeof callback == 'function') { | ||
return callback(build_error('model.save.invalidvalue',fieldvalue,f,fieldtype)); | ||
} | ||
else { | ||
throw build_error('model.save.invalidvalue',fieldvalue,f,fieldtype); | ||
} | ||
} | ||
@@ -1124,5 +1173,12 @@ } | ||
this.constructor._execute_table_query(query, null, {prepare: options.prepare}, function(err, result){ | ||
if(err) return callback(build_error('model.save.dberror',err)); | ||
callback(null, result); | ||
if(options.return_query) return query; | ||
this.constructor._execute_table_query(query, null, {prepare: options.prepare}, function(err, result) { | ||
if(typeof callback == 'function') { | ||
if(err) return callback(build_error('model.save.dberror',err)); | ||
callback(null, result); | ||
} | ||
else if(err) { | ||
throw build_error('model.save.dberror',err); | ||
} | ||
}); | ||
@@ -1137,7 +1193,7 @@ }; | ||
BaseModel.prototype.delete = function(options, callback){ | ||
if(arguments.length == 1){ | ||
if(arguments.length == 1 && typeof options == 'function'){ | ||
callback = options; | ||
options = {}; | ||
} | ||
var schema = this.constructor._properties.schema; | ||
@@ -1151,4 +1207,4 @@ var delete_query = {}; | ||
} | ||
this.constructor.delete(delete_query, options, callback); | ||
return this.constructor.delete(delete_query, options, callback); | ||
}; | ||
@@ -1155,0 +1211,0 @@ |
{ | ||
"name": "express-cassandra", | ||
"version": "0.4.12", | ||
"version": "0.5.0", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "async": "^1.0.0", |
@@ -571,4 +571,36 @@ [![Build Status](https://travis-ci.org/masumsoft/express-cassandra.svg)](https://travis-ci.org/masumsoft/express-cassandra) | ||
## Batch Query | ||
## Batching ORM Operations | ||
You can batch any number of save, update and delete operations using the `models.doBatch` function. To use those functions a a batch operation, you need to tell each of the save/update/delete functions that you want to get the final built query from the orm instead of executing it immediately. You can do that by adding a `return_query` parameter in the options object of the corresponding function and build an array of batch operations like the following: | ||
```js | ||
var queries = []; | ||
var event = new models.instance.Event({ | ||
id: 3, | ||
body: 'hello3' | ||
}); | ||
var save_query = event.save({return_query: true}); | ||
queries.push(save_query); | ||
var update_query = models.instance.Event.update( | ||
{id: 1}, | ||
{body: 'hello1 updated'}, | ||
{return_query: true} | ||
); | ||
queries.push(update_query); | ||
var delete_query = models.instance.Event.delete( | ||
{id: 2}, | ||
{return_query: true} | ||
); | ||
queries.push(delete_query); | ||
models.doBatch(queries, function(err){ | ||
if(err) throw err; | ||
}); | ||
``` | ||
## Raw Batch Query | ||
You can get the batch query interface from cassandra nodejs-driver using the `execute_batch` method. | ||
@@ -575,0 +607,0 @@ |
@@ -288,3 +288,3 @@ var models = require('../index'); | ||
describe('#batch queries',function(){ | ||
describe('#raw batch queries',function(){ | ||
it('should insert data properly', function(done) { | ||
@@ -298,3 +298,3 @@ var queries = [ | ||
query: "INSERT INTO event (email, id, body) VALUES (?, ?, ?)", | ||
params: ['hello2@h.com', models.timeuuid(), 'hello2'] | ||
params: ['hello2@h.com', event_id, 'hello2'] | ||
} | ||
@@ -314,3 +314,3 @@ ]; | ||
describe('#find after batch events',function(){ | ||
describe('#find after raw batch events',function(){ | ||
it('should find the event with timeuuid in query', function(done) { | ||
@@ -328,2 +328,45 @@ models.instance.Event.findOne({email: 'hello1@h.com', id: event_id}, function(err, event){ | ||
describe('#orm batch queries',function(){ | ||
it('should save, update and delete data properly', function(done) { | ||
var queries = []; | ||
var event = new models.instance.Event({ | ||
email: 'hello3@h.com', | ||
id: event_id, | ||
body: 'hello3' | ||
}); | ||
queries.push(event.save({return_query: true})); | ||
queries.push(models.instance.Event.update({email: 'hello1@h.com', id: event_id}, {body:'hello1 updated'}, {return_query: true})); | ||
queries.push(models.instance.Event.delete({email: 'hello2@h.com', id: event_id}, {return_query: true})); | ||
models.doBatch(queries, function(err){ | ||
if(err) throw err; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('#find after orm batch events',function(){ | ||
it('should find updated events', function(done) { | ||
models.instance.Event.find({'$limit':10}, function(err, events){ | ||
if(err) throw err; | ||
events.length.should.equal(2); | ||
events[0].body.should.equal('hello1 updated'); | ||
events[1].body.should.equal('hello3'); | ||
var queries = []; | ||
for(var i=0;i<events.length;i++) { | ||
queries.push(events[i].delete({return_query: true})); | ||
} | ||
models.doBatch(queries, function(err){ | ||
if(err) throw err; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('#close cassandra connection',function(){ | ||
@@ -330,0 +373,0 @@ it('should close connection to cassandra without errors', function(done) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
117606
2197
656