DataStax Node.js Driver for Apache Cassandra

License: Apache-2.0

Language: JavaScript

Keywords: cassandra, client, database, datastax, datastax-driver, driver, javascript, nodejs, nosql

DataStax Node.js Driver for Apache Cassandra

A modern, feature-rich and highly tunable Node.js client library for Apache Cassandra (1.2+) using exclusively Cassandra's binary protocol and Cassandra Query Language v3. Use the DSE Node.js driver for better compatibility and support for DataStax Enterprise.


$ npm install cassandra-driver

Build Status Build status



Getting Help

You can use the project mailing list or create a ticket on the Jira issue tracker. Additionally, you can use the #datastax-drivers channel in the DataStax Academy Slack.

Basic usage

const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['h1', 'h2'], localDataCenter: 'datacenter1', keyspace: 'ks1' });

const query = 'SELECT name, email FROM users WHERE key = ?';
client.execute(query, [ 'someone' ])
  .then(result => console.log('User with email %s', result.rows[0].email));

Alternatively, you can use the callback-based execution for all asynchronous methods of the API.

client.execute(query, [ 'someone' ], function(err, result) {
  console.log('User with email %s', result.rows[0].email);

Prepare your queries

Using prepared statements provides multiple benefits. Prepared statements are parsed and prepared on the Cassandra nodes and are ready for future execution. Also, when preparing, the driver retrieves information about the parameter types which allows an accurate mapping between a JavaScript type and a Cassandra type.

The driver will prepare the query once on each host and execute the statement with the bound parameters.

// Use query markers (?) and parameters
const query = 'UPDATE users SET birth = ? WHERE key=?'; 
const params = [ new Date(1942, 10, 1), 'jimi-hendrix' ];
// Set the prepare flag in the query options
client.execute(query, params, { prepare: true })
  .then(result => console.log('Row updated on the cluster'));

Row streaming and pipes

When using #eachRow() and #stream() methods, the driver parses each row as soon as it is received, yielding rows without buffering them.

// Reducing a large result
client.eachRow('SELECT time, val FROM temperature WHERE station_id=', ['abc'],
  function(n, row) {
    // The callback will be invoked per each row as soon as they are received
    minTemperature = Math.min(row.val, minTemperature);
  function (err) {

The #stream() method works in the same way but instead of callback it returns a Readable Streams2 object in objectMode that emits instances of Row. It can be piped downstream and provides automatic pause/resume logic (it buffers when not read).'SELECT time, val FROM temperature WHERE station_id=', [ 'abc' ])
  .on('readable', function () {
    // 'readable' is emitted as soon a row is received and parsed
    let row;
    while (row = {
      console.log('time %s and value %s', row.time, row.val);
  .on('end', function () {
    // Stream ended, there aren't any more rows
  .on('error', function (err) {
    // Something went wrong: err is a response error from Cassandra

User defined types

User defined types (UDT) are represented as JavaScript objects.

For example: Consider the following UDT and table

CREATE TYPE address (
  street text,
  city text,
  state text,
  zip int,
  phones set<text>
  name text PRIMARY KEY,
  email text,
  address frozen<address>

You can retrieve the user address details as a regular JavaScript object.

const query = 'SELECT name, address FROM users WHERE key = ?';
client.execute(query, [ key ], { prepare: true })
  .then(result => {
    const row = result.first();
    const address = row.address;
    console.log('User lives in %s, %s - %s', address.street,, address.state); 

Read more information about using UDTs with the Node.js Driver.


All driver methods use a default fetchSize of 5000 rows, retrieving only first page of results up to a maximum of 5000 rows to shield an application against accidentally large result sets. To retrieve the following records you can use the autoPage flag in the query options of #eachRow() and #stream() methods.

//Imagine a column family with millions of rows
const query = 'SELECT * FROM largetable';
client.eachRow(query, [], { autoPage: true }, function (n, row) {
  // This function will be invoked per each of the rows in all the table
}, endCallback);

Batch multiple statements

You can execute multiple statements in a batch to update/insert several rows atomically even in different column families.

const queries = [
    query: 'UPDATE user_profiles SET email=? WHERE key=?',
    params: [ emailAddress, 'hendrix' ]
    query: 'INSERT INTO user_track (key, text, date) VALUES (?, ?, ?)',
    params: [ 'hendrix', 'Changed email', new Date() ]
client.batch(queries, { prepare: true })
  .then(result => console.log('Data updated on cluster'));

Object Mapper

The driver provides a built-in object mapper that lets you interact with your data like you would interact with a set of documents.

Retrieving objects from the database:

const videos = await videoMapper.find({ userId });
for (let video of videos) {

Updating an object from the database:

await videoMapper.update({ id, userId, name, addedDate, description });

You can read more information about getting started with the Mapper in our documentation.

Data types

There are few data types defined in the ECMAScript specification, this usually represents a problem when you are trying to deal with data types that come from other systems in JavaScript.

The driver supports all the CQL data types in Apache Cassandra (3.0 and below) even for types with no built-in JavaScript representation, like decimal, varint and bigint. Check the documentation on working with numerical values, uuids and collections.


Instances of Client() are EventEmitter and emit log events:

client.on('log', function(level, className, message, furtherInfo) {
  console.log('log event: %s -- %s', level, message);

The level being passed to the listener can be verbose, info, warning or error.


  • Apache Cassandra versions 2.0 and above.
  • DataStax Enterprise versions 4.5 and above.
  • Node.js versions 4 and above.

Note: DataStax products do not support big-endian systems.

Feedback Requested

Help us focus our efforts! Provide your input on the Platform and Runtime Survey (we kept it short).


This driver is based on the original work of Jorge Bay on node-cassandra-cql and adds a series of advanced features that are common across all other DataStax drivers for Apache Cassandra.

The development effort to provide an up to date, high performance, fully featured Node.js Driver for Apache Cassandra will continue on this project, while node-cassandra-cql will be discontinued.


© DataStax, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Project Statistics

Sourcerank 14
Repository Size 4.52 MB
Stars 915
Forks 148
Watchers 73
Open issues disabled
Dependencies 7
Contributors 45
Tags 38
Last updated
Last pushed

Top Contributors See all

Jorge Bay Gondra Andrew Tolbert Andrew Kish Arthur Landim Jan Schmidle Jim Bisso Gabriel Wicke Daniel Smedegaard Buus Sebastian Max Sandeep Tamhankar Aaron Miller Sam Grönblom Michael Ermer Rafał Pocztarski Adrian Pike Jarrett Cruger Suguru Namura Guy Ellis Jack Kingsman Connor Peet

Packages Referencing this Repo

DataStax Node.js Driver for Apache Cassandra
Latest release 4.2.0 - Updated - 915 stars

Recent Tags See all

v4.2.0 September 23, 2019
v4.1.0 April 25, 2019
v4.0.0 December 14, 2018
v3.6.0 December 04, 2018
v3.5.0 April 17, 2018
v3.4.1 February 20, 2018
v3.4.0 February 05, 2018
v3.3.0 September 19, 2017
v3.2.2 June 05, 2017
v3.2.1 April 24, 2017
v3.2.0 January 17, 2017
v3.1.6 November 14, 2016
v3.1.5 October 07, 2016
v3.1.4 September 21, 2016
v3.1.3 August 31, 2016

Interesting Forks See all

Node.js Driver for YugaByte Cloud Query Language (YCQL)
JavaScript - Apache-2.0 - Last pushed - 2 stars
DataStax Node.js Driver for Apache Cassandra
JavaScript - Published - 1 stars
DataStax Node.js Driver for Apache Cassandra
JavaScript - Apache-2.0 - Last pushed - 1 stars
DataStax Node.js Driver for Apache Cassandra
JavaScript - Apache-2.0 - Last pushed - 1 stars

Something wrong with this page? Make a suggestion

Last synced: 2019-09-23 13:46:22 UTC

Login to resync this repository