# Client

<div class="jsdoc-content"><header>
    
        <h2><span class="attribs"><span class="type-signature"></span></span>Client<span class="signature">(options)</span><span class="type-signature"></span></h2>
        
            <div class="class-description"><p>Represents a database client that maintains multiple connections to the cluster nodes, providing methods to
execute CQL statements.</p>
<p>The <code>Client</code> uses <a href="policies/index.html">policies</a> to decide which nodes to connect to, which node
to use per each query execution, when it should retry failed or timed-out executions and how reconnection to down
nodes should be made.</p></div>
        
    
</header>


    
        

    
    </div>

## Constructor

<div class="jsdoc-content">
    

    
    <h4 class="name" id="Client"><span class="type-signature"></span>new Client<span class="signature">(options)</span><span class="type-signature"></span></h4>
    

    



<div class="description">
    <p>Creates a new instance of <a href="Client.html">Client</a>.</p>
</div>









    <h5>Parameters:</h5>
    

<table class="params docutils align-default">
    <thead>
    <tr>
        
        <th>Name</th>
        

        <th>Type</th>

        

        

        <th class="last">Description</th>
    </tr>
    </thead>

    <tbody>
    

        <tr>
            
                <td class="name"><code>options</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>The options for this instance.</p></td>
        </tr>

    
    </tbody>
</table>






<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L92" target="_blank" rel="noopener">client.ts, line 92</a></p></div>



















    <h5>Examples</h5>
    
        <p class="code-caption">Creating a new client instance</p>
    
    <div class="highlight-javascript notranslate"><div class="highlight"><pre>const client = new Client({
  contactPoints: ['10.0.1.101', '10.0.1.102'],
});</pre></div></div>

        <p class="code-caption">Executing a query</p>
    
    <div class="highlight-javascript notranslate"><div class="highlight"><pre>const result = await client.connect();
console.log(\`Connected to ${client.hosts.length} nodes in the cluster: ${client.hosts.keys().join(', ')}\`);</pre></div></div>

        <p class="code-caption">Executing a query</p>
    
    <div class="highlight-javascript notranslate"><div class="highlight"><pre>const result = await client.execute('SELECT key FROM system.local');
const row = result.first();
console.log(row['key']);</pre></div></div>



    
    

    
        </div>

## Extends

<div class="jsdoc-content">

        


    <ul>
        <li>events.EventEmitter</li>
    </ul>


    

    

    

    

    

    

    
        </div>

## Members

<div class="jsdoc-content">

        
            
<h4 class="name" id="hosts"><span class="type-signature"></span>hosts<span class="type-signature"></span></h4>




<div class="description">
    <p>Gets an associative array of cluster hosts.</p>
</div>







<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L205" target="_blank" rel="noopener">client.ts, line 205</a></p></div>






        
            
<h4 class="name" id="keyspace"><span class="type-signature"></span>keyspace<span class="type-signature"></span></h4>




<div class="description">
    <p>Gets the name of the active keyspace.</p>
</div>







<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L193" target="_blank" rel="noopener">client.ts, line 193</a></p></div>






        
            
<h4 class="name" id="metadata"><span class="type-signature"></span>metadata<span class="type-signature"></span></h4>




<div class="description">
    <p>Gets the schema and cluster metadata information.
TODO: This field is currently not supported</p>
</div>







<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L131" target="_blank" rel="noopener">client.ts, line 131</a></p></div>






        
            
<h4 class="name" id="metrics"><span class="type-signature"></span>metrics<span class="type-signature"></span></h4>




<div class="description">
    <p>The <a href="metrics/ClientMetrics.html">ClientMetrics</a> instance used to expose measurements of its internal
behavior and of the server as seen from the driver side.</p>
<p>By default, a <a href="metrics/DefaultMetrics.html">DefaultMetrics</a> instance is used.
TODO: This field is currently not supported</p>
</div>







<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L139" target="_blank" rel="noopener">client.ts, line 139</a></p></div>






        
    

    
        </div>

## Methods

<div class="jsdoc-content">

        
            

    

    
    <h4 class="name" id="batch"><span class="type-signature"></span>batch<span class="signature">(queries, options, callback)</span><span class="type-signature"></span></h4>
    

    



<div class="description">
    <p>Executes batch of queries on an available connection to a host.</p>
<p>It returns a <code>Promise</code> when a <code>callback</code> is not provided.</p>
</div>









    <h5>Parameters:</h5>
    

<table class="params docutils align-default">
    <thead>
    <tr>
        
        <th>Name</th>
        

        <th>Type</th>

        

        

        <th class="last">Description</th>
    </tr>
    </thead>

    <tbody>
    

        <tr>
            
                <td class="name"><code>queries</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>The queries to execute as an Array of strings or as an array
of object containing the query and params.</p></td>
        </tr>

    

        <tr>
            
                <td class="name"><code>options</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>The query options.</p></td>
        </tr>

    

        <tr>
            
                <td class="name"><code>callback</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>Executes callback(err, result) when the batch was executed</p></td>
        </tr>

    
    </tbody>
</table>






<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L814" target="_blank" rel="noopener">client.ts, line 814</a></p></div>




















        
            

    

    
    <h4 class="name" id="connect"><span class="type-signature"></span>connect<span class="signature">(callback)</span><span class="type-signature"></span></h4>
    

    



<div class="description">
    <p>Attempts to connect to one of the <a href="globals.html#ClientOptions">contactPoints</a> and discovers the rest the nodes of the
cluster.</p>
<p>When the <a href="Client.html">Client</a> is already connected, it resolves immediately.</p>
<p>It returns a <code>Promise</code> when a <code>callback</code> is not provided.</p>
</div>









    <h5>Parameters:</h5>
    

<table class="params docutils align-default">
    <thead>
    <tr>
        
        <th>Name</th>
        

        <th>Type</th>

        

        

        <th class="last">Description</th>
    </tr>
    </thead>

    <tbody>
    

        <tr>
            
                <td class="name"><code>callback</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>The optional callback that is invoked when the pool is connected or it failed to
connect.</p></td>
        </tr>

    
    </tbody>
</table>






<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L262" target="_blank" rel="noopener">client.ts, line 262</a></p></div>



















    <h5>Example</h5>
    
        <p class="code-caption">Usage example</p>
    
    <div class="highlight-javascript notranslate"><div class="highlight"><pre>await client.connect();</pre></div></div>



        
            

    

    
    <h4 class="name" id="eachRow"><span class="type-signature"></span>eachRow<span class="signature">(query, params, options, rowCallback, callback)</span><span class="type-signature"></span></h4>
    

    



<div class="description">
    <p>Executes the query and calls <code>rowCallback</code> for each row as soon as they are received. Calls the final
<code>callback</code> after all rows have been sent, or when there is an error.</p>
<p>The query can be prepared (recommended) or not depending on the <a href="globals.html#QueryOptions"><code>prepare</code></a> flag.</p>
</div>









    <h5>Parameters:</h5>
    

<table class="params docutils align-default">
    <thead>
    <tr>
        
        <th>Name</th>
        

        <th>Type</th>

        

        

        <th class="last">Description</th>
    </tr>
    </thead>

    <tbody>
    

        <tr>
            
                <td class="name"><code>query</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>The query to execute</p></td>
        </tr>

    

        <tr>
            
                <td class="name"><code>params</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>Array of parameter values or an associative array (object) containing parameter names
as keys and its value.</p></td>
        </tr>

    

        <tr>
            
                <td class="name"><code>options</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>The query options.</p></td>
        </tr>

    

        <tr>
            
                <td class="name"><code>rowCallback</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>Executes <code>rowCallback(n, row)</code> per each row received, where n is the row
index and row is the current Row.</p></td>
        </tr>

    

        <tr>
            
                <td class="name"><code>callback</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>Executes <code>callback(err, result)</code> after all rows have been received.</p>
<p>When dealing with paged results, <a href="types/ResultSet.html#nextPage">ResultSet#nextPage()</a> method can be used
to retrieve the following page. In that case, <code>rowCallback()</code> will be again called for each row and
the final callback will be invoked when all rows in the following page has been retrieved.</p></td>
        </tr>

    
    </tbody>
</table>






<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L646" target="_blank" rel="noopener">client.ts, line 646</a></p></div>



















    <h5>Examples</h5>
    
        <p class="code-caption">Using per-row callback and arrow functions</p>
    
    <div class="highlight-javascript notranslate"><div class="highlight"><pre>client.eachRow(query, params, { prepare: true }, (n, row) => console.log(n, row), err => console.error(err));</pre></div></div>

        <p class="code-caption">Overloads</p>
    
    <div class="highlight-javascript notranslate"><div class="highlight"><pre>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);</pre></div></div>



        
            

    

    
    <h4 class="name" id="execute"><span class="type-signature"></span>execute<span class="signature">(query, params, options, callback)</span><span class="type-signature"></span></h4>
    

    



<div class="description">
    <p>Executes a query on an available connection.</p>
<p>The query can be prepared (recommended) or not depending on the <a href="globals.html#QueryOptions"><code>prepare</code></a> flag.</p>
<p>Some execution failures can be handled transparently by the driver, according to the
<a href="policies/retry/RetryPolicy.html"><code>RetryPolicy</code></a> or the
<a href="policies/speculativeExecution/index.html"><code>SpeculativeExecutionPolicy</code></a> used.</p>
<p>It returns a <code>Promise</code> when a <code>callback</code> is not provided.</p>
</div>









    <h5>Parameters:</h5>
    

<table class="params docutils align-default">
    <thead>
    <tr>
        
        <th>Name</th>
        

        <th>Type</th>

        

        

        <th class="last">Description</th>
    </tr>
    </thead>

    <tbody>
    

        <tr>
            
                <td class="name"><code>query</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>The query to execute.</p></td>
        </tr>

    

        <tr>
            
                <td class="name"><code>params</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>Array of parameter values or an associative array (object) containing parameter names
as keys and its value.</p></td>
        </tr>

    

        <tr>
            
                <td class="name"><code>options</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>The query options for the execution.</p></td>
        </tr>

    

        <tr>
            
                <td class="name"><code>callback</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>Executes callback(err, result) when execution completed. When not defined, the
method will return a promise.</p></td>
        </tr>

    
    </tbody>
</table>






<dl class="details">

    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    <dt class="tag-see">See:</dt>
    <dd class="tag-see">
        <ul>
            <li><a href="ExecutionProfile.html">ExecutionProfile</a> to reuse a set of options across different query executions.</li>
        </ul>
    </dd>
    

    
</dl><div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L385" target="_blank" rel="noopener">client.ts, line 385</a></p></div>



















    <h5>Examples</h5>
    
        <p class="code-caption">Promise-based API, using async/await</p>
    
    <div class="highlight-javascript notranslate"><div class="highlight"><pre>const query = 'SELECT name, email FROM users WHERE id = ?';
const result = await client.execute(query, [ id ], { prepare: true });
const row = result.first();
console.log('%s: %s', row['name'], row['email']);</pre></div></div>

        <p class="code-caption">Callback-based API</p>
    
    <div class="highlight-javascript notranslate"><div class="highlight"><pre>const query = 'SELECT name, email FROM users WHERE id = ?';
client.execute(query, [ id ], { prepare: true }, function (err, result) {
  assert.ifError(err);
  const row = result.first();
  console.log('%s: %s', row['name'], row['email']);
});</pre></div></div>



        
            

    

    
    <h4 class="name" id="executeGraph"><span class="type-signature"></span>executeGraph<span class="signature">()</span><span class="type-signature"></span></h4>
    

    















<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L615" target="_blank" rel="noopener">client.ts, line 615</a></p></div>
<div class="admonition warning"><p class="admonition-title">Deprecated</p><p>Not supported by the driver. Usage will throw an error.</p></div>




















        
            

    

    
    <h4 class="name" id="getReplicas"><span class="type-signature"></span>getReplicas<span class="signature">()</span><span class="type-signature"></span></h4>
    

    



<div class="description">
    <p>Gets the host that are replicas of a given token.</p>
</div>













<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L914" target="_blank" rel="noopener">client.ts, line 914</a></p></div>




















        
            

    

    
    <h4 class="name" id="getState"><span class="type-signature"></span>getState<span class="signature">()</span><span class="type-signature"></span></h4>
    

    















<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L925" target="_blank" rel="noopener">client.ts, line 925</a></p></div>
<div class="admonition warning"><p class="admonition-title">Deprecated</p><p>This is not planned feature for the driver. Currently this remains in place, but returns Client state with
no information. This endpoint may be removed at any point.</p></div>















<h5>Returns:</h5>

        
<div class="param-desc">
    <p>A dummy <a href="metadata/ClientState.html"><code>ClientState</code></a> instance.</p>
</div>



    





        
            

    

    
    <h4 class="name" id="shutdown"><span class="type-signature"></span>shutdown<span class="signature">(callback)</span><span class="type-signature"></span></h4>
    

    



<div class="description">
    <p>The only effect of calling shutdown is rejecting any following queries to the database.</p>
<p>It returns a <code>Promise</code> when a <code>callback</code> is not provided.</p>
</div>









    <h5>Parameters:</h5>
    

<table class="params docutils align-default">
    <thead>
    <tr>
        
        <th>Name</th>
        

        <th>Type</th>

        

        

        <th class="last">Description</th>
    </tr>
    </thead>

    <tbody>
    

        <tr>
            
                <td class="name"><code>callback</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>Optional callback to be invoked when finished closing all connections.</p></td>
        </tr>

    
    </tbody>
</table>






<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L941" target="_blank" rel="noopener">client.ts, line 941</a></p></div>
<div class="admonition warning"><p class="admonition-title">Deprecated</p><p>Explicit connection shutdown is deprecated and may be removed in the future.
Drop this client to close the connection to the database.</p></div>




















        
            

    

    
    <h4 class="name" id="stream"><span class="type-signature"></span>stream<span class="signature">(query, params, options, callback)</span><span class="type-signature"></span></h4>
    

    



<div class="description">
    <p>Executes the query and pushes the rows to the result stream as soon as they received.</p>
<p>The stream is a <a href="https://nodejs.org/api/stream.html#stream_class_stream_readable">ReadableStream</a> object
that emits rows.
It can be piped downstream and provides automatic pause/resume logic (it buffers when not read).</p>
<p>The query can be prepared (recommended) or not depending on <a href="globals.html#QueryOptions">QueryOptions</a>.prepare flag. Retries on multiple
hosts if needed.</p>
</div>









    <h5>Parameters:</h5>
    

<table class="params docutils align-default">
    <thead>
    <tr>
        
        <th>Name</th>
        

        <th>Type</th>

        

        

        <th class="last">Description</th>
    </tr>
    </thead>

    <tbody>
    

        <tr>
            
                <td class="name"><code>query</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>The query to prepare and execute.</p></td>
        </tr>

    

        <tr>
            
                <td class="name"><code>params</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>Array of parameter values or an associative array (object) containing parameter names
as keys and its value</p></td>
        </tr>

    

        <tr>
            
                <td class="name"><code>options</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>The query options.</p></td>
        </tr>

    

        <tr>
            
                <td class="name"><code>callback</code></td>
            

            <td class="type">
            
            </td>

            

            

            <td class="description last"><p>executes callback(err) after all rows have been received or if there is an error</p></td>
        </tr>

    
    </tbody>
</table>






<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L750" target="_blank" rel="noopener">client.ts, line 750</a></p></div>




















        
    

    

    
        </div>

## Events

<div class="jsdoc-content">

        
            

    

    
    <h4 class="name" id="event:hostAdd">hostAdd</h4>
    

    



<div class="description">
    <p>Emitted when a new host is added to the cluster.</p>
<ul>
<li><a href="Host.html">Host</a> The host being added.</li>
</ul>
</div>













<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L169" target="_blank" rel="noopener">client.ts, line 169</a></p></div>




















        
            

    

    
    <h4 class="name" id="event:hostDown">hostDown</h4>
    

    



<div class="description">
    <p>Emitted when a host in the cluster changed status from up to down.</p>
<ul>
<li><a href="Host.html">host</a> The host that changed the status.</li>
</ul>
</div>













<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L184" target="_blank" rel="noopener">client.ts, line 184</a></p></div>




















        
            

    

    
    <h4 class="name" id="event:hostRemove">hostRemove</h4>
    

    



<div class="description">
    <p>Emitted when a host is removed from the cluster</p>
<ul>
<li><a href="Host.html">Host</a> The host being removed.</li>
</ul>
</div>













<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L174" target="_blank" rel="noopener">client.ts, line 174</a></p></div>




















        
            

    

    
    <h4 class="name" id="event:hostUp">hostUp</h4>
    

    



<div class="description">
    <p>Emitted when a host in the cluster changed status from down to up.</p>
<ul>
<li><a href="Host.html">host</a> The host that changed the status.</li>
</ul>
</div>













<div class="admonition note"><p class="admonition-title">Source</p><p><a href="https://github.com/scylladb/nodejs-rs-driver/blob/v0.6.1/lib/client.ts#L179" target="_blank" rel="noopener">client.ts, line 179</a></p></div></div>
