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
- Simple, Prepared, and Batch statements
- Asynchronous IO, parallel execution, request pipelining
- Connection pooling
- Auto node discovery
- Automatic reconnection
- Configurable load balancing and retry policies
- Works with any cluster size
- Built-in object mapper
- Both promise and callback-based API
- Row streaming and pipes
- Built-in TypeScript support
const cassandra = ;const client = contactPoints: 'h1' 'h2' localDataCenter: 'datacenter1' keyspace: 'ks1' ;const query = 'SELECT name, email FROM users WHERE key = ?';client;
Alternatively, you can use the callback-based execution for all asynchronous methods of the API.
Prepare your queries
The driver will prepare the query once on each host and execute the statement with the bound parameters.
// Use query markers (?) and parametersconst query = 'UPDATE users SET birth = ? WHERE key=?';const params = 1942 10 1 'jimi-hendrix' ;// Set the prepare flag in the query optionsclient;
Row streaming and pipes
#stream() methods, the driver parses each row as soon as it is received,
yielding rows without buffering them.
// Reducing a large resultclient;
#stream() method works in the same way but instead of callback it returns a Readable Streams2 object
objectMode that emits instances of
It can be piped downstream and provides automatic pause/resume logic (it buffers when not read).
User defined types
For example: Consider the following UDT and table
CREATE TYPE address (street text,city text,state text,zip int,phones set<text>);CREATE TABLE users (name text PRIMARY KEY,email text,address frozen<address>);
const query = 'SELECT name, address FROM users WHERE key = ?';client;
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
//Imagine a column family with millions of rowsconst query = 'SELECT * FROM largetable';client;
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';client;
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;for let video of videosconsole;
Updating an object from the database:
You can read more information about getting started with the Mapper in our documentation.
EventEmitter and emit
level being passed to the listener can be
- 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.
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.