DataStax Enterprise Node.js Driver
This driver is built on top of Node.js driver for Apache Cassandra and provides the following additions for DataStax Enterprise:
Authenticator
implementations that use the authentication scheme negotiation in the server-sideDseAuthenticator
;- encoders for geospatial types which integrate seamlessly with the driver;
- DSE graph integration.
The DataStax Enterprise Node.js Driver can be used solely with DataStax Enterprise. Please consult the license.
Installation
npm install dse-driver
Documentation
Getting Help
You can use the project mailing list or create a ticket on the Jira issue tracker.
Getting Started
Client
inherits from the CQL driver counterpart Client
.
const dse = ;const client = contactPoints: 'host1' 'host2' localDataCenter: 'datacenter1' ; const query = 'SELECT name, email FROM users WHERE key = ?';client ;
Along with the rest of asynchronous execution methods in the driver, execute()
returns a Promise
that
can be chained using then()
method. On modern JavaScript engines, promises can be awaited upon using the await
keyword within async functions.
Alternatively, you can use the callback-based execution for all asynchronous methods of the API by providing a callback as the last parameter.
client;
In order to have concise code examples in this documentation, we will use the promise-based API of the driver
along with the await
keyword.
The same submodules structure in the Node.js driver for Apache Cassandra is available in the dse-driver
, for example:
const dse = ;const Uuid = dsetypesUuid;
Authentication
For clients connecting to a DSE cluster secured with DseAuthenticator
, two authentication providers are included:
DsePlainTextAuthProvider
: Plain-text authentication;DseGSSAPIAuthProvider
: GSSAPI authentication;
To configure a provider, pass it when initializing a cluster:
const dse = ;const client = contactPoints: 'h1' 'h2' keyspace: 'ks1' authProvider: ;
See the jsdoc of each implementation for more details.
Graph
Client
includes the executeGraph()
method to execute graph queries:
const client = contactPoints: 'host1' 'host2' profiles: 'default' graphOptions: name: 'demo' ;
// executeGraph() method returns a Promiseconst result = await client;const vertex = result;console;
Graph Options
You can set graph options in execution profiles when initializing Client
. Also, to avoid providing the graph name
option in each executeGraph()
call, you can set the graph options in the default execution profile:
const client = contactPoints: 'host1' 'host2' profiles: 'default' graphOptions: name: 'demo' 'demo2-profile' graphOptions: name: 'demo2' ;
// Execute a traversal on the 'demo' graphconst result = await client;
If needed, you can specify an execution profile different from the default one:
// Execute a traversal on the 'demo2' graphclient;
Additionally, you can also set the default graph options without using execution profiles (not recommended).
const client = contactPoints: 'host1' 'host2' graphOptions: name: 'demo' ;
Handling Results
Graph queries return a GraphResultSet
, which is an iterable of rows. The format of the data returned is
dependent on the data requested. For example, the payload representing edges will be different than those that
represent vertices using the 'modern' graph:
// Creating the 'modern' graphconst query = 'Vertex marko = graph.addVertex(label, "person", "name", "marko", "age", 29);\n' + 'Vertex vadas = graph.addVertex(label, "person", "name", "vadas", "age", 27);\n' + 'Vertex lop = graph.addVertex(label, "software", "name", "lop", "lang", "java");\n' + 'Vertex josh = graph.addVertex(label, "person", "name", "josh", "age", 32);\n' + 'Vertex ripple = graph.addVertex(label, "software", "name", "ripple", "lang", "java");\n' + 'Vertex peter = graph.addVertex(label, "person", "name", "peter", "age", 35);\n' + 'marko.addEdge("knows", vadas, "weight", 0.5f);\n' + 'marko.addEdge("knows", josh, "weight", 1.0f);\n' + 'marko.addEdge("created", lop, "weight", 0.4f);\n' + 'josh.addEdge("created", ripple, "weight", 1.0f);\n' + 'josh.addEdge("created", lop, "weight", 0.4f);\n' + 'peter.addEdge("created", lop, "weight", 0.2f);'; await client;
// Handling Edgesconst result = await client;result;
// Using ES6 for...ofconst result = await client;for let edge of result console; // created // ...
// Handling Verticesconst result = await client;result;
Parameters
Unlike CQL queries which support both positional and named parameters, graph queries only support named parameters. As a result of this, parameters must be passed in as an object:
const query = 'g.addV(label, vertexLabel, "name", username)';const result = await client;const vertex = result;// ...
Parameters are encoded in json, thus will ultimately use their json representation (toJSON
if present,
otherwise object representation).
You can use results from previous queries as parameters to subsequent queries. For example, if you want to use the id of a vertex returned in a previous query for making a subsequent query:
let result = await client;const vertex = result;result = await client;const names = result;console; // [ 'vadas', 'josh' ]
Prepared graph statements
Prepared graph statements are not supported by DSE Graph yet (they will be added in the near future).
Geospatial types
DSE 5.0 comes with a set of additional CQL types to represent geospatial data: PointType
, LineStringType
and
PolygonType
.
cqlsh> CREATE TABLE points_of_interest(name text PRIMARY KEY, coords 'PointType');
cqlsh> INSERT INTO points_of_interest (name, coords) VALUES ('Eiffel Tower', 'POINT(48.8582 2.2945)');
The DSE driver includes encoders and representations of these types in the geometry
module that can be used directly
as parameters in queries:
const dse = ;const Point = dsegeometryPoint;const insertQuery = 'INSERT INTO points_of_interest (name, coords) VALUES (?, ?)';const selectQuery = 'SELECT coords FROM points_of_interest WHERE name = ?'; await client;const result = await client;const row = result;const point = row'coords';console; // trueconsole; // x: 48.8582, y: 2.2945
Logging
Instances of Client
are EventEmitter
and emit 'log'
events:
client;
The level
values passed to the listener can be verbose
, info
, warning
or error
. In production environment, you should filter out verbose
log events, that are suitable for debug.
Compatibility
- DataStax Enterprise versions 4.5 and above.
- Node.js versions 4 and above.
Note: DataStax products do not support big-endian systems.
License
© DataStax, Inc.
The full license terms are available at https://www.datastax.com/terms/datastax-dse-driver-license-terms