Connect the Node.js driver to your database
DataStax recommends using the TypeScript client with HCD databases. Use the Node.js driver only if you are working with an existing application that previously used a CQL-based driver or if you plan to explicitly use CQL. |
Initialize the Node.js driver to connect to your database.
Prerequisite
-
You have installed a cluster.
-
You have created a keyspace.
-
You have installed NodeJS.
HCD APIs use the term keyspace to refer to both namespaces and keyspaces. |
Connect
This code configures the Node.js driver with authentication details, then establishes a connection to the database through an asynchronous run function.
const cassandra = require('cassandra-driver');
const authProvider = new cassandra.auth.PlainTextAuthProvider('token', process.env['DB_APPLICATION_TOKEN']);
const client = new cassandra.Client({ cloud, authProvider });
async function run() {
await client.connect();
// ...
}
See the Node.js quickstart for details on how to install the package, connect to a HCD database, create a table and vector-compatible SAI (Storage-Attached Index), load data, and perform a similarity search.