CQLify
A ORM (Object-relational mapping) libraby for Cassandra ontop of the data-stax cassandra driver. This library will abstract the need to write raw insert/update/select statements.
Status : PRE-ALPHA
(use at your own risk!, higly unstable)
Current Version : 0.0.15
Installation
$ npm install cqlify
Creating a Connection
Uses all of the same connection options as the data-stax cassandra driver. Here is an exmple of a basic C*(Cassandra) database with basic authentication:
var cassandra = require('cassandra-driver');
var connectionOptions = {
contactPoints: ["2.2.2.2","3.3.3.3"],
keyspace: "keyspace",
policies: {},
authProvider: new cassandra.auth.PlainTextAuthProvider("user", "password")
};
var cqlify = require('./cqlify');
cqlify.createConnection(connectionOptions);
Creating a model
Now that you have a connection you can create a model. In your model you will specify the schema, which describes the format of your object. Let's take an example table:
Column | Type |
---|---|
id | timeuuid |
first_name | text |
address | text |
user_model.js
var { var schema = id: type: cqlifytypesTIMEUUID key_type: cqlifytypesPRIMARY_KEY key_order: 1 first_name: type: cqlifytypesTEXT address: type: cqlifytypesTEXT ; var model = cqlify; model; return model; }; moduleexports = userModel;
Supported Types
- TimeUUID - C* type
- TimeStamp - C* type
- Int - C* type
- BigInt - C* type
- Boolean - C* type
- UUID - C* type
- JSONTOTEXT - Propietry type which will allow you to specify your field as a JSON object. Cqlify will convert that object to JSON and store it as text in the DB.
- ENUMTOTEXT - Like JSON to text you can specify a Javascript enum value, and in turn cqlify will handle the conversions to text and back.
Mixed Support
- Map : Supported on Insert and Get.. will eventually support update with a future enhancement
- List : Supported on Insert and Get.. will eventually support update with a future enhancement
- Counter : No support.. to come.. you could an Int for now, however, need to build in logic for incremental updates.. i.e. "counter + 1"
Select
In this section we will cover how to select data using cqlify. It is a very easy process to select out data
Lookup with one field
var user = cqlify; //create your model user;
Lookup with multi-field, c always treats this as an AND*
var user = cqlify; //create your model var query = params: name:'id' value: 'idvalue' comparer: cqlifycomparerEQUALS name:'first_name' value: 'first_name' comparer: cqlifycomparerEQUALS ; user;
Comparer Types
- EQUALS : cqlify.types.EQUALS : Standard equals operator .. i.e. field="value"
- GREATER_THAN :cqlify.types.GREATER_THAN : count > 10
- LESS_THAN :cqlify.types.LESS_THAN : count < 10
- IN : cqlify.types.IN : value in ("value1","value2","value3)
Insert
var user = cqlify; userid = cqlifytypes; userfirst_name = "Robert"; useraddress = "some address"; var query = {}; user;
Update
Here is an example of pulling a record from the database and then updating one field (address in this case).
var user = cqlify; var query = params: name:'id' value: userid comparer: cqlifycomparerEQUALS ; user;
Using Counters
Counters are kind of a special animal when it comes to CQL.. To insert a Counter you actually have to do an Update.. even if it is new.. odd I know.. Counters must always be incremented or decremented, thus CQLify enforces a format like '+0' '+1' '-5' etc.. Thus, here is how you would create a new counter..
Model
var { var schema = counter_value: type: cqlifytypesCOUNTER url_name: type: cqlifytypesTEXT page_name: type: cqlifytypesTEXT ; var opts = tableName: 'counts' ; var model = cqlify; return model; };
Code
var page = ; pagepage_name = 'cnn'; pageurl_name = 'www.cnn.com'; page; //this is kind of a hack for now.. basically you never want the primary keys being marked as dirty, which then puts it on the update set. pagecounter_value = "+0"; //must always increment or decrement .. cannot set a value var query = params: name: "page_name" value: pagepage_name comparer: cqlifycomparerEQUALS name: "url_name" value: pageurl_name comparer: cqlifycomparerEQUALS ; page;
Validation
Out of the box we validate certain fields to ensure they meet the specification to insert into the database. The fields we do contstraint validation on :
- TimeUuid
- Text
- Int(Int32)
- bigint(Int64)
- Date
- Boolean
Custom Vaidation
All schema types allow for custom validation, all you need to do is add a "validators" array to a schema element.
Validate first name must be atleast 5 characters
var userModel = function () { var schema = { id: { type: cqlify.types.TIMEUUID, key_type: cqlify.types.PRIMARY_KEY, key_order: 1 }, first_name: { type: cqlify.types.TEXT, validators: [ function (obj) { if (obj && obj.length < 5){ return cqlify.util.constructValidationMessage(false, "First name must be atleast 5 characters"); //construct the error message. } return true; //return back the object is valid } ] } } });
Ad-hoc validation
You can always validate manually by invoking the _validate method
var isValid = user;
Author : Robert Polak
Bugs
See https://github.com/robpolak/cqlify/issues.
License
The MIT License (MIT)