Constructs a new instance of the DataAPIClient without a default token. The token will instead need to
be specified when calling .db()
or .admin()
.
💡Tip: Prefer this overload when using a db-scoped token instead of a more universal token.
Optional
options: DataAPIClientOptionsThe default options to use when spawning new instances of Db or AstraAdmin.
const client = new DataAPIClient();
// OK
const db1 = client.db('<endpoint>', { token: '<token>' });
// Will throw error as no token is ever provided
const db2 = client.db('<endpoint>');
Constructs a new instance of the DataAPIClient with a default token. This token will be used everywhere
if no overriding token is provided in .db()
or .admin()
.
💡Tip: Prefer this overload when using a universal/admin-scoped token.
The default token to use when spawning new instances of Db or AstraAdmin.
Optional
options: DataAPIClientOptionsThe default options to use when spawning new instances of Db or AstraAdmin.
const client = new DataAPIClient('<default_token>');
// OK
const db1 = client.db('<endpoint>', { token: '<weaker_token>' });
// OK; will use <default_token>
const db2 = client.db('<endpoint>');
Private
Readonly
#optionsInternal
internalSpawns a new AstraAdmin instance using the given options to work with the DevOps API (for admin work such as creating/managing databases).
⚠️Warning: This method is only available for Astra databases. If you try to use it with a non-Astra database, it will throw an error.
Optional
options: AdminOptionsAny options to override the default options set when creating the DataAPIClient.
A new AstraAdmin instance.
const admin1 = client.admin();
const admin2 = client.admin({ adminToken: '<stronger_token>' });
const dbs = await admin1.listDatabases();
console.log(dbs);
The options for the AstraAdmin instance are a deep merge of the options for the DataAPIClient and the options for the AstraAdmin itself.
Closes the client and disconnects all underlying connections. This should be called when the client is no longer needed to free up resources.
🚨Important: The client will be no longer usable after this method is called.
A promise that resolves when the client has been closed.
const client = new DataAPIClient(...);
await client.close();
// Error: Can't make requests on a closed client
const coll = client.db(...).collection(...);
await coll.findOne();
This method is idempotent and can be called multiple times without issue.
For most users, this method is not necessary to call, as resources will be freed up when the server is shut down or the process is killed.
However, it's useful in long-running processes or when you want to free up resources immediately.
Think of it as using malloc or using a file descriptor. Freeing them isn't strictly necessary when the resources are used for the duration of the program, but it's there for when you need it.
The direct endpoint to use.
Optional
options: DbOptionsAny options to override the default options set when creating the DataAPIClient.
A new Db instance.
const db = client.db('http://localhost:8181');
const db = client.db('https://<db_id>-<region>.apps.astra.datastax.com', {
keyspace: 'my_keyspace',
token: 'AstraCS:...',
});
🚨Important: This does not verify the existence of the database—it only creates a reference.
Use AstraAdmin.createDatabase to create a new database if you need to.
It is on the user to ensure that the database endpoint is correct.
This endpoint should include the protocol and the hostname, but not the path.
If you're using Astra, this will typically be of the form https://<db_id>-<region>.apps.astra.datastax.com
(the exception being private endpoints); any other database may have a completely unique domain.
⚠️Warning: Spawning a db using an ID and region is no longer supported in
astra-db-ts v2.0+
.Use the buildAstraEndpoint to create the endpoint if you need to.
The options for the Db instance are a deep merge of the options for the DataAPIClient and the options for the Db itself.
Any options provided to DbOptions may generally also be overridden in any spawned classes' options (e.g. CollectionOptions).
const db = client.db('https://<db_id>-<region>.apps.astra.datastax.com', {
keyspace: 'my_keyspace',
token: 'AstraCS:...',
});
const coll = db.collection('my_coll', {
keyspace: 'other_keyspace',
});
Unsubscribe from an event.
The event to unsubscribe from.
The listener to remove.
Subscribe to an event.
The event to listen for.
The callback to invoke when the event is emitted.
A function to unsubscribe the listener.
Subscribe to an event once.
The listener will be automatically unsubscribed after the first time it is called.
Note that the listener will be unsubscribed BEFORE the actual listener callback is invoked.
The event to listen for.
The callback to invoke when the event is emitted.
A function to prematurely unsubscribe the listener.
Overview
The main entrypoint into working with the Data API. It sits at the top of the conceptual hierarchy of the SDK.
The client may take in a default token, which can be overridden by a stronger/weaker token when spawning a new Db or AstraAdmin instance.
It also takes in a set of default options (see DataAPIClientOptions) that may also generally be overridden in lower classes.
Example
The options hierarchy
Like the class hierarchy aforementioned, the options for each class also form an adjacent hierarchy.
The options for any class are a deep merge of the options for the class itself and the options for its parent classes.
For example, you may set default CollectionOptions.logging options in DataAPIClientOptions.logging, and override them in the CollectionOptions themselves as desired.
Example
Non-Astra support (DSE, HCD, etc.)
Depending on the Data API backend used, you may need to set the environment option in certain places to "dse", "hcd", etc.
See DataAPIEnvironment for all possible backends; it defaults to "astra" if not specified.
Example
See