Skip to content

Commit 30ec0dc

Browse files
committed
Supporting counter column operations
1 parent 5ca9731 commit 30ec0dc

5 files changed

Lines changed: 67 additions & 1 deletion

File tree

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,28 @@ Express cassandra exposes some node driver methods for convenience. To generate
164164
* `models.timeuuid()`
165165
returns a type 1 (time-based) uuid, suitable for Cassandra `timeuuid` fields, as a string
166166
* `models.consistencies`
167-
this object contains all the available consistency enums defined by cassandra driver, so you can for example use models.consistencies.one, models.consistencies.quorum etc.
167+
this object contains all the available consistency enums defined by node cassandra driver, so you can for example use models.consistencies.one, models.consistencies.quorum etc.
168+
* `models.datatypes`
169+
this object contains all the available datatypes defined by node cassandra driver, so you can for example use
170+
models.datatypes.Long to deal with the cassandra bigint or counter field types.
168171

172+
### Counter Column Operations
173+
174+
Cassandra counter column increment and decrement operations are supported via the update operation. To increment/decrement a counter, you can use the following types of update operation:
175+
176+
```js
177+
//Say your model name is StatsModel that has a user_id as the primary key and visit_count as a counter column.
178+
179+
models.instance.Stats.update({user_id:1234}, {visit_count:2}, function(err){
180+
//visit_count will be incremented by 2
181+
});
182+
183+
models.instance.Stats.update({user_id:1234}, {visit_count:-1}, function(err){
184+
//visit_count will be decremented by 1
185+
});
186+
```
187+
188+
Please note that counter columns has special limitations, to know more about the counter column usage, see the <a href="http://docs.datastax.com/en/cql/3.1/cql/cql_using/use_counter_t.html">cassandra docs</a>.
169189

170190
### Support for Composite Data Types
171191

lib/expressCassandra.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ module.exports = {
9393
return cql.types.consistencies;
9494
},
9595

96+
get datatypes() {
97+
return cql.types;
98+
},
99+
96100
_translateFileNameToModelName : function(fileName) {
97101
return fileName
98102
.slice( 0,

lib/orm/base_model.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,18 @@ BaseModel._get_db_value_expression = function(fieldname, fieldvalue){
549549
if(retvalset.length > 1) retvalset = retvalset.slice(0,retvalset.length-1);
550550
retvalset += "}";
551551
return retvalset;
552+
case 'counter':
553+
var retvalcounter = fieldname;
554+
if(fieldvalue > 0) {
555+
retvalcounter += " + " + fieldvalue;
556+
}
557+
else if(fieldvalue < 0) {
558+
retvalcounter += " - " + Math.abs(fieldvalue);
559+
}
560+
else {
561+
throw(build_error('model.value.invalidvalue',fieldvalue,fieldname,fieldtype));
562+
}
563+
return retvalcounter;
552564
default:
553565
return fieldvalue;
554566
}

test/models/CounterModel.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
"fields": {
3+
"user_id": "bigint",
4+
"visit_count": "counter"
5+
},
6+
"key" : ["user_id"]
7+
}

test/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,29 @@ describe('Unit Tests', function(){
240240
});
241241
});
242242

243+
describe('#update counter column',function(){
244+
it('should increment the counter to 2', function(done) {
245+
models.instance.Counter.update({user_id:1234}, {visit_count:2}, function(err){
246+
if(err) throw err;
247+
models.instance.Counter.findOne({user_id:1234}, function(err, stats){
248+
if(err) throw err;
249+
stats.visit_count.toString().should.equal('2');
250+
done();
251+
});
252+
});
253+
});
254+
it('should decrement the counter to 0', function(done) {
255+
models.instance.Counter.update({user_id:1234}, {visit_count:-2}, function(err){
256+
if(err) throw err;
257+
models.instance.Counter.findOne({user_id:1234}, function(err, stats){
258+
if(err) throw err;
259+
stats.visit_count.toString().should.equal('0');
260+
done();
261+
});
262+
});
263+
});
264+
});
265+
243266
describe('#close cassandra connection',function(){
244267
it('should close connection to cassandra without errors', function(done) {
245268
models.close(function(err){

0 commit comments

Comments
 (0)