lib/client.js
function Client(options) {
  events.EventEmitter.call(this);
  this.options = clientOptions.extend({ logEmitter: this.emit.bind(this) }, options);
  Object.defineProperty(this, 'profileManager', { value: new ProfileManager(this.options) });
  Object.defineProperty(this, 'controlConnection', {
    value: new ControlConnection(this.options, this.profileManager), writable: true }
  );
  //Unlimited amount of listeners for internal event queues by default
  this.setMaxListeners(0);
  this.connected = false;
  this.isShuttingDown = false;
  /**
   * Gets the name of the active keyspace.
   * @type {String}
   */
  this.keyspace = options.keyspace;
  /**
   * Gets the schema and cluster metadata information.
   * @type {Metadata}
   */
  this.metadata = this.controlConnection.metadata;
  /**
   * Gets an associative array of cluster hosts.
   * @type {HostMap}
   */
  this.hosts = this.controlConnection.hosts;

  /**
   * The [ClientMetrics]{@link module:metrics~ClientMetrics} instance used to expose measurements of its internal
   * behavior and of the server as seen from the driver side.
   * <p>By default, a [DefaultMetrics]{@link module:metrics~DefaultMetrics} instance is used.</p>
   * @type {ClientMetrics}
   */
  this.metrics = this.options.metrics;
}
function Client(options) {
  events.EventEmitter.call(this);
  this.options = clientOptions.extend({ logEmitter: this.emit.bind(this) }, options);
  Object.defineProperty(this, 'profileManager', { value: new ProfileManager(this.options) });
  Object.defineProperty(this, 'controlConnection', {
    value: new ControlConnection(this.options, this.profileManager), writable: true }
  );
  //Unlimited amount of listeners for internal event queues by default
  this.setMaxListeners(0);
  this.connected = false;
  this.isShuttingDown = false;
  /**
   * Gets the name of the active keyspace.
   * @type {String}
   */
  this.keyspace = options.keyspace;
  /**
   * Gets the schema and cluster metadata information.
   * @type {Metadata}
   */
  this.metadata = this.controlConnection.metadata;
  /**
   * Gets an associative array of cluster hosts.
   * @type {HostMap}
   */
  this.hosts = this.controlConnection.hosts;

  /**
   * The [ClientMetrics]{@link module:metrics~ClientMetrics} instance used to expose measurements of its internal
   * behavior and of the server as seen from the driver side.
   * <p>By default, a [DefaultMetrics]{@link module:metrics~DefaultMetrics} instance is used.</p>
   * @type {ClientMetrics}
   */
  this.metrics = this.options.metrics;
}
Query options
@typedef {Object} QueryOptions
@property {Boolean} [autoPage] Determines if the driver must retrieve the following result pages automatically.
<p>
This setting is only considered by the [Client#eachRow()]{@link Client#eachRow} method. For more information,
check the
[paging results documentation]{@link http://docs.datastax.com/en/developer/nodejs-driver/latest/features/paging/}.
</p>
@property {Boolean} [captureStackTrace] Determines if the stack trace before the query execution should be
maintained.
<p>
Useful for debugging purposes, it should be set to <code>false</code> under production environment as it adds an
unnecessary overhead to each execution.
</p>
Default: false.
@property {Number} [consistency] [Consistency level]{@link module:types~consistencies}. Default: localOne.
@property {Object} [customPayload] Key-value payload to be passed to the server. On the Cassandra side,
implementations of QueryHandler can use this data.
@property {String|ExecutionProfile} [executionProfile] Name or instance of the [profile]{@link ExecutionProfile} to
be used for this execution. If not set, it will the use "default" execution profile.
@property {Number} [fetchSize] Amount of rows to retrieve per page.
@property {Array|Array<Array>} [hints] Type hints for parameters given in the query, ordered as for the parameters.
<p>For batch queries, an array of such arrays, ordered as with the queries in the batch.</p>
@property {Host} [host] The host that should handle the query.
<p>
Use of this option is <em>heavily discouraged</em> and should only be used in the following cases:
</p>
<ol>
<li>
Querying node-local tables, such as tables in the <code>system</code> and <code>system_views</code>
keyspaces.
</li>
<li>
Applying a series of schema changes, where it may be advantageous to execute schema changes in sequence on the
same node.
</li>
</ol>
<p>
Configuring a specific host causes the configured
[LoadBalancingPolicy]{@link module:policies/loadBalancing~LoadBalancingPolicy} to be completely bypassed.
However, if the load balancing policy dictates that the host is at a
[distance of ignored]{@link module:types~distance} or there is no active connectivity to the host, the request will
fail with a [NoHostAvailableError]{@link module:errors~NoHostAvailableError}.
</p>
@property {Boolean} [isIdempotent] Defines whether the query can be applied multiple times without changing the result
beyond the initial application.
<p>
The query execution idempotence can be used at [RetryPolicy]{@link module:policies/retry~RetryPolicy} level to
determine if an statement can be retried in case of request error or write timeout.
</p>
<p>Default: <code>false</code>.</p>
@property {String} [keyspace] Specifies the keyspace for the query. Used for routing within the driver, this
property is suitable when the query operates on a different keyspace than the current {@link Client#keyspace}.
<p>
This property should only be set manually by the user when the query operates on a different keyspace than
the current {@link Client#keyspace} and using either batch or non-prepared query executions.
</p>
@property {Boolean} [logged] Determines if the batch should be written to the batchlog. Only valid for
[Client#batch()]{@link Client#batch}, it will be ignored by other methods. Default: true.
@property {Boolean} [counter] Determines if its a counter batch. Only valid for
[Client#batch()]{@link Client#batch}, it will be ignored by other methods. Default: false.
@property {Buffer|String} [pageState] Buffer or string token representing the paging state.
<p>Useful for manual paging, if provided, the query will be executed starting from a given paging state.</p>
@property {Boolean} [prepare] Determines if the query must be executed as a prepared statement.
@property {Number} [readTimeout] When defined, it overrides the default read timeout
(<code>socketOptions.readTimeout</code>) in milliseconds for this execution per coordinator.
<p>
Suitable for statements for which the coordinator may allow a longer server-side timeout, for example aggregation
queries.
</p>
<p>
A value of <code>0</code> disables client side read timeout for the execution. Default: <code>undefined</code>.
</p>
@property {RetryPolicy} [retry] Retry policy for the query.
<p>
This property can be used to specify a different [retry policy]{@link module:policies/retry} to the one specified
in the {@link ClientOptions}.policies.
</p>
@property {Array} [routingIndexes] Index of the parameters that are part of the partition key to determine
the routing.
@property {Buffer|Array} [routingKey] Partition key(s) to determine which coordinator should be used for the query.
@property {Array} [routingNames] Array of the parameters names that are part of the partition key to determine the
routing. Only valid for non-prepared requests, it's recommended that you use the prepare flag instead.
@property {Number} [serialConsistency] Serial consistency is the consistency level for the serial phase of
conditional updates.
This option will be ignored for anything else that a conditional update/insert.
@property {Number|Long} [timestamp] The default timestamp for the query in microseconds from the unix epoch
(00:00:00, January 1st, 1970).
<p>If provided, this will replace the server side assigned timestamp as default timestamp.</p>
<p>Use [generateTimestamp()]{@link module:types~generateTimestamp} utility method to generate a valid timestamp
based on a Date and microseconds parts.</p>
@property {Boolean} [traceQuery] Enable query tracing for the execution. Use query tracing to diagnose performance
problems related to query executions. Default: false.
<p>To retrieve trace, you can call [Metadata.getTrace()]{@link module:metadata~Metadata#getTrace} method.</p>
 
Creates a new instance of {@link Client}.
@classdesc
A Client holds connections to a Cassandra cluster, allowing it to be queried.
Each Client instance maintains multiple connections to the cluster nodes,
provides [policies]{@link module:policies} to choose which node to use for each query,
and handles [retries]{@link module:policies/retry} for failed query (when it makes sense), etc...
<p>
Client instances are designed to be long-lived and usually a single instance is enough
per application. As a given Client can only be "logged" into one keyspace at
a time (where the "logged" keyspace is the one used by query if the query doesn't
explicitly use a fully qualified table name), it can make sense to create one
client per keyspace used. This is however not necessary to query multiple keyspaces
since it is always possible to use a single session with fully qualified table name
in queries.
</p>
@extends EventEmitter
@param {ClientOptions} options The options for this instance.
@example <caption>Creating a new client instance</caption>
const client = new Client({ contactPoints: ['192.168.1.100'], localDataCenter: 'datacenter1' });
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());
});
@example <caption>Executing a query</caption>
// 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']);
});
@example <caption>Executing a query with promise-based API</caption>
const result = await client.execute('SELECT key FROM system.local');
const row = result.first();
console.log(row['key']);
@constructor