Class: Client

Client

Extends the Client class of the DataStax Node.js Driver for Apache Cassandra to provide DSE-specific features support such as Graph, geospatial types representation and authentication, in addition to the inherited Client methods used to execute CQL queries (execute(), eachRow(), stream()).

Client instances are designed to be long-lived and usually a single instance is enough per application.

Constructor

new Client(options)

Creates a new Client instance.
Parameters:
Name Type Description
options DseClientOptions The options used to create the client instance.
Example
const dse = require('dse-driver');
const client = new dse.Client({
  contactPoints: ['h1', 'h2'],
  keyspace: 'ks1',
  graphOptions: { name: 'graph1' }
});
const query = 'SELECT email, last_name FROM users WHERE key=?';
client.execute(query, ['guy'], function(err, result) {
  assert.ifError(err);
  console.log('User email ' + result.first().email);
});

Extends

Members

hosts :HostMap

Gets an associative array of cluster hosts.
Type:
Overrides:

keyspace :String

Gets the name of the active keyspace.
Type:
  • String
Overrides:

metadata :Metadata

Gets the schema and cluster metadata information.
Type:
  • Metadata
Overrides:

Methods

batch(queries, optionsopt, callback)

Executes batch of queries on an available connection to a host.
Parameters:
Name Type Attributes Description
queries Array.<string> | Array.<{query, params}> The queries to execute as an Array of strings or as an array of object containing the query and params
options QueryOptions <optional>
callback ResultCallback Executes callback(err, result) when the batch was executed
Overrides:

connect(callback)

Tries to connect to one of the contactPoints and discover the nodes of the cluster.

If the Client is already connected, it immediately invokes callback.

Parameters:
Name Type Description
callback function The callback that is invoked when a connection pool is created to at least one node or it has failed to connect.
Overrides:

connect(callback)

Tries to connect to one of the contactPoints and discover the nodes of the cluster.

If the Client is already connected, it immediately invokes callback.

Parameters:
Name Type Description
callback function The callback is invoked when the pool is connected (or at least 1 connected and the rest failed to connect) or it is not possible to connect
Overrides:

eachRow(query, paramsopt, optionsopt, rowCallback, callbackopt)

Executes the query and calls rowCallback for each row as soon as they are received. Calls final callback after all rows have been sent, or when there is an error.

The query can be prepared (recommended) or not depending on QueryOptions.prepare flag. Retries on multiple hosts if needed.

Parameters:
Name Type Attributes Description
query String The query to execute
params Array | Object <optional>
Array of parameter values or an associative array (object) containing parameter names as keys and its value.
options QueryOptions <optional>
rowCallback function Executes rowCallback(n, row) per each row received, where n is the row index and row is the current Row.
callback function <optional>
Executes callback(err, result) after all rows have been received.

When dealing with paged results, ResultSet#nextPage() method can be used to retrieve the following page. In that case, rowCallback() will be again called for each row and the final callback will be invoked when all rows in the following page has been retrieved.

Overrides:
Examples

Using per-row callback and arrow functions

client.eachRow(query, params, { prepare: true }, (n, row) => console.log(n, row), err => console.error(err));

Overloads

client.eachRow(query, rowCallback);
client.eachRow(query, params, rowCallback);
client.eachRow(query, params, options, rowCallback);
client.eachRow(query, params, rowCallback, callback);
client.eachRow(query, params, options, rowCallback, callback);

execute(query, paramsopt, optionsopt, callback)

Executes a query on an available connection.

The query can be prepared (recommended) or not depending on QueryOptions.prepare flag. Retries on multiple hosts if needed.

Parameters:
Name Type Attributes Description
query String The query to execute
params Array | Object <optional>
Array of parameter values or an associative array (object) containing parameter names as keys and its value
options QueryOptions <optional>
callback ResultCallback Executes callback(err, result) when finished
Overrides:

executeGraph(query, parametersopt, optionsopt, callback)

Executes a graph query.
Parameters:
Name Type Attributes Description
query String The gremlin query.
parameters Object | null <optional>
An associative array containing the key and values of the parameters.
options GraphQueryOptions | null <optional>
The graph query options.
callback function Function to execute when the response is retrieved, taking two arguments: err and result.
Overrides:
Examples
// Getting the first item (vertex, edge, scalar value, ...)
client.executeGraph('g.V()', function (err, result) {
  assert.ifError(err);
  const vertex = result.first();
  console.log(vertex.label);
 });
// Using result.forEach()
client.executeGraph('g.V().hasLabel("person")', function (err, result) {
  assert.ifError(err);
  result.forEach(function(vertex) {
    console.log(vertex.type); // vertex
    console.log(vertex.label); // person
  });
});
// Using ES6 for...of
client.executeGraph('g.E()', function (err, result) {
  assert.ifError(err);
  for (let edge of result) {
    console.log(edge.label); // created
    // ...
  });
});

getReplicas(keyspace, token) → {Array}

Gets the host list representing the replicas that contain such partition.
Parameters:
Name Type Description
keyspace String
token Buffer
Overrides:
Returns:
Type
Array

shutdown(callbackopt)

Closes all connections to all hosts
Parameters:
Name Type Attributes Description
callback function <optional>
Overrides:

stream(query, paramsopt, optionsopt, callbackopt) → {types.ResultStream}

Executes the query and pushes the rows to the result stream as soon as they received. Calls callback after all rows have been sent, or when there is an error.

The stream is a Readable Streams2 object that contains the raw bytes of the field value. It can be piped downstream and provides automatic pause/resume logic (it buffers when not read).

The query can be prepared (recommended) or not depending on QueryOptions.prepare flag. Retries on multiple hosts if needed.

Parameters:
Name Type Attributes Description
query String The query to prepare and execute
params Array | Object <optional>
Array of parameter values or an associative array (object) containing parameter names as keys and its value
options QueryOptions <optional>
callback function <optional>
executes callback(err) after all rows have been received or if there is an error
Overrides:
Returns:
Type
types.ResultStream

Events

hostAdd

Emitted when a new host is added to the cluster.
  • Host The host being added.
Overrides:

hostDown

Emitted when a host in the cluster changed status from up to down.
  • host The host that changed the status.
Overrides:

hostRemove

Emitted when a host is removed from the cluster
  • Host The host being removed.
Overrides:

hostUp

Emitted when a host in the cluster changed status from down to up.
  • host The host that changed the status.
Overrides: