Constructs a new instance of the DataAPIClient without a default token. The token will instead need to
be specified when calling .db()
or .admin()
.
Prefer this method 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()
.
Prefer this method 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).
Note: this method is only available for Astra databases.
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.
The client will be no longer usable after this method is called.
This method is idempotent and can be called multiple times without issue.
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();
For most users, this method isn't 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 they're used for the duration of the program, but it's there for when you need it.
Spawns a new Db instance using a direct endpoint and given options.
This method does not validate the existence of the database—it simply creates a reference.**
Note that this does not perform any I/O or validation on if the endpoint is valid or not. It's up to the user to ensure that the endpoint is correct. If you want to create an actual database, see AstraAdmin.createDatabase instead.
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.
Spawning a db via just an ID and region is no longer supported in astra-db-ts 2.0+
. Use the buildAstraEndpoint to create the endpoint if you need to.
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('https://<db_id>-<region>.apps.astra.datastax.com');
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',
});
Note that this does not perform any IO or validation on if the endpoint is valid or not. It's up to the user to ensure that the endpoint is correct. If you want to create an actual database, see AstraAdmin.createDatabase instead.
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.
Currently, if you're not using Astra, you need to specify the environment when:
Example
See