node-cassandra
An ORM for Cassandra
- Provides support for current Cassandra 3.x builds.
- Tested at scale with distribution in real production environments.
- Mostly feature-complete, active development is underway to add remaining features.
- Open to suggestions! Just create an issue and tag it.
Read the full API Documentation.
Examples:
- Getting Started
- Table Compaction
- Static Methods
- Working with List Types
- Working with Set Types
- Working with Map Types
Cassandra Known Versions Supported:
- 3.x
Known Supported NodeJS Versions:
- <= 7.6
- 6.x
- 5.x
Getting Started
Connecting to the database
var Cassandra = ;var config = contactPoints: '127.0.0.1:9042' protocolOptions: port: 9042 keyspace: testkeyspace: durableWrites: true withReplication: class: 'SimpleStrategy' replication_factor: 1 ; var cassandra = Cassandra;cassandra;cassandra;
Creating your first Schema, Model and Materialized View
//create a new schemavar schema = id: type: 'uuid' default: Cassandrauuid username: type: 'text' required: true ageName: type: 'text' { //default value functions are passed the object instance return instanceage + instancename; } name: 'text' age: 'int' primaryKeys: 'username' views: byName: primaryKeys: 'name' orderBy: name: 'asc' ; //create modelvar TestModel = cassandra; var test = name: 'bar' username: 'foo' age: 30;
Working with Collections and Indexes
List Types
//create a new schemavar schema = name: 'text' usernames: type: list: 'text' indexes: 'usernames' //create an index on the usernames column primaryKeys: 'name';
Inserting lists
//create modelvar TestModel = cassandra;
Updating lists
//following abovevar query = name: 'bar' usernames: $contains: 'foo' ;var updateObject = usernames: $filter: 'foo' //remove items matching "foo" $append: 'baz' //append item "baz" $prepend: 'fii' //prepend item "fii" ;TestModel;
Updating lists by index
//following abovevar query = name: 'bar' usernames: $contains: 'bar' ;var updateObject = usernames: $set: 0: 'foo' ;TestModel;
Deleting lists by column or index
//following abovevar query = name: 'bar' usernames: $contains: 'fii' ;var deleteObject = usernames: 02 //delete first and last index;TestModel;
Set Types
//create a new schemavar schema = name: 'text' usernames: type: set: 'text' indexes: 'usernames' //create an index on the usernames column primaryKeys: 'name';
Inserting sets
//create modelvar TestModel = cassandra;
Updating sets
//following abovevar query = name: 'bar' usernames: $contains: 'bar' ;var updateObject = usernames: $add: 'aba' 'boa' $filter: 'foo' ;TestModel;
Update sets to null
//following abovevar query = name: 'bar' usernames: $contains: 'bar' ;var updateObject = usernames: //or null;TestModel;
Deleting sets
//not much you can do with deleting setsvar query = name: 'bar' usernames: $contains: 'fii' ;var deleteObject = usernames: 1 //delete usernames column;TestModel;
Map Types
//create a new schemavar schema = name: 'text' usernames: type: map: 'text' 'int' indexes: 'usernames' //create an index on the usernames column keys primaryKeys: 'name';
Inserting maps
//create modelvar TestModel = cassandra;
Updating maps
//following abovevar query = name: 'bar' usernames: $containsKey: 'bar' ;var updateObject = usernames: foo: 5 //update "foo" to 5 fii: 7 /set "fii" to 7 ;TestModel;
Deleting maps by Keys
//not much you can do with deleting mapsvar query = name: 'bar' usernames: $containsKey: 'bar' ;var deleteObject = usernames: 'foo';TestModel;
Deleting maps
//not much you can do with deleting mapsvar query = name: 'bar' usernames: $containsKey: 'bar' ;var deleteObject = usernames: 1;TestModel;
Table Compaction
//create a new schemavar schema = id: 'uuid' name: 'text' primaryKeys: 'username' compaction: class: 'DateTieredCompactionStrategy' timestamp_resolution: 'MICROSECONDS' base_time_seconds: 3600 max_sstable_age_days: 365 ;
Static Methods
//create a new schemavar schema = id: type: 'uuid' default: Cassandrauuid username: type: 'text' required: true name: 'text' primaryKeys: 'username' views: byName: primaryKeys: 'name' ; //attach static method - fat arrow functions won't work here as we need the context to changeschemastatics { thisviewsbyName;}; //create modelvar TestModel = cassandra;