Skip to content

Commit 5404bd0

Browse files
committed
Minor testing, bug fixes, and changes
1 parent e220bcc commit 5404bd0

36 files changed

+543
-186
lines changed

.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ ASTRA_URI=https://<db_id>-<region>.apps.astra.datastax.com
44
# Application token, used to authenticate with the Astra API
55
APPLICATION_TOKEN=AstraCS:<rest_of_token>
66

7-
# Set this to some value to enable running tests that require a dev environment (make sure you're actually using the dev environment!!)
8-
ASTRA_RUN_DEV_TESTS=
7+
# Set this to some value to enable running tests that require a $vectorize enabled environment
8+
ASTRA_RUN_VECTORIZE_TESTS=
99

1010
# Set this to some value to enable running long-running tests
1111
ASTRA_RUN_LONG_TESTS=

src/api/data-api-http-client.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,6 @@ export function replacer(this: any, key: string, value: any): any {
127127
}
128128

129129
if (typeof this[key] === 'object') {
130-
if (value instanceof ObjectId) {
131-
return { $objectId: value.toString() };
132-
}
133-
134-
if (value instanceof UUID) {
135-
return { $uuid: value.toString() };
136-
}
137-
138130
if (key === '$date') {
139131
return new Date(value).valueOf();
140132
}

src/data-api/collection.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
353353
* @see StrictFilter
354354
* @see StrictUpdateFilter
355355
*/
356-
public async updateOne(filter: Filter<Schema>, update: UpdateFilter<Schema>, options?: UpdateOneOptions<Schema>): Promise<UpdateOneResult> {
356+
public async updateOne(filter: Filter<Schema>, update: UpdateFilter<Schema>, options?: UpdateOneOptions<Schema>): Promise<UpdateOneResult<Schema>> {
357357
options = coalesceVectorSpecialsIntoSort(options);
358358

359359
const command: UpdateOneCommand = {
@@ -383,7 +383,10 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
383383
upsertedId: resp.status?.upsertedId,
384384
upsertedCount: 1,
385385
}
386-
: commonResult;
386+
: {
387+
...commonResult,
388+
upsertedCount: 0,
389+
};
387390
}
388391

389392
/**
@@ -438,7 +441,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
438441
* @see StrictFilter
439442
* @see StrictUpdateFilter
440443
*/
441-
public async updateMany(filter: Filter<Schema>, update: UpdateFilter<Schema>, options?: UpdateManyOptions): Promise<UpdateManyResult> {
444+
public async updateMany(filter: Filter<Schema>, update: UpdateFilter<Schema>, options?: UpdateManyOptions): Promise<UpdateManyResult<SomeDoc>> {
442445
const command: UpdateManyCommand = {
443446
updateMany: {
444447
filter,
@@ -454,6 +457,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
454457
const commonResult = {
455458
modifiedCount: 0,
456459
matchedCount: 0,
460+
upsertedCount: <const>0,
457461
};
458462

459463
let resp;
@@ -473,6 +477,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
473477

474478
commonResult.modifiedCount += desc.rawResponse?.status?.modifiedCount ?? 0;
475479
commonResult.matchedCount += desc.rawResponse?.status?.matchedCount ?? 0;
480+
commonResult.upsertedCount = desc.rawResponse?.status?.upsertedCount ?? 0;
476481

477482
throw mkRespErrorFromResponse(UpdateManyError, command, desc.rawResponse, commonResult);
478483
}
@@ -536,7 +541,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
536541
*
537542
* @see StrictFilter
538543
*/
539-
public async replaceOne(filter: Filter<Schema>, replacement: NoId<Schema>, options?: ReplaceOneOptions<Schema>): Promise<ReplaceOneResult> {
544+
public async replaceOne(filter: Filter<Schema>, replacement: NoId<Schema>, options?: ReplaceOneOptions<Schema>): Promise<ReplaceOneResult<Schema>> {
540545
options = coalesceVectorSpecialsIntoSort(options);
541546

542547
const command: FindOneAndReplaceCommand = {
@@ -567,7 +572,10 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
567572
upsertedId: resp.status?.upsertedId,
568573
upsertedCount: 1,
569574
}
570-
: commonResult;
575+
: {
576+
...commonResult,
577+
upsertedCount: 0,
578+
};
571579
}
572580

573581
/**
@@ -1397,7 +1405,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
13971405
*
13981406
* @throws BulkWriteError - If the operation fails
13991407
*/
1400-
public async bulkWrite(operations: AnyBulkWriteOperation<Schema>[], options?: BulkWriteOptions): Promise<BulkWriteResult> {
1408+
public async bulkWrite(operations: AnyBulkWriteOperation<Schema>[], options?: BulkWriteOptions): Promise<BulkWriteResult<Schema>> {
14011409
const timeoutManager = this._httpClient.timeoutManager(options?.maxTimeMS)
14021410

14031411
return (options?.ordered)
@@ -1589,7 +1597,7 @@ const insertMany = async <Schema>(httpClient: DataApiHttpClient, documents: unkn
15891597
/**
15901598
* @internal
15911599
*/
1592-
const bulkWriteOrdered = async (httpClient: DataApiHttpClient, operations: AnyBulkWriteOperation<SomeDoc>[], timeoutManager: TimeoutManager): Promise<BulkWriteResult> => {
1600+
const bulkWriteOrdered = async (httpClient: DataApiHttpClient, operations: AnyBulkWriteOperation<SomeDoc>[], timeoutManager: TimeoutManager): Promise<BulkWriteResult<SomeDoc>> => {
15931601
const results = new BulkWriteResult();
15941602
let i = 0;
15951603

@@ -1616,7 +1624,7 @@ const bulkWriteOrdered = async (httpClient: DataApiHttpClient, operations: AnyBu
16161624
/**
16171625
* @internal
16181626
*/
1619-
const bulkWriteUnordered = async (httpClient: DataApiHttpClient, operations: AnyBulkWriteOperation<SomeDoc>[], concurrency: number, timeoutManager: TimeoutManager): Promise<BulkWriteResult> => {
1627+
const bulkWriteUnordered = async (httpClient: DataApiHttpClient, operations: AnyBulkWriteOperation<SomeDoc>[], concurrency: number, timeoutManager: TimeoutManager): Promise<BulkWriteResult<SomeDoc>> => {
16201628
const results = new BulkWriteResult();
16211629
let masterIndex = 0;
16221630

@@ -1654,7 +1662,7 @@ const bulkWriteUnordered = async (httpClient: DataApiHttpClient, operations: Any
16541662
return results;
16551663
}
16561664

1657-
const bulkWrite = async (httpClient: DataApiHttpClient, operation: AnyBulkWriteOperation<SomeDoc>, results: BulkWriteResult, i: number, timeoutManager: TimeoutManager): Promise<void> => {
1665+
const bulkWrite = async (httpClient: DataApiHttpClient, operation: AnyBulkWriteOperation<SomeDoc>, results: BulkWriteResult<SomeDoc>, i: number, timeoutManager: TimeoutManager): Promise<void> => {
16581666
const command = buildBulkWriteCommand(operation);
16591667
const resp = await httpClient.executeCommand(command, { timeoutManager });
16601668
addToBulkWriteResult(results, resp.status!, i);
@@ -1685,8 +1693,8 @@ const buildBulkWriteCommand = (operation: AnyBulkWriteOperation<SomeDoc>): Recor
16851693
/**
16861694
* @internal
16871695
*/
1688-
const addToBulkWriteResult = (result: BulkWriteResult, resp: Record<string, any>, i: number) => {
1689-
const asMutable = result as Mutable<BulkWriteResult>;
1696+
const addToBulkWriteResult = (result: BulkWriteResult<SomeDoc>, resp: Record<string, any>, i: number) => {
1697+
const asMutable = result as Mutable<BulkWriteResult<SomeDoc>>;
16901698

16911699
asMutable.insertedCount += resp.insertedIds?.length ?? 0;
16921700
asMutable.modifiedCount += resp.modifiedCount ?? 0;

src/data-api/cursor.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import {
1717
FindOptions,
1818
InternalFindOptions,
1919
InternalGetMoreCommand,
20-
ProjectionOption,
21-
SortOption,
20+
StrictProjection,
21+
StrictSort,
2222
} from '@/src/data-api/types';
2323
import { CursorAlreadyInitializedError, SomeDoc } from '@/src/data-api';
2424
import { DataApiHttpClient } from '@/src/api';
@@ -141,7 +141,7 @@ export class FindCursor<T, TRaw extends SomeDoc = SomeDoc> {
141141
*
142142
* @return The cursor.
143143
*/
144-
sort(sort: SortOption<TRaw>): FindCursor<T, TRaw> {
144+
sort(sort: StrictSort<TRaw>): FindCursor<T, TRaw> {
145145
this._assertUninitialized();
146146
this._options.sort = sort;
147147
return this;
@@ -215,7 +215,7 @@ export class FindCursor<T, TRaw extends SomeDoc = SomeDoc> {
215215
*
216216
* @return The cursor.
217217
*/
218-
project<R = any>(projection: ProjectionOption<TRaw>): FindCursor<R, TRaw> {
218+
project<R = any, RRaw extends SomeDoc = SomeDoc>(projection: StrictProjection<TRaw>): FindCursor<R, RRaw> {
219219
this._assertUninitialized();
220220
this._options.projection = projection;
221221
return this as any;

src/data-api/db.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import { Collection, CollectionAlreadyExistsError, extractDbIdFromUrl, SomeDoc } from '@/src/data-api';
1616
import { DataApiHttpClient, DEFAULT_DATA_API_PATH, DEFAULT_NAMESPACE, RawDataApiResponse } from '@/src/api';
1717
import {
18-
CollectionName,
1918
CreateCollectionCommand,
2019
CreateCollectionOptions,
2120
FullCollectionInfo,
@@ -381,7 +380,7 @@ export class Db implements Disposable {
381380
*
382381
* @see CollectionOptions
383382
*/
384-
public async listCollections(options?: ListCollectionsOptions & { nameOnly?: true }): Promise<CollectionName[]>
383+
public async listCollections(options: ListCollectionsOptions & { nameOnly: true }): Promise<string[]>
385384

386385
/**
387386
* Lists the collections in the database.
@@ -402,22 +401,19 @@ export class Db implements Disposable {
402401
*
403402
* @see CollectionOptions
404403
*/
405-
public async listCollections(options?: ListCollectionsOptions & { nameOnly: false }): Promise<FullCollectionInfo[]>
404+
public async listCollections(options?: ListCollectionsOptions & { nameOnly?: false }): Promise<FullCollectionInfo[]>
406405

407-
public async listCollections(options?: ListCollectionsOptions): Promise<CollectionName[] | FullCollectionInfo[]> {
406+
public async listCollections(options?: ListCollectionsOptions): Promise<string[] | FullCollectionInfo[]> {
408407
const command: ListCollectionsCommand = {
409408
findCollections: {
410409
options: {
411-
explain: options?.nameOnly === false,
410+
explain: options?.nameOnly !== true,
412411
}
413412
},
414413
}
415414

416415
const resp = await this._httpClient.executeCommand(command, options);
417-
418-
return (options?.nameOnly !== false)
419-
? resp.status!.collections.map((name: string) => ({ name }))
420-
: resp.status!.collections;
416+
return resp.status!.collections;
421417
}
422418

423419
/**

src/data-api/errors.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { DeleteManyResult } from '@/src/data-api/types/delete/delete-many';
1717
import type { UpdateManyResult } from '@/src/data-api/types/update/update-many';
1818
import type { BulkWriteResult } from '@/src/data-api/types/misc/bulk-write';
1919
import type { RawDataApiResponse } from '@/src/api';
20+
import { SomeDoc } from '@/src/data-api/document';
2021

2122
/**
2223
* An object representing a single "soft" (2XX) error returned from the Data API, typically with an error code and a
@@ -316,7 +317,7 @@ export class DeleteManyError extends CumulativeDataAPIError {
316317
*/
317318
export class UpdateManyError extends CumulativeDataAPIError {
318319
name = 'UpdateManyError';
319-
declare public readonly partialResult: UpdateManyResult;
320+
declare public readonly partialResult: UpdateManyResult<SomeDoc>;
320321
}
321322

322323
/**
@@ -335,7 +336,7 @@ export class UpdateManyError extends CumulativeDataAPIError {
335336
*/
336337
export class BulkWriteError extends CumulativeDataAPIError {
337338
name = 'BulkWriteError';
338-
declare public readonly partialResult: BulkWriteResult;
339+
declare public readonly partialResult: BulkWriteResult<SomeDoc>;
339340
}
340341

341342
/** @internal */

src/data-api/ids.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ export class UUID {
134134
public inspect(): string {
135135
return `UUID("${this.toString()}")`;
136136
}
137+
138+
// noinspection JSUnusedGlobalSymbols
139+
/**
140+
* @internal
141+
*/
142+
public toJSON() {
143+
return { $uuid: this.toString() };
144+
}
137145
}
138146

139147
const objectIdRegex = new RegExp('^[0-9a-fA-F]{24}$');
@@ -237,6 +245,14 @@ export class ObjectId {
237245
public inspect(): string {
238246
return `ObjectId("${this.toString()}")`;
239247
}
248+
249+
// noinspection JSUnusedGlobalSymbols
250+
/**
251+
* @internal
252+
*/
253+
public toJSON() {
254+
return { $objectId: this.toString() };
255+
}
240256
}
241257

242258
/**

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,6 @@ export interface ListCollectionsOptions extends WithTimeout, WithNamespace {
5656
nameOnly?: boolean,
5757
}
5858

59-
/**
60-
* The name of a collection, used when `nameOnly` is true or undefined in {@link ListCollectionsOptions}.
61-
*
62-
* @field name - The name of the collection.
63-
*
64-
* @see ListCollectionsOptions
65-
* @see Db.listCollections
66-
*/
67-
export interface CollectionName {
68-
/**
69-
* The name of the collection.
70-
*/
71-
name: string,
72-
}
73-
7459
/**
7560
* Information about a collection, used when `nameOnly` is false in {@link ListCollectionsOptions}.
7661
*

src/data-api/types/common.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ import type { ToDotNotation } from '@/src/data-api/types';
1717

1818
export type SomeId = string | number | bigint | boolean | Date | UUID | ObjectId;
1919

20+
export type Sort =
21+
| Record<string, 1 | -1>
22+
| { $vector: { $meta: number[] } }
23+
| { $vector: number[] }
24+
| { $vectorize: string };
25+
26+
export type Projection = Record<string, 1 | 0 | true | false | Slice>;
27+
2028
/**
2129
* Specifies the sort criteria for selecting documents.
2230
*
@@ -27,18 +35,18 @@ export type SomeId = string | number | bigint | boolean | Date | UUID | ObjectId
2735
* @example
2836
* ```typescript
2937
* // Sort by name in ascending order, then by age in descending order
30-
* const sort1: SortOption<SomeDoc> = {
38+
* const sort1: StrictSort<SomeDoc> = {
3139
*   name: 1,
3240
*   age: -1,
3341
* }
3442
*
3543
* // Sort by vector distance
36-
* const sort2: SortOption<SomeDoc> = {
44+
* const sort2: StrictSort<SomeDoc> = {
3745
*   $vector: [0.23, 0.38, 0.27, 0.91, 0.21],
3846
* }
3947
* ```
4048
*/
41-
export type SortOption<Schema extends SomeDoc> =
49+
export type StrictSort<Schema extends SomeDoc> =
4250
| { [K in keyof ToDotNotation<Schema>]?: 1 | -1 }
4351
| { $vector: { $meta: number[] } }
4452
| { $vector: number[] }
@@ -52,24 +60,24 @@ export type SortOption<Schema extends SomeDoc> =
5260
* @example
5361
* ```typescript
5462
* // Include _id, name, and address.state
55-
* const projection1: ProjectionOption<SomeDoc> = {
63+
* const projection1: StrictProjection<SomeDoc> = {
5664
*   _id: 1,
5765
*   name: 1,
5866
*   'address.state': 1,
5967
* }
6068
*
6169
* // Exclude the $vector
62-
* const projection2: ProjectionOption<SomeDoc> = {
70+
* const projection2: StrictProjection<SomeDoc> = {
6371
*   $vector: 0,
6472
* }
6573
*
6674
* // Return array indices 2, 3, 4, and 5
67-
* const projection3: ProjectionOption<SomeDoc> = {
75+
* const projection3: StrictProjection<SomeDoc> = {
6876
*   test_scores: { $slice: [2, 4] },
6977
* }
7078
* ```
7179
*/
72-
export type ProjectionOption<Schema extends SomeDoc> = {
80+
export type StrictProjection<Schema extends SomeDoc> = {
7381
[K in keyof ToDotNotation<Schema> | '_id']?: any[] extends (ToDotNotation<Schema> & { _id: any })[K]
7482
? 1 | 0 | true | false | Slice
7583
: 1 | 0 | true | false;

src/data-api/types/delete/delete-one.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
// limitations under the License.
1414

1515
import type { SomeDoc } from '@/src/data-api';
16-
import type { SortOption } from '@/src/data-api/types';
16+
import type { StrictSort } from '@/src/data-api/types';
1717
import { WithTimeout } from '@/src/common/types';
1818

1919
/** @internal */
2020
export interface DeleteOneCommand {
2121
deleteOne: {
2222
filter: Record<string, unknown>;
23-
sort?: SortOption<any>;
23+
sort?: StrictSort<any>;
2424
};
2525
}
2626

@@ -41,7 +41,7 @@ export interface DeleteOneOptions<Schema extends SomeDoc> extends WithTimeout {
4141
* Defaults to `null`, where the order is not guaranteed.
4242
* @defaultValue null
4343
*/
44-
sort?: SortOption<Schema>,
44+
sort?: StrictSort<Schema>,
4545
/**
4646
* An optional vector to use of the appropriate dimensionality to perform an ANN vector search on the collection
4747
* to find the closest matching document.

0 commit comments

Comments
 (0)