Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
cassandra-driver
Advanced tools
A modern, feature-rich and highly tunable Node.js client library for Apache Cassandra (1.2+) and DataStax Enterprise (3.1+) using exclusively Cassandra's binary protocol and Cassandra Query Language v3.
$ npm install cassandra-driver
You can use the project mailing list or create a ticket on the Jira issue tracker.
If you are upgrading from the 1.x branch of the driver, be sure to have a look at the upgrade guide.
var cassandra = require('cassandra-driver');
var client = new cassandra.Client({ contactPoints: ['h1', 'h2'], keyspace: 'ks1'});
var query = 'SELECT email, last_name FROM user_profiles WHERE key=?';
client.execute(query, ['guy'], function(err, result) {
assert.ifError(err);
console.log('got user profile with email ' + result.rows[0].email);
});
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
var query = 'UPDATE user_profiles SET birth=? WHERE key=?';
var params = [new Date(1942, 10, 1), 'jimi-hendrix'];
//Set the prepare flag in the query options
client.execute(query, params, {prepare: true}, function(err) {
assert.ifError(err);
console.log('Row updated on the cluster');
});
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) {
assert.ifError(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).
client.stream('SELECT time, val FROM temperature WHERE station_id=', ['abc'])
.on('readable', function () {
//readable is emitted as soon a row is received and parsed
var row;
while (row = this.read()) {
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 (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>
);
CREATE TABLE users (
name text PRIMARY KEY,
email text,
address frozen<address>
);
You can retrieve the user address details as a regular Javascript object.
var query = 'SELECT name, email, address FROM users WHERE name = ?';
client.execute(query, [name], { prepare: true}, function (err, result) {
var row = result.first();
var address = row.address;
console.log('User lives in %s, %s - %s', address.street, address.city, 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
var query = 'SELECT * FROM largetable';
client.eachRow(query, [], {autoPage: true}, function (n, row) {
//This function will be called per each of the rows in all the table
}, endCallback);
You can execute multiple statements in a batch to update/insert several rows atomically even in different column families.
var 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 }, function(err) {
assert.ifError(err);
console.log('Data updated on cluster');
});
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 (2.2 and below) even for types that no built-in Javascript representation exists, 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
.
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.
Copyright 2015 DataStax
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
http://www.apache.org/licenses/LICENSE-2.0
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.
2.2.2
2015-10-14
FAQs
DataStax Node.js Driver for Apache Cassandra
The npm package cassandra-driver receives a total of 60,054 weekly downloads. As such, cassandra-driver popularity was classified as popular.
We found that cassandra-driver demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.