Skip to content

Commit 0725f01

Browse files
committed
the documentation never ends...
1 parent ddf6d9c commit 0725f01

File tree

9 files changed

+224
-20
lines changed

9 files changed

+224
-20
lines changed

src/api/constants.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,46 @@
1414

1515
import { LIB_NAME, LIB_VERSION } from '@/src/version';
1616

17+
/**
18+
* @internal
19+
*/
1720
export const CLIENT_USER_AGENT = LIB_NAME + '/' + LIB_VERSION;
1821

22+
/**
23+
* @internal
24+
*/
1925
export const enum HTTP_METHODS {
2026
Get = 'GET',
2127
Post = 'POST',
2228
Delete = 'DELETE',
2329
}
2430

31+
/**
32+
* @internal
33+
*/
2534
export const DEFAULT_NAMESPACE = 'default_keyspace';
26-
export const DEFAULT_METHOD = HTTP_METHODS.Get;
35+
36+
/**
37+
* @internal
38+
*/
2739
export const DEFAULT_TIMEOUT = 30000;
2840

41+
/**
42+
* @internal
43+
*/
2944
export const DEFAULT_DATA_API_AUTH_HEADER = 'Token';
45+
46+
/**
47+
* @internal
48+
*/
3049
export const DEFAULT_DEVOPS_API_AUTH_HEADER = 'Authorization';
3150

51+
/**
52+
* @internal
53+
*/
3254
export const DEFAULT_DEVOPS_API_ENDPOINT = 'https://api.astra.datastax.com/v2';
55+
56+
/**
57+
* @internal
58+
*/
3359
export const DEFAULT_DATA_API_PATH = 'api/json/v1';

src/api/http-client.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { setLevel } from '@/src/logger';
16-
import { CLIENT_USER_AGENT, DEFAULT_METHOD, DEFAULT_TIMEOUT } from '@/src/api/constants';
15+
import { CLIENT_USER_AGENT, DEFAULT_TIMEOUT } from '@/src/api/constants';
1716
import {
1817
Caller,
19-
HTTPRequestInfo,
20-
HTTPRequestStrategy,
2118
GuaranteedAPIResponse,
22-
HTTPClientOptions
19+
HTTPClientOptions,
20+
HTTPRequestInfo,
21+
HTTPRequestStrategy
2322
} from '@/src/api/types';
2423
import { HTTP1AuthHeaderFactories, HTTP1Strategy } from '@/src/api/http1';
2524
import { HTTP2Strategy } from '@/src/api/http2';
@@ -57,10 +56,6 @@ export class HttpClient {
5756
? new HTTP2Strategy(this.baseUrl)
5857
: new HTTP1Strategy(HTTP1AuthHeaderFactories.DataApi);
5958

60-
if (options.logLevel) {
61-
setLevel(options.logLevel);
62-
}
63-
6459
if (options.baseApiPath) {
6560
this.baseUrl += '/' + options.baseApiPath;
6661
}
@@ -105,7 +100,7 @@ export class HttpClient {
105100
url: info.url,
106101
data: info.data,
107102
timeout: info.timeout || DEFAULT_TIMEOUT,
108-
method: info.method || DEFAULT_METHOD,
103+
method: info.method,
109104
params: info.params ?? {},
110105
token: this.#applicationToken,
111106
userAgent: this.userAgent,

src/api/types.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import type { HTTP_METHODS } from '@/src/api/index';
1616

1717
export type Caller = [name: string, version?: string];
1818

19+
/**
20+
* @internal
21+
*/
1922
export interface HTTPClientOptions {
2023
baseUrl: string;
2124
caller?: Caller | Caller[];
22-
logLevel?: string;
2325
baseApiPath?: string;
2426
applicationToken: string;
2527
useHttp2?: boolean;
@@ -34,28 +36,40 @@ export interface RawDataApiResponse {
3436
data?: Record<string, any>;
3537
}
3638

39+
/**
40+
* @internal
41+
*/
3742
export interface GuaranteedAPIResponse {
3843
data?: Record<string, any>,
3944
headers: Record<string, string>,
4045
status: number,
4146
}
4247

48+
/**
49+
* @internal
50+
*/
4351
export interface HTTPRequestInfo {
4452
url: string,
4553
data?: unknown,
4654
params?: Record<string, string>,
47-
method?: HTTP_METHODS,
55+
method: HTTP_METHODS,
4856
timeout?: number,
4957
timeoutError: () => Error,
5058
}
5159

60+
/**
61+
* @internal
62+
*/
5263
export interface InternalHTTPRequestInfo extends HTTPRequestInfo {
5364
token: string,
5465
method: HTTP_METHODS,
5566
timeout: number,
5667
userAgent: string,
5768
}
5869

70+
/**
71+
* @internal
72+
*/
5973
export interface HTTPRequestStrategy {
6074
request: (params: InternalHTTPRequestInfo) => Promise<GuaranteedAPIResponse>;
6175
close?: () => void;

src/client/data-api-client.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Db, mkDb } from '@/src/data-api/db';
22
import { AstraAdmin, mkAdmin } from '@/src/devops/astra-admin';
33
import { AdminSpawnOptions, DbSpawnOptions, RootClientOptions, RootClientOptsWithToken } from '@/src/client/types';
4+
import { setLevel } from '@/src/logger';
45

56
/**
67
* The main entrypoint into working with the Data API. It sits at the top of the
@@ -49,6 +50,10 @@ export class DataApiClient {
4950
...options?.adminOptions,
5051
},
5152
};
53+
54+
if (options?.logLevel) {
55+
setLevel(options.logLevel);
56+
}
5257
}
5358

5459
/**

src/data-api/db.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ export class Db implements Disposable {
132132
applicationToken: dbOpts.token,
133133
baseApiPath: dbOpts?.dataApiPath || DEFAULT_DATA_API_PATH,
134134
caller: options.caller,
135-
logLevel: options.logLevel,
136135
logSkippedOptions: dbOpts.logSkippedOptions,
137136
useHttp2: dbOpts.useHttp2,
138137
}),
@@ -191,7 +190,7 @@ export class Db implements Disposable {
191190
}
192191

193192
/**
194-
* Fetches information about the database, such as the database name, ID, region, and other metadata.
193+
* Fetches information about the database, such as the database name, region, and other metadata.
195194
*
196195
* For the full, complete, information, see {@link AstraDbAdmin.info}.
197196
*
@@ -481,7 +480,7 @@ export class Db implements Disposable {
481480
*
482481
* Meant for usage with the `using` clause in ERM (Explicit Resource Management).
483482
*/
484-
[Symbol.dispose]() {
483+
public [Symbol.dispose]() {
485484
this.close();
486485
}
487486
}

src/devops/astra-admin.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export class AstraAdmin {
5656
baseUrl: adminOpts.endpointUrl || DEFAULT_DEVOPS_API_ENDPOINT,
5757
applicationToken: adminOpts.adminToken,
5858
caller: options.caller,
59-
logLevel: options.logLevel,
6059
}),
6160
enumerable: false,
6261
});
@@ -256,7 +255,8 @@ export class AstraAdmin {
256255
/**
257256
* Creates a new database with the given configuration.
258257
*
259-
* **NB. this is a long-running operation. See {@link AdminBlockingOptions} about such blocking operations.**
258+
* **NB. this is a long-running operation. See {@link AdminBlockingOptions} about such blocking operations.** The
259+
* default polling interval is 10 seconds. Expect it to take roughly 2 min to complete.
260260
*
261261
* Note that **the `name` field is non-unique** and thus creating a database, even with the same options, is **not
262262
* idempotent**.
@@ -329,7 +329,8 @@ export class AstraAdmin {
329329
/**
330330
* Terminates a database by ID or by a given {@link Db} instance.
331331
*
332-
* **NB. this is a long-running operation. See {@link AdminBlockingOptions} about such blocking operations.**
332+
* **NB. this is a long-running operation. See {@link AdminBlockingOptions} about such blocking operations.** The
333+
* default polling interval is 10 seconds. Expect it to take roughly 6-7 min to complete.
333334
*
334335
* The database info will still be accessible by ID, or by using the {@link listDatabases} method with the filter
335336
* set to `'ALL'` or `'TERMINATED'`. However, all of its data will very much be lost.
@@ -346,6 +347,8 @@ export class AstraAdmin {
346347
* @param db - The database to drop, either by ID or by instance.
347348
* @param options - The options for the blocking behavior of the operation.
348349
*
350+
* @returns A promise that resolves when the operation completes.
351+
*
349352
* @remarks Use with caution. Wear a harness. Don't say I didn't warn you.
350353
*/
351354
async dropDatabase(db: Db | string, options?: AdminBlockingOptions): Promise<void> {

src/devops/astra-db-admin.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,51 @@ export class AstraDbAdmin extends DbAdmin {
5858
}
5959

6060
/**
61+
* Gets the ID of the Astra DB instance this object is managing.
62+
*
63+
* @returns the ID of the Astra DB instance.
64+
*/
65+
public get id(): string {
66+
return this._db.id;
67+
}
68+
69+
/**
70+
* Gets the underlying `Db` object. The options for the db were set when the AstraDbAdmin instance, or whatever
71+
* spawned it, was created.
72+
*
73+
* @example
74+
* ```typescript
75+
* const dbAdmin = client.admin().dbAdmin('<endpoint>', {
76+
*   namespace: 'my-namespace',
77+
*   useHttp2: false,
78+
* });
79+
*
80+
* const db = dbAdmin.db();
81+
* console.log(db.id);
82+
* ```
83+
*
6184
* @returns the underlying `Db` object.
6285
*/
6386
public override db(): Db {
6487
return this._db;
6588
}
6689

90+
/**
91+
* Fetches the complete information about the database, such as the database name, IDs, region, status, actions, and
92+
* other metadata.
93+
*
94+
* The method issues a request to the DevOps API each time it is invoked, without caching mechanisms;
95+
* this ensures up-to-date information for usages such as real-time collection validation by the application.
96+
*
97+
*
98+
* @example
99+
* ```typescript
100+
* const info = await dbAdmin.info();
101+
* console.log(info.info.name, info.creationTime);
102+
* ```
103+
*
104+
* @returns A promise that resolves to the complete database information.
105+
*/
67106
public async info(): Promise<FullDatabaseInfo> {
68107
const resp = await this._httpClient.request({
69108
method: HTTP_METHODS.Get,
@@ -72,10 +111,56 @@ export class AstraDbAdmin extends DbAdmin {
72111
return resp.data;
73112
}
74113

114+
/**
115+
* Lists the namespaces in the database.
116+
*
117+
* The first element in the returned array is the default namespace of the database, and the rest are additional
118+
* namespaces in no particular order.
119+
*
120+
* @example
121+
* ```typescript
122+
* const namespaces = await dbAdmin.listNamespaces();
123+
*
124+
* // ['default_keyspace', 'my_other_keyspace']
125+
* console.log(namespaces);
126+
* ```
127+
*
128+
* @returns A promise that resolves to an array of namespace names.
129+
*/
75130
public override async listNamespaces(): Promise<string[]> {
76131
return this.info().then(i => [i.info.keyspace!, ...i.info.additionalKeyspaces ?? []].filter(Boolean))
77132
}
78133

134+
/**
135+
* Creates a new, additional, namespace (aka keyspace) for this database.
136+
*
137+
* **NB. this is a "long-running" operation. See {@link AdminBlockingOptions} about such blocking operations.** The
138+
* default polling interval is 2 seconds. Expect it to take roughly 8-10 seconds to complete.
139+
*
140+
* @example
141+
* ```typescript
142+
* await dbAdmin.createNamespace('my_other_keyspace1');
143+
*
144+
* // ['default_keyspace', 'my_other_keyspace1']
145+
* console.log(await dbAdmin.listNamespaces());
146+
*
147+
* await dbAdmin.createNamespace('my_other_keyspace2', {
148+
*   blocking: false,
149+
* });
150+
*
151+
* // Will not include 'my_other_keyspace2' until the operation completes
152+
* console.log(await dbAdmin.listNamespaces());
153+
* ```
154+
*
155+
* @remarks
156+
* Note that if you choose not to block, the created namespace object will not be able to be used until the
157+
* operation completes, which is up to the caller to determine.
158+
*
159+
* @param namespace - The name of the new namespace.
160+
* @param options - The options for the blocking behavior of the operation.
161+
*
162+
* @returns A promise that resolves when the operation completes.
163+
*/
79164
public override async createNamespace(namespace: string, options?: AdminBlockingOptions): Promise<void> {
80165
await this._httpClient.request({
81166
method: HTTP_METHODS.Post,
@@ -84,6 +169,37 @@ export class AstraDbAdmin extends DbAdmin {
84169
await this._httpClient.awaitStatus(this._db, 'ACTIVE', ['MAINTENANCE'], options, 1000);
85170
}
86171

172+
/**
173+
* Drops a namespace (aka keyspace) from this database.
174+
*
175+
* **NB. this is a "long-running" operation. See {@link AdminBlockingOptions} about such blocking operations.** The
176+
* default polling interval is 2 seconds. Expect it to take roughly 8-10 seconds to complete.
177+
*
178+
* @example
179+
* ```typescript
180+
* await dbAdmin.dropNamespace('my_other_keyspace1');
181+
*
182+
* // ['default_keyspace', 'my_other_keyspace2']
183+
* console.log(await dbAdmin.listNamespaces());
184+
*
185+
* await dbAdmin.dropNamespace('my_other_keyspace2', {
186+
*   blocking: false,
187+
* });
188+
*
189+
* // Will still include 'my_other_keyspace2' until the operation completes
190+
* // ['default_keyspace', 'my_other_keyspace2']
191+
* console.log(await dbAdmin.listNamespaces());
192+
* ```
193+
*
194+
* @remarks
195+
* Note that if you choose not to block, the namespace object will still be able to be used until the operation
196+
* completes, which is up to the caller to determine.
197+
*
198+
* @param namespace - The name of the namespace to drop.
199+
* @param options - The options for the blocking behavior of the operation.
200+
*
201+
* @returns A promise that resolves when the operation completes.
202+
*/
87203
public override async dropNamespace(namespace: string, options?: AdminBlockingOptions): Promise<void> {
88204
await this._httpClient.request({
89205
method: HTTP_METHODS.Delete,
@@ -92,6 +208,27 @@ export class AstraDbAdmin extends DbAdmin {
92208
await this._httpClient.awaitStatus(this._db, 'ACTIVE', ['MAINTENANCE'], options, 1000);
93209
}
94210

211+
/**
212+
* Drops the database.
213+
*
214+
* **NB. this is a long-running operation. See {@link AdminBlockingOptions} about such blocking operations.** The
215+
* default polling interval is 10 seconds. Expect it to take roughly 6-7 min to complete.
216+
*
217+
* The database info will still be accessible by ID, or by using the {@link listDatabases} method with the filter
218+
* set to `'ALL'` or `'TERMINATED'`. However, all of its data will very much be lost.
219+
*
220+
* @example
221+
* ```typescript
222+
* const db = client.db('https://<db_id>-<region>.apps.astra.datastax.com');
223+
* await db.admin().drop();
224+
* ```
225+
*
226+
* @param options - The options for the blocking behavior of the operation.
227+
*
228+
* @returns A promise that resolves when the operation completes.
229+
*
230+
* @remarks Use with caution. Use a surge protector. Don't say I didn't warn you.
231+
*/
95232
public async drop(options?: AdminBlockingOptions): Promise<void> {
96233
await this._httpClient.request({
97234
method: HTTP_METHODS.Post,

0 commit comments

Comments
 (0)