DataStax Astra Developer Hub

Welcome to the DataStax Astra Developer Hub. You'll find comprehensive guides and documentation to help you start working with DataStax Astra as quickly as possible. Use the included APIs to create, modify, and terminate databases, and interact with the databases you create. Let's do it!

Astra Docs      API Reference      CQL for Astra

DataStax Node.js driver

Use the unified DataStax Node.js driver to connect to your DataStax Astra database and begin building your own application.

๐Ÿ“˜

Tip

DataStax recommends using the unified DataStax Node.js driver. If you have an existing Apache Cassandra or DataStax Enterprise (DSE) Node.js driver, migrate the driver to a version that is capable of connecting to Astra databases.

Prerequisites

  1. Download the secure connect bundle to obtain connection credentials for your DataStax Astra databasedatabase - A group of distributed instances for storing data. Each paid Astra database has at least three instances..
  2. Download and install a current Node.js LTS version with npm.

๐Ÿ“˜

Tip

Alternatively, have a teammate provide access to their Astra database.

Procedure

  1. Install the DataStax Node.js driver:
npm install cassandra-driver
  1. Create a connect-database.js file in the main directory for your Node.js project.
cd nodejsProject
touch connect-database.js
  1. Copy the following connection code into the connect-database.js file.

๐Ÿ“˜

Note

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');
  1. After the Client class, add the following code to connect-database.js. This code creates a Client instance to connect to your Astra 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: 'username', password: 'password' }
  });

  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();
  1. Save and close the connect-database.js file.
  2. 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 tabletable - Stores data based on a primary key, which consists of a partition key and optional clustering columns. A partition key defines the node on which the data is stored, and divides data into logical groups. Define partition keys that evenly distribute the data and also satisfy specific queries. Query and write requests across multiple partitions should be avoided if possible. A clustering column defines the sort order of rows within a partition. When defining a clustering column, consider the purpose of the data. For example, retrieving the most recent transactions, sorted by date, in descending order..

Updated 17 days ago



DataStax Node.js driver


Use the unified DataStax Node.js driver to connect to your DataStax Astra database and begin building your own application.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.