express-cassandra
Advanced tools
Comparing version 0.3.8 to 0.4.0
@@ -545,24 +545,49 @@ var util = require('util'), | ||
var cql_ops = {'$eq':'=', '$gt':'>', '$lt':'<', '$gte':'>=', '$lte':'<=', '$in':'IN', '$token':'token'}; | ||
var rel_keys = Object.keys(field_relation); | ||
if(rel_keys.length > 1) | ||
throw(build_error('model.find.multiop')); | ||
for(var rk in rel_keys) { | ||
var first_key = rel_keys[rk], | ||
first_value = field_relation[first_key]; | ||
if(first_key.toLowerCase() in cql_ops){ | ||
first_key = first_key.toLowerCase(); | ||
var op = cql_ops[first_key]; | ||
var cql_ops = {'$eq':'=', '$gt':'>', '$lt':'<', '$gte':'>=', '$lte':'<=', '$in':'IN'}; | ||
if(first_key == '$in' && !(first_value instanceof Array)) | ||
throw(build_error('model.find.invalidinset')); | ||
if(first_key == '$token' && !(first_value instanceof Object)) | ||
throw(build_error('model.find.invalidinset')); | ||
var first_key = rel_keys[0], | ||
first_value = field_relation[first_key]; | ||
if(first_key.toLowerCase() in cql_ops){ | ||
first_key = first_key.toLowerCase(); | ||
var op = cql_ops[first_key]; | ||
var where_template = '"%s" %s %s'; | ||
if(first_key == '$token') { | ||
where_template = 'token("%s") %s token(%s)'; | ||
if(first_key == '$in' && !(first_value instanceof Array)) | ||
throw(build_error('model.find.invalidinset')); | ||
query_relations.push( util.format( | ||
'"%s" %s %s', | ||
k,op,this._get_db_value_expression(k,first_value) | ||
)); | ||
var token_rel_keys = Object.keys(first_value); | ||
for(var token_rk in token_rel_keys) { | ||
var token_first_key = token_rel_keys[token_rk]; | ||
var token_first_value = first_value[token_first_key]; | ||
if((token_first_key.toLowerCase() in cql_ops) && token_first_key.toLowerCase()!='$token' && token_first_key.toLowerCase()!='$in'){ | ||
token_first_key = token_first_key.toLowerCase(); | ||
op = cql_ops[token_first_key]; | ||
} | ||
else { | ||
throw(build_error('model.find.invalidop',token_first_key)); | ||
} | ||
query_relations.push( util.format( | ||
where_template, | ||
k,op,this._get_db_value_expression(k,token_first_value) | ||
)); | ||
} | ||
} | ||
else { | ||
query_relations.push( util.format( | ||
where_template, | ||
k,op,this._get_db_value_expression(k,first_value) | ||
)); | ||
} | ||
} | ||
else { | ||
throw(build_error('model.find.invalidop',first_key)); | ||
} | ||
} | ||
else { | ||
throw(build_error('model.find.invalidop',first_key)); | ||
} | ||
} | ||
@@ -636,3 +661,3 @@ } | ||
if(options.allow_filtering) query += ' ALLOW FILTERING;'; | ||
else query += ';' | ||
else query += ';'; | ||
@@ -639,0 +664,0 @@ return query; |
{ | ||
"name": "express-cassandra", | ||
"version": "0.3.8", | ||
"version": "0.4.0", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "async": "^1.0.0", |
@@ -370,3 +370,3 @@ [![Build Status](https://travis-ci.org/masumsoft/express-cassandra.svg)](https://travis-ci.org/masumsoft/express-cassandra) | ||
name: 'John', // stays for name='john' | ||
age : { '$gt':10 }, // stays for age>10 You can also use $gte, $lt, $lte | ||
age : { '$gt':10, '$lte':20 }, // stays for age>10 and age<=20 You can also use $gt, $gte, $lt, $lte, $eq | ||
surname : { '$in': ['Doe','Smith'] }, //This is an IN clause | ||
@@ -393,2 +393,27 @@ $orderby:{'$asc' :'age'}, //Order results by age in ascending order. Also allowed $desc and complex order like $orderby:{'$asc' : ['k1','k2'] } | ||
You can also use the `token` comparison function while querying a result set using the $token operator. This is specially useful for <a href="http://docs.datastax.com/en/cql/3.1/cql/cql_using/paging_c.html">paging through unordered partitioner results</a>. | ||
```js | ||
//consider the following situation | ||
var query = { | ||
$limit:10 | ||
}; | ||
models.instance.Person.find(query, function(err, people){ | ||
//people is an array of first 10 persons | ||
//Say your PRIMARY_KEY column is `name` and the 10th person has the name 'John' | ||
//Now to get the next 10 results, you may use the $token operator like the following: | ||
var query = { | ||
name:{ | ||
'$token':{'$gt':'John'} | ||
}, | ||
$limit:10 | ||
}; | ||
//The above query translates to `Select * from person where token(name) > token('John') limit 10` | ||
models.instance.Person.find(query, function(err, people){ | ||
//people is an array of objects containing the 11th - 20th person | ||
}); | ||
}); | ||
``` | ||
Note that all query clauses must be Cassandra compliant. You cannot, for example, use $in operator for a key which is not the partition key. Querying in Cassandra is very basic but could be confusing at first. Take a look at this <a href="http://mechanics.flite.com/blog/2013/11/05/breaking-down-the-cql-where-clause/" target="_blank">post</a> and, obvsiouly, at the <a href="http://www.datastax.com/documentation/cql/3.1/cql/cql_using/about_cql_c.html" target="_blank">documentation</a> | ||
@@ -395,0 +420,0 @@ |
@@ -75,5 +75,5 @@ var models = require('../index'); | ||
describe('#find with $gt operator',function(){ | ||
describe('#find with $gt and $lt operator',function(){ | ||
it('should find data as saved without errors', function(done) { | ||
models.instance.Person.find({userID:1234, age:{'$gt':31}}, function(err, people){ | ||
models.instance.Person.find({userID:1234, age:{'$gt':31,'$lt':35}}, function(err, people){ | ||
if(err) throw err; | ||
@@ -96,2 +96,12 @@ people.length.should.equal(1); | ||
describe('#find with $token operator',function(){ | ||
it('should find data as saved without errors', function(done) { | ||
models.instance.Person.find({userID:{'$token':{'$gt':1235,'$lte':1234}},$limit:1}, function(err, people){ | ||
if(err) throw err; | ||
people.length.should.equal(1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('#update',function(){ | ||
@@ -98,0 +108,0 @@ it('should update data on db 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
96202
1770
573