Constructor
new Client(options)
Parameters:
Name | Type | Description |
---|---|---|
options |
ClientOptions | The options for this instance. |
Examples
Creating a new client instance
const client = new Client({ contactPoints: ['192.168.1.100'] });
client.connect(function (err) {
if (err) return console.error(err);
console.log('Connected to cluster with %d host(s): %j', client.hosts.length, client.hosts.keys());
});
Executing a query
// calling #execute() can be made without previously calling #connect(), as internally
// it will ensure it's connected before attempting to execute the query
client.execute('SELECT key FROM system.local', function (err, result) {
if (err) return console.error(err);
const row = result.first();
console.log(row['key']);
});
Extends
- EventEmitter
Members
hosts :HostMap
Type:
keyspace :String
Type:
- String
metadata :Metadata
Type:
- Metadata
Methods
batch(queries, optionsopt, callback)
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 |
connect(callback)
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 |
eachRow(query, paramsopt, optionsopt, rowCallback, callbackopt)
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, |
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)
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 |
getReplicas(keyspace, token) → {Array}
Parameters:
Name | Type | Description |
---|---|---|
keyspace |
String | |
token |
Buffer |
Returns:
- Type
- Array
shutdown(callbackopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
function |
<optional> |
stream(query, paramsopt, optionsopt, callbackopt) → {types.ResultStream}
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 |
Returns:
- Type
- types.ResultStream
Events
hostAdd
- Host The host being added.
hostDown
- host The host that changed the status.
hostRemove
- Host The host being removed.
hostUp
- host The host that changed the status.