Skip to content

Commit 6b0ead3

Browse files
authored
Minor ts-doc cleanup (#30)
* updated some ts-doc in db.ts * added quick explanation as to why close() exists * Improved sort/projection documentation * couple minor doc updates * commented the build script
1 parent e8971a9 commit 6b0ead3

File tree

10 files changed

+106
-31
lines changed

10 files changed

+106
-31
lines changed

scripts/build.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
#!/usr/bin/sh
22

3+
# Cleans the previous build
34
rm -rf ./dist
5+
6+
# Creates the version file
47
echo "export const LIB_NAME = 'astra-db-ts';" > src/version.ts
58
node -p "'export const LIB_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'" >> src/version.ts
9+
10+
# Transpiles the project
611
npx tsc --project tsconfig.build.json
12+
13+
# Replaces alias paths with relative paths (e.g. `@/src/version` -> `../../src/version`)
714
npx tsc-alias -p tsconfig.build.json
15+
16+
# Creates the rollup .d.ts and generates an API report in etc/
817
npm run api-extractor
18+
19+
# Deletes the temp folder that was created by API extractor
920
rm -r ./temp
21+
22+
# Removes all .d.ts files except the main rollup .d.ts
1023
cd dist || return 1
1124
find . -type f -name '*.d.ts' ! -name 'astra-db-ts.d.ts' -exec rm {} +
1225
cd ..

src/client/data-api-client.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ export type DataAPIClientEvents =
4949
& AdminCommandEvents
5050

5151
/**
52-
* The base class for the {@link DataAPIClient} event emitter to make it properly typed. Should probably never need
53-
* to be used directly.
52+
* The base class for the {@link DataAPIClient} event emitter to make it properly typed.
53+
*
54+
* Should probably never need to be used directly.
5455
*
5556
* @public
5657
*/
@@ -247,6 +248,17 @@ export class DataAPIClient extends DataAPIClientEventEmitterBase {
247248
* @remarks
248249
* This method is idempotent and can be called multiple times without issue.
249250
*
251+
* --
252+
*
253+
* For most users, this method isn't necessary to call, as resources will be freed up when the
254+
* server is shut down or the process is killed. However, it's useful in long-running processes or when you want to
255+
* free up resources immediately.
256+
*
257+
* --
258+
*
259+
* Think of it as using malloc or using a file descriptor. Freeing them isn't always strictly necessary for
260+
* long-running usages, but it's there for when you need it.
261+
*
250262
* @returns A promise that resolves when the client has been closed.
251263
*/
252264
public async close(): Promise<void> {

src/data-api/cursor.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,6 @@ export class FindCursor<T, TRaw extends SomeDoc = SomeDoc> {
315315
* resultant data was already fetched by this cursor.
316316
*/
317317
rewind(): void {
318-
// if (this._state === CursorStatus.Uninitialized) {
319-
// return;
320-
// }
321-
322318
this._buffer.length = 0;
323319
this._nextPageState = undefined;
324320
this._state = CursorStatus.Uninitialized;

src/data-api/db.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import { InternalRootClientOpts } from '@/src/client/types';
5252
*
5353
* // Overrides default options from the DataAPIClient
5454
* const db2 = client.db('https://<db_id>-<region>.apps.astra.datastax.com', {
55-
*   namespace: 'my-namespace',
55+
*   namespace: 'my_namespace',
5656
*   useHttp2: false,
5757
* });
5858
*
@@ -78,18 +78,18 @@ export class Db {
7878
* ```typescript
7979
*
8080
* // Uses 'default_keyspace' as the default namespace for all future db spawns
81-
* const client1 = new DataAPIClient('AstraCS:...');
81+
* const client1 = new DataAPIClient('*TOKEN*');
8282
*
8383
* // Overrides the default namespace for all future db spawns
84-
* const client2 = new DataAPIClient('AstraCS:...', {
84+
* const client2 = new DataAPIClient('*TOKEN*', {
8585
*   dbOptions: { namespace: 'my_namespace' }
8686
* });
8787
*
8888
* // Created with 'default_keyspace' as the default namespace
89-
* const db1 = client1.db('https://<db_id>-<region>.apps.astra.datastax.com');
89+
* const db1 = client1.db('*ENDPOINT*');
9090
*
9191
* // Created with 'my_namespace' as the default namespace
92-
* const db2 = client1.db('https://<db_id>-<region>.apps.astra.datastax.com', {
92+
* const db2 = client1.db('*ENDPOINT*', {
9393
*   namespace: 'my_namespace'
9494
* });
9595
*
@@ -139,7 +139,7 @@ export class Db {
139139
}
140140

141141
/**
142-
* The ID of the database, if it's an Astra database. If it's a non-Astra database, this will throw an error.
142+
* The ID of the database, if it's an Astra database. If it's not an Astra database, this will throw an error.
143143
*
144144
* @throws Error - if the database is not an Astra database.
145145
*/
@@ -154,7 +154,7 @@ export class Db {
154154
* Spawns a new {@link AstraDbAdmin} instance for this database, used for performing administrative operations
155155
* on the database, such as managing namespaces, or getting database information.
156156
*
157-
* **NB. This method will throw an error if the database is not an Astra database.**
157+
* **NB. Only available for Astra databases.**
158158
*
159159
* The given options will override any default options set when creating the {@link DataAPIClient} through
160160
* a deep merge (i.e. unset properties in the options object will just default to the default options).
@@ -184,13 +184,13 @@ export class Db {
184184
/**
185185
* Fetches information about the database, such as the database name, region, and other metadata.
186186
*
187+
* **NB. Only available for Astra databases.**
188+
*
187189
* For the full, complete, information, see {@link AstraDbAdmin.info}.
188190
*
189191
* The method issues a request to the DevOps API each time it is invoked, without caching mechanisms;
190192
* this ensures up-to-date information for usages such as real-time collection validation by the application.
191193
*
192-
* Only available for Astra databases.
193-
*
194194
* @example
195195
* ```typescript
196196
* const info = await db.info();
@@ -249,9 +249,9 @@ export class Db {
249249
}
250250

251251
/**
252-
* Fetches all the collections in the database (& in the working/given namespace), and establishes references to them.
252+
* Establishes references to all the collections in the working/given namespace.
253253
*
254-
* You can also specify a namespace in the options parameter, which will override the default namespace for this database.
254+
* You can specify a namespace in the options parameter, which will override the default namespace for this `Db` instance.
255255
*
256256
* @example
257257
* ```typescript
@@ -269,11 +269,9 @@ export class Db {
269269
* @returns A promise that resolves to an array of references to the working Db's collections.
270270
*/
271271
public async collections(options?: WithNamespace & WithTimeout): Promise<Collection[]> {
272-
const timeoutManager = this._httpClient.timeoutManager(options?.maxTimeMS);
273-
274272
const collections = await this.listCollections({
275273
namespace: options?.namespace,
276-
maxTimeMS: timeoutManager.msRemaining,
274+
maxTimeMS: options?.maxTimeMS,
277275
nameOnly: true,
278276
});
279277

@@ -283,13 +281,13 @@ export class Db {
283281
/**
284282
* Creates a new collection in the database, and establishes a reference to it.
285283
*
286-
* **NB. You are limited to 10 collections per database in Astra, so be wary when using this command.**
284+
* **NB. You are limited in the amount of collections you can create, so be wary when using this command.**
287285
*
288286
* This is a blocking command which performs actual I/O unlike {@link Db.collection}, which simply creates an
289287
* unvalidated reference to a collection.
290288
*
291-
* **If `checkExists: false`, creation is idempotent, so if the collection already exists with the same options,
292-
* this method will not throw an error. If the options differ though, it'll raise an error.**
289+
* If `checkExists: false`, creation is idempotent, so if the collection already exists with the same options,
290+
* this method will not throw an error. If the options mismatch, it will throw a {@link DataAPIResponseError}.
293291
*
294292
* Typed as `Collection<SomeDoc>` by default, but you can specify a schema type to get a typed collection. If left
295293
* as `SomeDoc`, the collection will be untyped.
@@ -298,6 +296,8 @@ export class Db {
298296
*
299297
* You can also specify a namespace in the options parameter, which will override the default namespace for this database.
300298
*
299+
* See {@link CreateCollectionOptions} for *much* more information on the options available.
300+
*
301301
* @example
302302
* ```typescript
303303
* interface User {
@@ -314,6 +314,7 @@ export class Db {
314314
*   defaultId: {
315315
*   type: "objectId",
316316
*   },
317+
*   checkExists: false,
317318
* });
318319
* ```
319320
*
@@ -322,6 +323,8 @@ export class Db {
322323
*
323324
* @returns A promised reference to the newly created collection.
324325
*
326+
* @throws CollectionAlreadyExistsError - if the collection already exists and `checkExists` is `true` or unset.
327+
*
325328
* @see SomeDoc
326329
* @see VectorDoc
327330
*/
@@ -433,6 +436,7 @@ export class Db {
433436
const command: ListCollectionsCommand = {
434437
findCollections: {
435438
options: {
439+
// Is 'nameOnly' instead of 'explain' for Mongo-compatibility reasons
436440
explain: options?.nameOnly !== true,
437441
},
438442
},

src/data-api/types/collections/collections-common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ export interface VectorizeServiceOptions {
9292
* @public
9393
*/
9494
export type IndexingOptions<Schema extends SomeDoc> =
95-
| { allow: (keyof ToDotNotation<Schema>)[] | ['*'], deny?: never }
96-
| { deny: (keyof ToDotNotation<Schema>)[] | ['*'], allow?: never }
95+
| { allow: (keyof ToDotNotation<Schema>)[] | ['*'], deny?: never }
96+
| { deny: (keyof ToDotNotation<Schema>)[] | ['*'], allow?: never }
9797

9898
/**
9999
* Represents the options for the default ID.

src/data-api/types/collections/create-collection.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ export interface CreateCollectionCommand {
4040
*/
4141
export interface CreateCollectionOptions<Schema extends SomeDoc> extends WithTimeout, CollectionOptions<Schema>, WithNamespace {
4242
/**
43-
* If `true`, runs an additional existence check before creating the collection, failing if the collection with the
44-
* same name already exists, raising an error. Otherwise, the creation is always attempted, and the command's will
45-
* succeed even if the collection with the given name already exists, as long as the options are the exact same.
43+
* If `true` or unset, runs an additional existence check before creating the collection, failing if the collection
44+
* with the same name already exists, raising a {@link CollectionAlreadyExistsError}.
45+
*
46+
* Otherwise, if `false`, the creation is always attempted, and the command will succeed even if the collection
47+
* with the given name already exists, as long as the options are the exact same (if options mismatch, it'll
48+
* throw a {@link DataAPIResponseError}).
49+
*
50+
* @defaultValue true
4651
*/
4752
checkExists?: boolean;
4853
}

src/data-api/types/common.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export type SortDirection = 1 | -1 | 'asc' | 'desc' | 'ascending' | 'descending'
3636
*
3737
* Can use `1`/`-1` for ascending/descending, or `$vector` for sorting by vector distance.
3838
*
39+
* See {@link SortDirection} for all possible sort values.
40+
*
3941
* **NB. The order of the fields in the sort option is significant—fields are sorted in the order they are listed.**
4042
*
4143
* @example
@@ -52,6 +54,9 @@ export type SortDirection = 1 | -1 | 'asc' | 'desc' | 'ascending' | 'descending'
5254
* }
5355
* ```
5456
*
57+
* @see StrictSort
58+
* @see SortDirection
59+
*
5560
* @public
5661
*/
5762
export type Sort =
@@ -64,7 +69,9 @@ export type Sort =
6469
*
6570
* **If you want stricter type-checking and full auto-complete, see {@link StrictProjection}.**
6671
*
67-
* Can use `1`/`0`, or `true`/`false`
72+
* Can use `1`/`0`, or `true`/`false`.
73+
*
74+
* There's a special field `'*'` that can be used to include/exclude all fields.
6875
*
6976
* @example
7077
* ```typescript
@@ -86,6 +93,8 @@ export type Sort =
8693
* }
8794
* ```
8895
*
96+
* @see StrictProjection
97+
*
8998
* @public
9099
*/
91100
export type Projection = Record<string, 1 | 0 | true | false | ProjectionSlice>;
@@ -95,6 +104,8 @@ export type Projection = Record<string, 1 | 0 | true | false | ProjectionSlice>;
95104
*
96105
* Can use `1`/`-1` for ascending/descending, or `$vector` for sorting by vector distance.
97106
*
107+
* See {@link SortDirection} for all possible sort values.
108+
*
98109
* **NB. The order of the fields in the sort option is significant—fields are sorted in the order they are listed.**
99110
*
100111
* @example
@@ -115,6 +126,9 @@ export type Projection = Record<string, 1 | 0 | true | false | ProjectionSlice>;
115126
* });
116127
* ```
117128
*
129+
* @see Sort
130+
* @see SortDirection
131+
*
118132
* @public
119133
*/
120134
export type StrictSort<Schema extends SomeDoc> =
@@ -125,7 +139,9 @@ export type StrictSort<Schema extends SomeDoc> =
125139
/**
126140
* Specifies which fields should be included/excluded in the returned documents.
127141
*
128-
* Can use `1`/`0`, or `true`/`false`
142+
* Can use `1`/`0`, or `true`/`false`.
143+
*
144+
* There's a special field `'*'` that can be used to include/exclude all fields.
129145
*
130146
* @example
131147
* ```typescript
@@ -150,6 +166,8 @@ export type StrictSort<Schema extends SomeDoc> =
150166
* });
151167
* ```
152168
*
169+
* @see Projection
170+
*
153171
* @public
154172
*/
155173
export type StrictProjection<Schema extends SomeDoc> = {
@@ -178,6 +196,25 @@ export type StrictProjection<Schema extends SomeDoc> = {
178196
* { $slice: [-4, 2] }
179197
* ```
180198
*
199+
* @example
200+
* ```typescript
201+
* await collection.insertOne({ arr: [1, 2, 3, 4, 5] });
202+
*
203+
* // Return [1, 2]
204+
* await collection.findOne({}, {
205+
*   projection: {
206+
*   arr: { $slice: 2 },
207+
*   },
208+
* });
209+
*
210+
* // Return [3, 4]
211+
* await collection.findOne({}, {
212+
*   projection: {
213+
*   arr: { $slice: [-3, 2] },
214+
*   },
215+
* });
216+
* ```
217+
*
181218
* @public
182219
*/
183220
export interface ProjectionSlice {

src/data-api/types/filter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import { IsDate, IsNum } from '@/src/data-api/types/utils';
3838
* });
3939
* ```
4040
*
41+
* @see StrictFilter
42+
*
4143
* @public
4244
*/
4345
export type Filter<Schema extends SomeDoc> = {
@@ -75,6 +77,8 @@ export type Filter<Schema extends SomeDoc> = {
7577
* } satisfies StrictFilter<BasicSchema>);
7678
* ```
7779
*
80+
* @see Filter
81+
*
7882
* @public
7983
*/
8084
export type StrictFilter<Schema extends SomeDoc> = {

src/data-api/types/misc/bulk-write.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class BulkWriteResult<Schema extends SomeDoc> {
133133
/**
134134
* Represents *some* bulk write operation.
135135
*
136-
* Be careful not to pass in multiple operations in the same object.
136+
* Be careful not to pass in multiple operations in the same object (only one operation per object allowed)
137137
*
138138
* @public
139139
*/

src/data-api/types/update-filter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import { IsDate, IsNum } from '@/src/data-api/types/utils';
5151
* @field $mul - Multiply the value of a field in the document.
5252
* @field $addToSet - Add an element to an array field in the document if it does not already exist.
5353
*
54+
* @see StrictUpdateFilter
55+
*
5456
* @public
5557
*/
5658
export interface UpdateFilter<Schema extends SomeDoc> {
@@ -249,6 +251,8 @@ export interface UpdateFilter<Schema extends SomeDoc> {
249251
* @field $mul - Multiply the value of a field in the document.
250252
* @field $addToSet - Add an element to an array field in the document if it does not already exist.
251253
*
254+
* @see UpdateFilter
255+
*
252256
* @public
253257
*/
254258
export interface StrictUpdateFilter<Schema extends SomeDoc> {

0 commit comments

Comments
 (0)