Get a database object (TypeScript)
When using a client, most Data API interactions require you to connect to a database through a DataAPIClient object.
Connecting to a database is an essential component of most Data API client scripts. For more information, see Get started with the Data API (TypeScript).
For more information about the Data API client hierarchy, see Python client internals, TypeScript client internals, Go client internals, Java client internals, and C# client internals.
Parameters
Use the db method, which belongs to the DataAPIClient class.
Method signature
db(
endpoint: string,
options?: {
logging?: LoggingConfig,
keyspace?: string | null,
token?: string | TokenProvider | null,
dataApiPath?: string,
serdes?: DbSerDesConfig,
timeoutDefaults?: TimeoutDescriptor,
}
): Db
| Name | Type | Summary |
|---|---|---|
|
|
A database API endpoint URL. |
|
Optional.
The options to use for the database.
See Properties of |
| Name | Type | Summary |
|---|---|---|
|
Optional.
Specify the working keyspace.
This keyspace is used for all downstream operations unless you later override it with a method such as If you don’t set this, you must specify the keyspace in a downstream method before performing an operation that requires a keyspace. |
|
|
Optional.
Whether to monitor commands through Default: false. |
|
|
Optional. An application token, or an instance of The username and password used to generate the token must be tied to a role that has sufficient permissions to perform the desired operations. If you specified a token when instantiating the |
|
|
Optional. The path to the Data API. Default: |
Examples
The following examples demonstrate how to connect to a database.
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
Client reference
For more information, see the client reference.