DataStax Node.js Driver for Apache Cassandra®
$ 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;
The driver supports both promises and callbacks for the asynchronous methods, you can choose the approach that suits your needs.
Note that in order to have concise code examples in this documentation, we will use the promise-based API of the
driver along with the
Prepare your queries
Using prepared statements provides multiple benefits.
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 optionsawait client;console;
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 = ?';const result = await client;const row = result;const address = rowaddress;console;
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 retrieving large result sets in a single response.
stream() method automatically fetches the following page once the current one was read. You can also use
method to retrieve the following pages by using
autoPage flag. See [paging documentation for more
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';await client;console;
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
error. Visit the logging
documentation for more information.
- Apache Cassandra versions 2.1 and above.
- DataStax Enterprise versions 4.8 and above.
- Node.js versions 8 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.