Connection pools are interchangeable with connection objects. You can make all Cassandra API calls against pools or single connections the same way. To create a pool:

var scamandrios =require('scamandrios');

var pool =newscamandrios.ConnectionPool(

{

hosts :['localhost:9160'],

keyspace :'scamandrios_test',

user :'test',

password :'test1233',

timeout :3000

cqlVersion :'3.0.0',// default

getHost : getHostFunc,// optional

});

Specify the cqlVersion parameter if you do not wish to use CQL 3.0.

You can supply a function in the getHost parameter to override the random host selection that the pool will perform when handling a request.

As with most error-emitting objects in node, if you do not listen for error it will bubble up to process.uncaughtException.

pool.on('error',function(err)

{

console.error(err.name,err.message);

});

All asynchronous operations return promises in lieu of taking callbacks. The promises library used is P, which is Promises/A+ spec compliant. Here's an example of making a CQL query:

pool.connect()

.then(function()

{

returnpool.cql('SELECT col FROM cf_one WHERE key = ?',['key123']);

})

.then(function(results)

{

// results can be iterated to get rows

results.forEach(function(row)

{

// rows can be iterated to get column contents

row.forEach(function(name,value,timestamp,ttl)

{

console.log(name, value, timestamp, ttl);

});

});

})

.fail(function(err)

{

console.log(err);

}).done();

The first argument to cql() is the query string. The second is an array of items to interpolate into the query string, which is accomplished using util.format(). The result is an array of Row objects. You can always skip quotes around placeholders. Quotes are added automatically. In CQL3 you cannot use placeholders for ColumnFamily or Column names.

CQL can express a set of types more specific than javascript's types. To javascript, a Cassandra set<text> and a list<text> both look like arrays. We found it helpful to have a query-contruction API that accepted type hints, so sets and lists could be interpolated properly into query strings.

The scamandrios.Query constructor exists to help you do this. Here's a somewhat contrived example:

Builds a dictionary in the query object mapping named parameters to their types. Can be called repeatedly to add fields to the dictionary. Types not appearing in this mapping are presumed to be Cassandra identifiers. That is, values that do not need to be quoted or escaped in any way.

If you would like your pool to automatically rediscover its nodes on a timer, use a DiscoveryPool.

var DiscoveryPool =require(scamandrios).DiscoveryPool;

var opts =

{

user:'george',

password:'mischief managed',

lookupSeed:true

};

var pool =newDiscoveryPool('loadbalancing.proxy.example.com', opts);

pool.connect()

.then(function()

{

// make queries etc

}).done();

The seed node will be queried to discover the current state of the ring periodically. As nodes enter & leave, clients will be created & destroyed. The interval isn't yet configurable. It's 30 seconds aka the same as the monitor interval.

Executes the given CQL query on all active clients. Very useful if you want to point your entire connection pool at a single keyspace, for instance, or if you're executing a health-check query on all of them.

assignKeyspace creates the named keyspace if it doesn't exist and then calls connection.use on the keyspace. It is safe to call more than once on a connection object. We use it in the following pattern. Suppose we have an object holding onto a connection to a cassandra instance. We want to make sure that this connection is set up & pointing to the right keyspace before we use it.

var self =this;

this.withKeyspace=this.connection.connect().then(function()

{

returnself.connection.assignKeyspace('my_keyspace');

});

This promise turns into a value for the keyspace. You can then preceed other function calls with obj.withKeyspace.then(). For instance,

scamandrios supports using a custom consistency level. By default, when using the thrift client reads and writes will both use QUORUM. When using the thrift driver, you simply pass a custom level in the options: