Connect with the TypeScript client
Learn how to connect to a HCD database with the astra-db-ts client.
Prerequisites
-
You have installed a cluster.
-
You have created a keyspace.
-
You have installed Node 14 or higher.
Install the TypeScript client
-
Verify that Node is version 14 or higher.
node --version
-
Use npm or Yarn to install the client.
-
npm
-
Yarn
To install the TypeScript client with npm:
npm install @datastax/astra-db-ts
To install the TypeScript client with Yarn:
-
Verify that Yarn is version 2.0 or higher.
yarn --version
-
Install the
astra-db-ts
package.yarn add @datastax/astra-db-ts
-
Connect to a vector-enabled Hyper-Converged Database (HCD) database
Create a file named quickstart.ts
.
import { DataAPIClient, UsernamePasswordTokenProvider, VectorDoc, UUID } from '@datastax/astra-db-ts';
// Database settings
const DB_USERNAME = "cassandra";
const DB_PASSWORD = "cassandra";
const DB_API_ENDPOINT = "http://localhost:8181";
const DB_ENVIRONMENT = "hcd";
const DB_NAMESPACE = "cycling";
// Database settings if you exported them as environment variables
// const DB_USERNAME = process.env.DB_USERNAME;
// const DB_PASSWORD = process.env.DB_PASSWORD;
// const DB_API_ENDPOINT = process.env.DB_API_ENDPOINT;
// OpenAI settings
const OPEN_AI_PROVIDER = "openai";
const OPENAI_API_KEY = process.env.OPENAI_API_KEY
const MODEL_NAME = "text-embedding-3-small";
// Build a token in the required format
const tp = new UsernamePasswordTokenProvider(DB_USERNAME, DB_PASSWORD);
// Initialize the client and get a "Db" object
const client = new DataAPIClient({ environment: DB_ENVIRONMENT });
const db = client.db(DB_API_ENDPOINT, { token: tp });
const dbAdmin = db.admin({ environment: DB_ENVIRONMENT });
See also
-
See a complete TypeScript example of how to connect to a database, load data into a collection, and perform a similarity search.
-
Read the TypeScript client reference for more details about available methods.
-
Also see the TypeScript tab for details and examples in each task-based section of the API Reference topics: