Skip to content

Commit 622d654

Browse files
committed
some docs work
1 parent b2b79c1 commit 622d654

File tree

23 files changed

+1348
-252
lines changed

23 files changed

+1348
-252
lines changed

examples/logging/src/01-basic-usage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DataAPIClient } from '@datastax/astra-db-ts';
77
// -----===<{ STEP 1: Instantiate the client }>===-----
88

99
// Instantiate the client & db with logging defaults enabled
10-
// See the documentation for `DataAPILoggingConfig` for more information on the available options
10+
// See the documentation for `LoggingConfig` for more information on the available options
1111
// (or hover over the `logging` property in your IDE!)
1212
const client = new DataAPIClient({ logging: 'all' });
1313
const db = client.db(process.env.ASTRA_DB_ENDPOINT!, { token: process.env.ASTRA_DB_TOKEN });

src/administration/astra-admin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export class AstraAdmin extends HierarchicalLogger<AdminCommandEventMap> {
121121
* const db1 = admin.db('https://<db_id>-<region>.apps.astra.datastax.com');
122122
*
123123
* const db2 = admin.db('https://<db_id>-<region>.apps.astra.datastax.com', {
124-
*   keyspace: 'my-keyspace',
124+
*   keyspace: 'my_keyspace',
125125
*   useHttp2: false,
126126
* });
127127
* ```
@@ -154,7 +154,7 @@ export class AstraAdmin extends HierarchicalLogger<AdminCommandEventMap> {
154154
* const db1 = admin.db('a6a1d8d6-31bc-4af8-be57-377566f345bf', 'us-east1');
155155
*
156156
* const db2 = admin.db('a6a1d8d6-31bc-4af8-be57-377566f345bf', 'us-east1', {
157-
*   keyspace: 'my-keyspace',
157+
*   keyspace: 'my_keyspace',
158158
*   useHttp2: false,
159159
* });
160160
* ```
@@ -197,7 +197,7 @@ export class AstraAdmin extends HierarchicalLogger<AdminCommandEventMap> {
197197
* const dbAdmin1 = admin.dbAdmin('https://<db_id>-<region>...');
198198
*
199199
* const dbAdmin2 = admin.dbAdmin('https://<db_id>-<region>...', {
200-
*   keyspace: 'my-keyspace',
200+
*   keyspace: 'my_keyspace',
201201
*   useHttp2: false,
202202
* });
203203
* ```
@@ -233,7 +233,7 @@ export class AstraAdmin extends HierarchicalLogger<AdminCommandEventMap> {
233233
* const dbAdmin1 = admin.dbAdmin('a6a1d8d6-...-377566f345bf', 'us-east1');
234234
*
235235
* const dbAdmin2 = admin.dbAdmin('a6a1d8d6-...-377566f345bf', 'us-east1', {
236-
*   keyspace: 'my-keyspace',
236+
*   keyspace: 'my_keyspace',
237237
*   useHttp2: false,
238238
* });
239239
* ```

src/administration/astra-db-admin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class AstraDbAdmin extends DbAdmin {
116116
* @example
117117
* ```typescript
118118
* const dbAdmin = client.admin().dbAdmin('<endpoint>', {
119-
*   keyspace: 'my-keyspace',
119+
*   keyspace: 'my_keyspace',
120120
*   useHttp2: false,
121121
* });
122122
*

src/administration/data-api-db-admin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class DataAPIDbAdmin extends DbAdmin {
8585
* @example
8686
* ```typescript
8787
* const dbAdmin = client.admin().dbAdmin('<endpoint>', {
88-
*   keyspace: 'my-keyspace',
88+
*   keyspace: 'my_keyspace',
8989
*   useHttp2: false,
9090
* });
9191
*

src/administration/db-admin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export abstract class DbAdmin extends HierarchicalLogger<AdminCommandEventMap> {
3939
* @example
4040
* ```typescript
4141
* const dbAdmin = client.admin().dbAdmin('<endpoint>', {
42-
*   keyspace: 'my-keyspace',
42+
*   keyspace: 'my_keyspace',
4343
*   useHttp2: false,
4444
* });
4545
*

src/administration/events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { DevOpsAPIRequestInfo } from '@/src/lib/api/clients/devops-api-http
1717
import type { DataAPIWarningDescriptor } from '@/src/documents/index.js';
1818
import { BaseClientEvent } from '@/src/lib/logging/base-event.js';
1919
import type { TimeoutDescriptor } from '@/src/lib/api/timeouts/timeouts.js';
20-
import { NonEmpty, ReadonlyNonEmpty } from '@/src/lib/index.js';
20+
import type { NonEmpty, ReadonlyNonEmpty } from '@/src/lib/index.js';
2121

2222
/**
2323
* The events emitted by the {@link DataAPIClient}. These events are emitted at various stages of the

src/client/data-api-client.ts

Lines changed: 127 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,49 +30,87 @@ import { InternalLogger } from '@/src/lib/logging/internal-logger.js';
3030
* ##### Overview
3131
*
3232
* The main entrypoint into working with the Data API. It sits at the top of the
33-
* [conceptual hierarchy](https://github.com/datastax/astra-db-ts/tree/signature-cleanup?tab=readme-ov-file#abstraction-diagram)
33+
* [conceptual hierarchy](https://github.com/datastax/astra-db-ts/tree/b2b79c15a388d2373e884e8921530d81f3593431?tab=readme-ov-file#high-level-architecture)
3434
* of the SDK.
3535
*
3636
* The client may take in a default token, which can be overridden by a stronger/weaker token when spawning a new
3737
* {@link Db} or {@link AstraAdmin} instance.
3838
*
39-
* It also takes in a set of default options (see {@link DataAPIClientOptions}) that may also generally be overridden as necessary.
40-
*
41-
* **Depending on the Data API backend used, you may need to set the environment option to "dse", "hcd", etc.** See
42-
* {@link DataAPIEnvironment} for all possible backends. It defaults to "astra".
39+
* It also takes in a set of default options (see {@link DataAPIClientOptions}) that may also generally be overridden in lower classes.
4340
*
4441
* @example
4542
* ```typescript
4643
* // Client with default token
47-
* const client1 = new DataAPIClient('AstraCS:...');
44+
* const client = new DataAPIClient('<token>');
45+
* const db = client.db('https://<db_id>-<region>.apps.astra.datastax.com');
4846
*
4947
* // Client with no default token; must provide token in .db() or .admin()
50-
* const client2 = new DataAPIClient();
51-
*
52-
* // Client connecting to a local DSE instance
53-
* const dseToken = new UsernamePasswordTokenProvider('username', 'password');
54-
* const client3 = new DataAPIClient(dseToken, { environment: 'dse' });
48+
* const client = new DataAPIClient();
49+
* const db = client.db('https://<db_id>-<region>.apps.astra.datastax.com', { token });
5550
*
56-
* const db1 = client1.db('https://<db_id>-<region>.apps.astra.datastax.com');
57-
* const db2 = client1.db('<db_id>', '<region>');
51+
* const coll = await db.collection('my_collection');
5852
*
59-
* const coll = await db1.collections('my-collections');
60-
*
61-
* const admin1 = client1.admin();
62-
* const admin2 = client1.admin({ adminToken: '<stronger_token>' });
53+
* const admin1 = client.admin();
54+
* const admin2 = client.admin({ adminToken: '<stronger_token>' });
6355
*
6456
* console.log(await coll.insertOne({ name: 'John Joe' }));
6557
* console.log(await admin1.listDatabases());
6658
* ```
6759
*
60+
* ---
61+
*
62+
* ##### The options hierarchy
63+
*
64+
* Like the class hierarchy aforementioned, the options for each class also form an [adjacent hierarchy](https://github.com/datastax/astra-db-ts/tree/b2b79c15a388d2373e884e8921530d81f3593431?tab=readme-ov-file#options-hierarchy).
65+
*
66+
* The options for any class are a deep merge of the options for the class itself and the options for its parent classes.
67+
*
68+
* For example, you may set default {@link CollectionOptions.logging} options in {@link DataAPIClientOptions.logging}, and override them in the {@link CollectionOptions} themselves as desired.
69+
*
70+
* @example
71+
* ```ts
72+
* const client = new DataAPIClient({
73+
* logging: [{ events: 'all', emits: 'event' }],
74+
* });
75+
* const db = client.db('https://<db_id>-<region>.apps.astra.datastax.com', { token });
76+
*
77+
* // Everything will still be emitted as an event,
78+
* // But now, `commandFailed` events from this collection will also log to stderr
79+
* const coll = db.collection('<name>', {
80+
* logging: [{ events: 'commandFailed', emits: ['event', 'stderr'] }],
81+
* });
82+
* ```
83+
*
84+
* ---
85+
*
86+
* ##### Non-Astra support (DSE, HCD, etc.)
87+
*
88+
* Depending on the Data API backend used, you may need to set the environment option in certain places to "dse", "hcd", etc.
89+
*
90+
* See {@link DataAPIEnvironment} for all possible backends; it defaults to "astra" if not specified.
91+
*
92+
* Currently, if you're not using Astra, you need to specify the environment when:
93+
* - Creating the {@link DataAPIClient}
94+
* - Using {@link Db.admin}
95+
*
96+
* @example
97+
* ```ts
98+
* // Client connecting to a local DSE instance
99+
* const dseToken = new UsernamePasswordTokenProvider('username', 'password');
100+
* const client = new DataAPIClient(dseToken, { environment: 'dse' });
101+
* ```
102+
*
68103
* @public
69104
*
70105
* @see DataAPIEnvironment
106+
* @see DataAPIClientOptions
71107
*/
72108
export class DataAPIClient extends HierarchicalLogger<DataAPIClientEventMap> {
73109
readonly #options: ParsedRootClientOpts;
74110

75111
/**
112+
* ##### Overview
113+
*
76114
* Constructs a new instance of the {@link DataAPIClient} without a default token. The token will instead need to
77115
* be specified when calling `.db()` or `.admin()`.
78116
*
@@ -83,17 +121,19 @@ export class DataAPIClient extends HierarchicalLogger<DataAPIClientEventMap> {
83121
* const client = new DataAPIClient();
84122
*
85123
* // OK
86-
* const db1 = client.db('<db_id>', '<region>', { token: 'AstraCS:...' });
124+
* const db1 = client.db('<endpoint>', { token: '<token>' });
87125
*
88126
* // Will throw error as no token is ever provided
89-
* const db2 = client.db('<db_id>', '<region>');
127+
* const db2 = client.db('<endpoint>');
90128
* ```
91129
*
92130
* @param options - The default options to use when spawning new instances of {@link Db} or {@link AstraAdmin}.
93131
*/
94132
constructor(options?: DataAPIClientOptions)
95133

96134
/**
135+
* ##### Overview
136+
*
97137
* Constructs a new instance of the {@link DataAPIClient} with a default token. This token will be used everywhere
98138
* if no overriding token is provided in `.db()` or `.admin()`.
99139
*
@@ -104,10 +144,10 @@ export class DataAPIClient extends HierarchicalLogger<DataAPIClientEventMap> {
104144
* const client = new DataAPIClient('<default_token>');
105145
*
106146
* // OK
107-
* const db1 = client.db('<db_id>', '<region>', { token: '<weaker_token>' });
147+
* const db1 = client.db('<endpoint>', { token: '<weaker_token>' });
108148
*
109149
* // OK; will use <default_token>
110-
* const db2 = client.db('<db_id>', '<region>');
150+
* const db2 = client.db('<endpoint>');
111151
* ```
112152
*
113153
* @param token - The default token to use when spawning new instances of {@link Db} or {@link AstraAdmin}.
@@ -138,28 +178,52 @@ export class DataAPIClient extends HierarchicalLogger<DataAPIClientEventMap> {
138178
}
139179

140180
/**
181+
* ##### Overview
182+
*
141183
* Spawns a new {@link Db} instance using a direct endpoint and given options.
142184
*
143-
* **NB. This method does not validate the existence of the database—it simply creates a reference.**
185+
* ---
144186
*
145-
* This endpoint should include the protocol and the hostname, but not the path. It's typically in the form of
146-
* `https://<db_id>-<region>.apps.astra.datastax.com`, but it can be used with DSE or any other Data-API-compatible
147-
* endpoint.
187+
* ##### Disclaimer
148188
*
149-
* The given options will override any default options set when creating the {@link DataAPIClient} through
150-
* a deep merge (i.e. unset properties in the options object will just default to the default options).
189+
* This method does *not* validate the existence of the database—it simply creates a reference.**
190+
*
191+
* 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
192+
* ensure that the endpoint is correct. If you want to create an actual database, see {@link AstraAdmin.createDatabase}
193+
* instead.
194+
*
195+
* ---
196+
*
197+
* ##### The API endpoint
198+
*
199+
* This endpoint should include the protocol and the hostname, but not the path.
200+
*
201+
* 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.
202+
*
203+
* Spawning a db via just an ID and region is no longer supported in `astra-db-ts 2.0+`. Use the {@link buildAstraEndpoint} to create the endpoint if you need to.
151204
*
152205
* @example
153-
* ```typescript
154-
* const db1 = client.db('https://<db_id>-<region>.apps.astra.datastax.com');
206+
* ```ts
207+
* const db = client.db('https://<db_id>-<region>.apps.astra.datastax.com');
208+
* ```
209+
*
210+
* ---
211+
*
212+
* ##### The options hierarchy
155213
*
156-
* const db2 = client.db('https://<db_id>-<region>.apps.astra.datastax.com', {
157-
*   keyspace: 'my-keyspace',
158-
*   useHttp2: false,
214+
* The options for the {@link Db} instance are a deep merge of the options for the {@link DataAPIClient} and the options for the {@link Db} itself.
215+
*
216+
* Any options provided to {@link DbOptions} may generally also be overridden in any spawned classes' options (e.g. {@link CollectionOptions}).
217+
*
218+
* @example
219+
* ```typescript
220+
* const db = client.db('https://<db_id>-<region>.apps.astra.datastax.com', {
221+
* keyspace: 'my_keyspace',
222+
* token: 'AstraCS:...',
159223
* });
160224
*
161-
* const db3 = client.db('https://<db_id>-<region>.apps.astra.datastax.com', {
162-
*   token: 'AstraCS:...'
225+
* const coll = db.collection('my_coll', {
226+
* keyspace: 'other_keyspace',
163227
* });
164228
* ```
165229
*
@@ -174,17 +238,19 @@ export class DataAPIClient extends HierarchicalLogger<DataAPIClientEventMap> {
174238
* @returns A new {@link Db} instance.
175239
*/
176240
public db(endpoint: string, options?: DbOptions): Db {
241+
if (typeof options as unknown === 'string') {
242+
throw new Error(`.db() no longer allows the .db('<id>', '<region>') overload; please pass in the full endpoint url (e.g. .db('<endpoint>')). You may use the exported \`buildAstraEndpoint\` utility function if you need to create an endpoint from just an ID and a region.`);
243+
}
177244
return new Db(this.#options, endpoint, DbOptsHandler.parse(options));
178245
}
179246

180247
/**
248+
* ##### Overview (Astra-only)
249+
*
181250
* Spawns a new {@link AstraAdmin} instance using the given options to work with the DevOps API (for admin
182251
* work such as creating/managing databases).
183252
*
184-
* **NB. This method is only available for Astra databases.**
185-
*
186-
* The given options will override any default options set when creating the {@link DataAPIClient} through
187-
* a deep merge (i.e. unset properties in the options object will just default to the default options).
253+
* **Note: this method is only available for Astra databases.**
188254
*
189255
* @example
190256
* ```typescript
@@ -195,6 +261,12 @@ export class DataAPIClient extends HierarchicalLogger<DataAPIClientEventMap> {
195261
* console.log(dbs);
196262
* ```
197263
*
264+
* ---
265+
*
266+
* ##### The options hierarchy
267+
*
268+
* The options for the {@link AstraAdmin} instance are a deep merge of the options for the {@link DataAPIClient} and the options for the {@link AstraAdmin} itself.
269+
*
198270
* @param options - Any options to override the default options set when creating the {@link DataAPIClient}.
199271
*
200272
* @returns A new {@link AstraAdmin} instance.
@@ -207,24 +279,34 @@ export class DataAPIClient extends HierarchicalLogger<DataAPIClientEventMap> {
207279
}
208280

209281
/**
282+
* ##### Overview
283+
*
210284
* Closes the client and disconnects all underlying connections. This should be called when the client is no longer
211285
* needed to free up resources.
212286
*
213287
* The client will be no longer usable after this method is called.
214288
*
215-
* @remarks
216289
* This method is idempotent and can be called multiple times without issue.
217290
*
218-
* --
291+
* @example
292+
* ```ts
293+
* const client = new DataAPIClient(...);
294+
* await client.close();
295+
*
296+
* // Error: Can't make requests on a closed client
297+
* const coll = client.db(...).collection(...);
298+
* await coll.findOne();
299+
* ```
300+
* ---
301+
*
302+
* ##### When to call this method
219303
*
220304
* For most users, this method isn't necessary to call, as resources will be freed up when the
221-
* server is shut down or the process is killed. However, it's useful in long-running processes or when you want to
222-
* free up resources immediately.
305+
* server is shut down or the process is killed.
223306
*
224-
* --
307+
* However, it's useful in long-running processes or when you want to free up resources immediately.
225308
*
226-
* Think of it as using malloc or using a file descriptor. Freeing them isn't always strictly necessary for
227-
* long-running usages, but it's there for when you need it.
309+
* 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.
228310
*
229311
* @returns A promise that resolves when the client has been closed.
230312
*/

0 commit comments

Comments
 (0)