express-cassandra
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -39,3 +39,8 @@ var TYPE_MAP = require('./cassandra_types'), | ||
}else{ | ||
output_schema.fields[k] = {'type':output_schema.fields[k].type}; | ||
if(output_schema.fields[k].typeDef) { | ||
output_schema.fields[k] = {'type':output_schema.fields[k].type, 'typeDef':output_schema.fields[k].typeDef}; | ||
} | ||
else { | ||
output_schema.fields[k] = {'type':output_schema.fields[k].type}; | ||
} | ||
} | ||
@@ -47,2 +52,11 @@ } | ||
} | ||
if(output_schema.fields[k] && ['map','list','set'].indexOf(output_schema.fields[k].type) > -1) { | ||
if(model_schema.typeMaps && model_schema.typeMaps[k]) { | ||
output_schema.fields[k].typeDef = "<" + model_schema.typeMaps[k].toString() + ">"; | ||
} | ||
else { | ||
output_schema.fields[k].typeDef = "<" + output_schema.fields[k].typeDef.replace(/[\s\<\>]/g,'').replace(/varchar/g,'text').split(',').toString() + ">"; | ||
} | ||
} | ||
} | ||
@@ -151,3 +165,4 @@ | ||
if(typeof model_schema.fields[fieldname].default == 'object' && !(model_schema.fields[fieldname].default['$db_function'])){ | ||
return false; | ||
if(['map','list','set'].indexOf(model_schema.fields[fieldname].type) > -1) return true; | ||
else return false; | ||
} | ||
@@ -163,2 +178,2 @@ else | ||
module.exports = schemer; | ||
module.exports = schemer; |
@@ -385,6 +385,11 @@ var util = require('util'), | ||
var db_schema = {fields:{}}; | ||
var db_schema = {fields:{},typeMaps:{}}; | ||
for(var r in result.rows){ | ||
var row = result.rows[r]; | ||
db_schema.fields[row.column_name] = TYPE_MAP.find_type_by_dbvalidator(row.validator); | ||
if(['map','list','set'].indexOf(db_schema.fields[row.column_name]) > -1) { | ||
db_schema.typeMaps[row.column_name] = TYPE_MAP.find_typeMap_by_dbvalidator(row.validator); | ||
} | ||
if(row.type == 'partition_key'){ | ||
@@ -508,3 +513,3 @@ if(!db_schema.key) | ||
return ("\'" + fieldvalue.toISOString().replace(/\..+/, '') + "\'"); | ||
return ("\'" + fieldvalue.toISOString().slice(0,-5).replace('T',' ') + '+0000' + "\'"); | ||
case 'blob': | ||
@@ -521,3 +526,3 @@ return util.format("textAsBlob('%s')",fieldvalue.toString()); | ||
if(typedef[0].trim().toLowerCase()=='timestamp' && (new Date(key)!='Invalid Date')) { | ||
retvalmap += "'"+(new Date(key).getTime())+"':'"+fieldvalue[key].replace(/'/g, "''")+"',"; | ||
retvalmap += "'"+(new Date(key).toISOString().slice(0,-5).replace('T',' ') + '+0000')+"':'"+fieldvalue[key].replace(/'/g, "''")+"',"; | ||
} | ||
@@ -529,3 +534,3 @@ else { | ||
else if(fieldvalue[key] instanceof Date) { | ||
retvalmap += "'"+key+"':'"+fieldvalue[key].toISOString().replace(/\..+/, '')+"',"; | ||
retvalmap += "'"+key+"':'"+ fieldvalue[key].toISOString().slice(0,-5).replace('T',' ') + '+0000' +"',"; | ||
} | ||
@@ -547,3 +552,3 @@ else { | ||
else if(fieldvalue[key] instanceof Date) { | ||
retvallist += "'"+fieldvalue[key].toISOString().replace(/\..+/, '')+"',"; | ||
retvallist += "'"+ fieldvalue[key].toISOString().slice(0,-5).replace('T',' ') + '+0000' +"',"; | ||
} | ||
@@ -565,3 +570,3 @@ else { | ||
else if(fieldvalue[key] instanceof Date) { | ||
retvallist += "'"+fieldvalue[key].toISOString().replace(/\..+/, '')+"',"; | ||
retvallist += "'"+ fieldvalue[key].toISOString().slice(0,-5).replace('T',' ') + '+0000' +"',"; | ||
} | ||
@@ -577,4 +582,4 @@ else { | ||
case 'counter': | ||
var retvalcounter = fieldname; | ||
if(fieldvalue > 0) { | ||
var retvalcounter = '"' + fieldname + '"'; | ||
if(fieldvalue >= 0) { | ||
retvalcounter += " + " + fieldvalue; | ||
@@ -581,0 +586,0 @@ } |
@@ -108,2 +108,30 @@ var check = require('check-types'), | ||
TYPE_MAP.find_typeMap_by_dbvalidator = function(val){ | ||
//decompose composite types | ||
var decomposed = val ? val.split(/[(,)]/) : ['']; | ||
var typeMaps = []; | ||
if(decomposed.length > 1) { | ||
for(var t in this){ | ||
if (this[t].dbvalidator == decomposed[1]) { | ||
typeMaps.push(t); | ||
break; | ||
} | ||
} | ||
} | ||
if(decomposed.length > 2) { | ||
for(var t in this){ | ||
if (this[t].dbvalidator == decomposed[2]) { | ||
typeMaps.push(t); | ||
break; | ||
} | ||
} | ||
} | ||
return typeMaps; | ||
}; | ||
module.exports = TYPE_MAP; |
{ | ||
"name": "express-cassandra", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "async": "^1.0.0", |
@@ -573,3 +573,3 @@ [![Build Status](https://travis-ci.org/masumsoft/express-cassandra.svg)](https://travis-ci.org/masumsoft/express-cassandra) | ||
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: | ||
You can batch any number of save, update and delete operations using the `models.doBatch` function. To use more than one of those functions as a combined 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 operations to execute atomically like the following: | ||
@@ -576,0 +576,0 @@ ```js |
module.exports = { | ||
"fields": { | ||
"user_id": "bigint", | ||
"visit_count": "counter" | ||
"visit_count": "counter", | ||
"visitCount": "counter" | ||
}, | ||
"key" : ["user_id"] | ||
} |
@@ -8,3 +8,3 @@ module.exports = { | ||
"surname": { "type": "varchar", "default": "no surname provided"}, | ||
"completeName": { "type": "varchar", "default": function(){ return this.Name + ' ' + this.surname;}}, | ||
"completeName": { "type": "varchar", "default": function(){return this.Name + (this.surname ? (' ' + this.surname) : '');}}, | ||
"age": { | ||
@@ -39,2 +39,10 @@ "type": "int", | ||
}, | ||
"intMapDefault": { | ||
type: "map", | ||
typeDef: "<text, int>", | ||
default: { | ||
'one': 1, | ||
'two': 2 | ||
} | ||
}, | ||
"stringMap": { | ||
@@ -56,2 +64,7 @@ type: "map", | ||
}, | ||
"stringListDefault": { | ||
type: "list", | ||
typeDef: "<text>", | ||
default: ['one','two'] | ||
}, | ||
"timeSet": { | ||
@@ -65,2 +78,7 @@ type: "set", | ||
}, | ||
"intSetDefault": { | ||
type: "set", | ||
typeDef: "<int>", | ||
default: [1,2] | ||
}, | ||
"stringSet": { | ||
@@ -67,0 +85,0 @@ type: "set", |
@@ -45,3 +45,2 @@ var models = require('../index'); | ||
age:-32, | ||
uniId: models.uuid(), | ||
timeId: models.timeuuid(), | ||
@@ -82,2 +81,4 @@ info:{'hello':'world'}, | ||
people[0].Name.should.equal('Mahafuzur'); | ||
people[0].surname.should.equal('no surname provided'); | ||
people[0].completeName.should.equal('Mahafuzur'); | ||
people[0].info.hello.should.equal('world'); | ||
@@ -105,2 +106,6 @@ people[0].phones[1].should.equal('234567'); | ||
should.exist(people[0]._validators); | ||
//test composite defaults | ||
people[0].intMapDefault.should.deep.equal({"one": 1, "two": 2}); | ||
expect(people[0].stringListDefault).to.have.members(['one', 'two']); | ||
expect(people[0].intSetDefault).to.have.members([1, 2]); | ||
done(); | ||
@@ -279,2 +284,12 @@ }); | ||
}); | ||
it('should keep the counter unchanged', function(done) { | ||
models.instance.Counter.update({user_id:1234}, {visit_count:0}, function(err){ | ||
if(err) throw err; | ||
models.instance.Counter.findOne({user_id:1234}, function(err, stats){ | ||
if(err) throw err; | ||
stats.visit_count.toString().should.equal('2'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should decrement the counter to 0', function(done) { | ||
@@ -290,2 +305,22 @@ models.instance.Counter.update({user_id:1234}, {visit_count:-2}, function(err){ | ||
}); | ||
it('should increment the counter visitCount to 2', function(done) { | ||
models.instance.Counter.update({user_id:1234}, {visitCount:2}, function(err){ | ||
if(err) throw err; | ||
models.instance.Counter.findOne({user_id:1234}, function(err, stats){ | ||
if(err) throw err; | ||
stats.visitCount.toString().should.equal('2'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should decrement the counter visitCount to 0', function(done) { | ||
models.instance.Counter.update({user_id:1234}, {visitCount:-2}, function(err){ | ||
if(err) throw err; | ||
models.instance.Counter.findOne({user_id:1234}, function(err, stats){ | ||
if(err) throw err; | ||
stats.visitCount.toString().should.equal('0'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -292,0 +327,0 @@ |
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
121790
2291