DataStax Node.js driver

Before using the DataStax driver, review the Best practices for DataStax drivers to understand the rules and recommendations for improving performance and minimizing resource utilization in applications that use a DataStax driver.

DataStax recommends using the unified DataStax driver. See this blog post for more information. You can also use the DataStax Enterprise (DSE) drivers, which exposes the same API for connecting to Cassandra databases.

Prerequisites

  • Download and install a current Node.js LTS version with npm.

  • Client ID and Client Secret by creating your application token for your username and password.

Working with secure connect bundle

Downloading secure connect bundle

To connect to your Astra DB database using the drivers, download the secure database bundle from the DataStax Astra DB console that contains the connection credentials.

  1. Open a browser, navigate to Astra DB, and log in.

  1. From your Dashboard page, select your database.

  1. On the Overview page, select Connect.

  1. In the Connect using a driver section, click your preferred language from the list to load language-specific instructions.

    If you have multiple regions, select the region you want to connect to from the dropdown menu for instructions.

Note, though, that the bundle URL is the same for all languages.

  1. Click Download Bundle.

    If you have multiple regions, you will have the option to download a bundle for each region from the expanded Download Bundle drop-down menu.

If you’ve enabled VPC peering, you can also Download External Secure Connect Bundle for use within your VPC peering. The secure-connect-database_name.zip file downloads, which contains the security certificates and credentials for your database.

VPC peering is only available on Classic databases.

Alternatively, you can right-click the Download credentials link, copy the link source, and then use a curl command to download the secure-connect-database_name.zip file:

curl -L "<secure-connect-database_name-download-URL>" > database_name.zip

Sharing secure connect bundle

Although teammates can access your Astra DB database, it will not display in their list of available databases under My Databases in the Astra DB console.

After you create an Astra DB database, you can grant access to other members of your team by providing them with the database credentials and connection details for your database.

Be careful when sharing connection details. Providing this information to another user grants them access to your Astra DB database and ownership capabilities, such as making modifications to the database.

For security, delete downloaded connection credentials after sending them to your teammate.

Connecting Node.js driver

  1. Install the DataStax Node.js driver:

    npm install cassandra-driver
  2. Create a connect-database.js file in the main directory for your Node.js project.

    cd nodejsProject
    touch connect-database.js
  3. Copy the following connection code into the connect-database.js file.

    Include the absolute path to the secure connect bundle for your Cassandra database (secure-connect-database_name.zip) in the secureConnectBundle parameter, as shown in the following examples.

    'use strict'
    const { Client } = require('cassandra-driver');
  4. After the Client class, add the following code to connect-database.js. This code creates a Client instance to connect to your Astra DB database, runs a CQL query, and prints the output to the console.

    async function run() {
      const client = new Client({
        cloud: { secureConnectBundle: 'path/to/secure-connect-database_name.zip' },
        credentials: { username: 'clientId', password: 'clientSecret' }
      });
    
      await client.connect();
    
      // Execute a query
      const rs = await client.execute('SELECT * FROM system.local');
      console.log(`Hello from cluster: ${rs.first()['cluster_name']}`);
    
      await client.shutdown();
    }
    
    // Run the async function
    run();
  5. Save and close the connect-database.js file.

  6. Run the connect-database.js example with the Node.js runtime:

    node connect-database.js

The console output displays the cluster_name value from the system.local table.

Migrating Node.js driver

Complete the following procedure to migrate your existing DataStax Node.js driver to a version that is capable of connecting to Astra DB databases created using DataStax Astra DB.

  1. Install the DataStax Node.js driver:

DataStax Node.js driver for Apache Cassandra

npm install cassandra-driver

DSE driver

npm install dse-driver
  1. In your existing DataStax Node.js driver code, modify the connection code to use the Astra DB API. Include the absolute path to the secure connect bundle for your Cassandra database (secure-connect-database_name.zip) in the secureConnectBundle parameter, as shown in the following examples.

DataStax Node.js driver for Apache Cassandra

const { Client } = require('cassandra-driver');
const client = new Client({
    cloud: { secureConnectBundle: 'path/to/secure-connect-database_name.zip' },
    credentials: { username: 'clientId', password: 'clientSecret' }
  });

DSE Node.js driver

const dse = require('dse-driver');

const client = new Client({
    cloud: { secureConnectBundle: 'path/to/secure-connect-database_name.zip' },
    credentials: { username: 'clientId', password: 'clientSecret' }
  });
  1. Run your Node.js script to connect to your Astra DB database:

node my_node.js_driver.js

If successful, the code builds, compiles, and connects to your Cassandra database.