NodeJs and Apache Cassandra

The next topic for my project was learning about alternative ways for interaction between NodeJs and the Apache Cassandra database.

As a background I am new to the Apache Cassandra environment. In fact, my recent background is in MEAN (MongoDB, ExpressJs, AngularJs, NodeJs). In that world the mapping between MongoDB documents and NodeJs artifacts is well understood and frankly it is one of simplest and most natural Object-Database Mapping (ODM) that I have seen. I am a fan of that approach.

It is not perfect and there is always a tradeoff of productivity vs. efficiency. In previous projects we have found a middle ground where most of the work was done with Mongoose letting the package do its magic and letting us focus more on our application, and only if and when needed, overwriting these commands, getting into the dirty details and implementing optimizations to the needed degree. As the saying goes “keep simple things simple and make complex things possible”.

With that in mind let’s look at the top packages I am using for my NodeJs-Cassandra project:

The first excellent package to consider is DataStax Node.js Driver for Apache Cassandra. Everyone simply call this the cassandra-driver and you can find it here. I found the package to be stable, complete and regularly updated. It provides a direct access to the Cassandra Query Language also known as CQL.

As a very simple example, here is a call to fetch all employees in a department:

findeployeesindepartment

The two aspects to highlight is the connection to the database:

var db = new cassandra.Client(
{ contactPoints: ['127.0.0.1'],
protocolOptions: {port: 9042},
keyspace: 'myDB'});

And the call to execute a command (in this case “SELECT” command):

var query = 'SELECT * FROM employees WHERE department = ?';
db.execute(query, [department], function(err, result)...

Note that when executing a command there is a method here to pass parameters to the command. The parameters use are marked with question-mark character inside the CQL command. The values are passed to the execute command as an array of ordered values (i.e. the order in which the the question-marks appear in the command).

All of this is simple enough and useful but it requires me to get into the CQL level and implement manually a set of classes to map database entities to NodJs artifacts. I intend to use this package directly every time there is a need to get to the deeper command level optimization. This is for the complex cases.

Back to the MEAN background, what about the simple cases? Is there a way to have a basic object mapping done transparently, something similar to Mongoose for MongoDB in the MEAN world?

Not to worry. Someone already built a package for that. It is called Cassie-ODM or simply Cassie and it currently in Beta. I gave it a run and liked it a lot. More on that next time.

Advertisements

Author: dave

Consider myself kid in soul and naive by choice. I am interested in people, technology and business and thrive when they all work together. My favorite quote and motto is that “You can fool some people some times but you cant fool all the people all the time” ― Bob Marley

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s