Skip to content

Commit c360911

Browse files
committed
Split SortOption into Sort and StrictSort
1 parent 3f3480a commit c360911

22 files changed

+277
-142
lines changed

src/data-api/collection.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,6 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
269269
public async insertMany(documents: MaybeId<Schema>[], options?: InsertManyOptions): Promise<InsertManyResult<Schema>> {
270270
const chunkSize = options?.chunkSize ?? 20;
271271

272-
if (options?.vectors && options?.vectorize) {
273-
throw new Error('Cannot set both vectors and vectorize options');
274-
}
275-
276272
if (options?.vectors) {
277273
if (options.vectors.length !== documents.length) {
278274
throw new Error('The number of vectors must match the number of documents');
@@ -292,6 +288,9 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
292288

293289
for (let i = 0, n = documents.length; i < n; i++) {
294290
if (options.vectorize[i]) {
291+
if (documents[i].$vector) {
292+
throw new Error('Vector and vectorize options cannot overlap');
293+
}
295294
documents[i] = { ...documents[i], $vectorize: options.vectorize[i] };
296295
}
297296
}
@@ -353,7 +352,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
353352
* @see StrictFilter
354353
* @see StrictUpdateFilter
355354
*/
356-
public async updateOne(filter: Filter<Schema>, update: UpdateFilter<Schema>, options?: UpdateOneOptions<Schema>): Promise<UpdateOneResult<Schema>> {
355+
public async updateOne(filter: Filter<Schema>, update: UpdateFilter<Schema>, options?: UpdateOneOptions): Promise<UpdateOneResult<Schema>> {
357356
options = coalesceVectorSpecialsIntoSort(options);
358357

359358
const command: UpdateOneCommand = {
@@ -541,7 +540,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
541540
*
542541
* @see StrictFilter
543542
*/
544-
public async replaceOne(filter: Filter<Schema>, replacement: NoId<Schema>, options?: ReplaceOneOptions<Schema>): Promise<ReplaceOneResult<Schema>> {
543+
public async replaceOne(filter: Filter<Schema>, replacement: NoId<Schema>, options?: ReplaceOneOptions): Promise<ReplaceOneResult<Schema>> {
545544
options = coalesceVectorSpecialsIntoSort(options);
546545

547546
const command: FindOneAndReplaceCommand = {
@@ -608,7 +607,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
608607
*
609608
* @see StrictFilter
610609
*/
611-
public async deleteOne(filter: Filter<Schema> = {}, options?: DeleteOneOptions<Schema>): Promise<DeleteOneResult> {
610+
public async deleteOne(filter: Filter<Schema> = {}, options?: DeleteOneOptions): Promise<DeleteOneResult> {
612611
options = coalesceVectorSpecialsIntoSort(options);
613612

614613
const command: DeleteOneCommand = {
@@ -783,7 +782,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
783782
* the Data API and the client periodically exchange new chunks of documents.
784783
* It should be noted that the behavior of the cursor in the case documents
785784
* have been added/removed after the `find` was started depends on database
786-
* internals and it is not guaranteed, nor excluded, that such "real-time"
785+
* internals, and it is not guaranteed, nor excluded, that such "real-time"
787786
* changes in the data would be picked up by the cursor.
788787
*
789788
* @param filter - A filter to select the documents to find. If not provided, all documents will be returned.
@@ -793,7 +792,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
793792
*
794793
* @see StrictFilter
795794
*/
796-
find<GetSim extends boolean = false>(filter: Filter<Schema>, options?: FindOptions<Schema, GetSim>): FindCursor<FoundDoc<Schema, GetSim>, FoundDoc<Schema, GetSim>> {
795+
find<GetSim extends boolean = false>(filter: Filter<Schema>, options?: FindOptions<GetSim>): FindCursor<FoundDoc<Schema, GetSim>, FoundDoc<Schema, GetSim>> {
797796
return new FindCursor(this.namespace, this._httpClient, filter as any, coalesceVectorSpecialsIntoSort(options)) as any;
798797
}
799798

@@ -931,7 +930,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
931930
*
932931
* @see StrictFilter
933932
*/
934-
public async findOne<GetSim extends boolean = false>(filter: Filter<Schema>, options?: FindOneOptions<Schema, GetSim>): Promise<FoundDoc<Schema, GetSim> | null> {
933+
public async findOne<GetSim extends boolean = false>(filter: Filter<Schema>, options?: FindOneOptions<GetSim>): Promise<FoundDoc<Schema, GetSim> | null> {
935934
options = coalesceVectorSpecialsIntoSort(options);
936935

937936
const command: FindOneCommand = {
@@ -1059,7 +1058,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
10591058
public async findOneAndReplace(
10601059
filter: Filter<Schema>,
10611060
replacement: NoId<Schema>,
1062-
options: FindOneAndReplaceOptions<Schema> & { includeResultMetadata: true },
1061+
options: FindOneAndReplaceOptions & { includeResultMetadata: true },
10631062
): Promise<ModifyResult<Schema>>
10641063

10651064
/**
@@ -1103,10 +1102,10 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
11031102
public async findOneAndReplace(
11041103
filter: Filter<Schema>,
11051104
replacement: NoId<Schema>,
1106-
options: FindOneAndReplaceOptions<Schema> & { includeResultMetadata?: false },
1105+
options: FindOneAndReplaceOptions & { includeResultMetadata?: false },
11071106
): Promise<WithId<Schema> | null>
11081107

1109-
public async findOneAndReplace(filter: Filter<Schema>, replacement: NoId<Schema>, options: FindOneAndReplaceOptions<Schema>): Promise<ModifyResult<Schema> | WithId<Schema> | null> {
1108+
public async findOneAndReplace(filter: Filter<Schema>, replacement: NoId<Schema>, options: FindOneAndReplaceOptions): Promise<ModifyResult<Schema> | WithId<Schema> | null> {
11101109
options = coalesceVectorSpecialsIntoSort(options);
11111110

11121111
const command: FindOneAndReplaceCommand = {
@@ -1175,7 +1174,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
11751174
*/
11761175
public async findOneAndDelete(
11771176
filter: Filter<Schema>,
1178-
options: FindOneAndDeleteOptions<Schema> & { includeResultMetadata: true },
1177+
options: FindOneAndDeleteOptions & { includeResultMetadata: true },
11791178
): Promise<ModifyResult<Schema>>
11801179

11811180
/**
@@ -1207,10 +1206,10 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
12071206
*/
12081207
public async findOneAndDelete(
12091208
filter: Filter<Schema>,
1210-
options?: FindOneAndDeleteOptions<Schema> & { includeResultMetadata?: false },
1209+
options?: FindOneAndDeleteOptions & { includeResultMetadata?: false },
12111210
): Promise<WithId<Schema> | null>
12121211

1213-
public async findOneAndDelete(filter: Filter<Schema>, options?: FindOneAndDeleteOptions<Schema>): Promise<ModifyResult<Schema> | WithId<Schema> | null> {
1212+
public async findOneAndDelete(filter: Filter<Schema>, options?: FindOneAndDeleteOptions): Promise<ModifyResult<Schema> | WithId<Schema> | null> {
12141213
options = coalesceVectorSpecialsIntoSort(options);
12151214

12161215
const command: FindOneAndDeleteCommand = {
@@ -1277,7 +1276,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
12771276
public async findOneAndUpdate(
12781277
filter: Filter<Schema>,
12791278
update: UpdateFilter<Schema>,
1280-
options: FindOneAndUpdateOptions<Schema> & { includeResultMetadata: true },
1279+
options: FindOneAndUpdateOptions & { includeResultMetadata: true },
12811280
): Promise<ModifyResult<Schema>>
12821281

12831282
/**
@@ -1318,10 +1317,10 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
13181317
public async findOneAndUpdate(
13191318
filter: Filter<Schema>,
13201319
update: UpdateFilter<Schema>,
1321-
options: FindOneAndUpdateOptions<Schema> & { includeResultMetadata?: false },
1320+
options: FindOneAndUpdateOptions & { includeResultMetadata?: false },
13221321
): Promise<WithId<Schema> | null>
13231322

1324-
public async findOneAndUpdate(filter: Filter<Schema>, update: UpdateFilter<Schema>, options: FindOneAndUpdateOptions<Schema>): Promise<ModifyResult<Schema> | WithId<Schema> | null> {
1323+
public async findOneAndUpdate(filter: Filter<Schema>, update: UpdateFilter<Schema>, options: FindOneAndUpdateOptions): Promise<ModifyResult<Schema> | WithId<Schema> | null> {
13251324
options = coalesceVectorSpecialsIntoSort(options);
13261325

13271326
const command: FindOneAndUpdateCommand = {

src/data-api/cursor.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import {
1717
FindOptions,
1818
InternalFindOptions,
1919
InternalGetMoreCommand,
20-
StrictProjection,
21-
StrictSort,
20+
Projection,
21+
Sort,
2222
} from '@/src/data-api/types';
2323
import { CursorAlreadyInitializedError, SomeDoc } from '@/src/data-api';
2424
import { DataApiHttpClient } from '@/src/api';
@@ -75,15 +75,15 @@ const enum CursorStatus {
7575
export class FindCursor<T, TRaw extends SomeDoc = SomeDoc> {
7676
private readonly _namespace: string;
7777
private readonly _httpClient: DataApiHttpClient;
78-
private readonly _options: FindOptions<SomeDoc, boolean>;
78+
private readonly _options: FindOptions<boolean>;
7979
private _filter: Filter<SomeDoc>;
8080
private _mapping?: (doc: unknown) => T;
8181

8282
private _buffer: TRaw[] = [];
8383
private _nextPageState?: string | null;
8484
private _state = CursorStatus.Uninitialized;
8585

86-
constructor(namespace: string, httpClient: DataApiHttpClient, filter: Filter<SomeDoc>, options?: FindOptions<SomeDoc, boolean>) {
86+
constructor(namespace: string, httpClient: DataApiHttpClient, filter: Filter<SomeDoc>, options?: FindOptions<boolean>) {
8787
this._namespace = namespace;
8888
this._httpClient = httpClient;
8989
this._filter = filter;
@@ -141,7 +141,7 @@ export class FindCursor<T, TRaw extends SomeDoc = SomeDoc> {
141141
*
142142
* @return The cursor.
143143
*/
144-
sort(sort: StrictSort<TRaw>): FindCursor<T, TRaw> {
144+
sort(sort: Sort): 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, RRaw extends SomeDoc = SomeDoc>(projection: StrictProjection<TRaw>): FindCursor<R, RRaw> {
218+
project<R = any, RRaw extends SomeDoc = SomeDoc>(projection: Projection): FindCursor<R, RRaw> {
219219
this._assertUninitialized();
220220
this._options.projection = projection;
221221
return this as any;

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

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,21 @@ import { ToDotNotation } from '@/src/data-api/types/dot-notation';
2323
* @field dimension - The dimension of the vectors.
2424
* @field metric - The similarity metric to use for the vector search.
2525
*/
26-
export type VectorOptions =
27-
| ManualVectorOptions
28-
| VectorizeOptions;
29-
30-
/**
31-
* Represents the options for the vector search.
32-
*
33-
* **If not specified, the collection will not support vector search.**
34-
*
35-
* @field dimension - The dimension of the vectors.
36-
* @field metric - The similarity metric to use for the vector search.
37-
* @field service - Options related to the vectorization pipeline, to specify an embedding service.
38-
*/
39-
export interface ManualVectorOptions {
26+
export interface VectorOptions {
4027
/**
4128
* The dimension of the vectors stored in the collection.
4229
*/
43-
dimension: number,
30+
dimension?: number,
4431
/**
4532
* The similarity metric to use for the vector search.
4633
*
4734
* See [intro to vector databases](https://docs.datastax.com/en/astra/astra-db-vector/get-started/concepts.html#metrics) for more details.
4835
*/
49-
metric: 'cosine' | 'euclidean' | 'dot_product',
36+
metric?: 'cosine' | 'euclidean' | 'dot_product',
5037
/**
5138
* @alpha
5239
*/
53-
service?: never,
54-
}
55-
56-
/**
57-
* @alpha
58-
*/
59-
export interface VectorizeOptions {
60-
service: VectorizeServiceOptions,
61-
dimension?: never,
62-
metric?: never,
40+
service?: VectorizeServiceOptions,
6341
}
6442

6543
/**

src/data-api/types/common.ts

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

15-
import { ObjectId, SomeDoc, UUID } from '@/src/data-api';
15+
import { ObjectId, SomeDoc, UUID, WithId } from '@/src/data-api';
1616
import type { ToDotNotation } from '@/src/data-api/types';
1717

1818
export type SomeId = string | number | bigint | boolean | Date | UUID | ObjectId;
@@ -35,19 +35,19 @@ export type Projection = Record<string, 1 | 0 | true | false | Slice>;
3535
* @example
3636
* ```typescript
3737
* // Sort by name in ascending order, then by age in descending order
38-
* const sort1: StrictSort<SomeDoc> = {
38+
* const sort1: Sort<SomeDoc> = {
3939
*   name: 1,
4040
*   age: -1,
4141
* }
4242
*
4343
* // Sort by vector distance
44-
* const sort2: StrictSort<SomeDoc> = {
44+
* const sort2: Sort<SomeDoc> = {
4545
*   $vector: [0.23, 0.38, 0.27, 0.91, 0.21],
4646
* }
4747
* ```
4848
*/
4949
export type StrictSort<Schema extends SomeDoc> =
50-
| { [K in keyof ToDotNotation<Schema>]?: 1 | -1 }
50+
| { [K in keyof ToDotNotation<WithId<Schema>>]?: 1 | -1 }
5151
| { $vector: { $meta: number[] } }
5252
| { $vector: number[] }
5353
| { $vectorize: string };
@@ -60,25 +60,25 @@ export type StrictSort<Schema extends SomeDoc> =
6060
* @example
6161
* ```typescript
6262
* // Include _id, name, and address.state
63-
* const projection1: StrictProjection<SomeDoc> = {
63+
* const projection1: Projection<SomeDoc> = {
6464
*   _id: 1,
6565
*   name: 1,
6666
*   'address.state': 1,
6767
* }
6868
*
6969
* // Exclude the $vector
70-
* const projection2: StrictProjection<SomeDoc> = {
70+
* const projection2: Projection<SomeDoc> = {
7171
*   $vector: 0,
7272
* }
7373
*
7474
* // Return array indices 2, 3, 4, and 5
75-
* const projection3: StrictProjection<SomeDoc> = {
75+
* const projection3: Projection<SomeDoc> = {
7676
*   test_scores: { $slice: [2, 4] },
7777
* }
7878
* ```
7979
*/
8080
export type StrictProjection<Schema extends SomeDoc> = {
81-
[K in keyof ToDotNotation<Schema> | '_id']?: any[] extends (ToDotNotation<Schema> & { _id: any })[K]
81+
[K in keyof ToDotNotation<WithId<Schema>>]?: any[] extends (ToDotNotation<WithId<Schema>>)[K]
8282
? 1 | 0 | true | false | Slice
8383
: 1 | 0 | true | false;
8484
};

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

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

15-
import type { SomeDoc } from '@/src/data-api';
16-
import type { StrictSort } from '@/src/data-api/types';
15+
import type { Sort } from '@/src/data-api/types';
1716
import { WithTimeout } from '@/src/common/types';
1817

1918
/** @internal */
2019
export interface DeleteOneCommand {
2120
deleteOne: {
2221
filter: Record<string, unknown>;
23-
sort?: StrictSort<any>;
22+
sort?: Sort;
2423
};
2524
}
2625

@@ -32,7 +31,7 @@ export interface DeleteOneCommand {
3231
*
3332
* @see Collection.deleteOne
3433
*/
35-
export interface DeleteOneOptions<Schema extends SomeDoc> extends WithTimeout {
34+
export interface DeleteOneOptions extends WithTimeout {
3635
/**
3736
* The order in which to apply the update if the filter selects multiple documents.
3837
*
@@ -41,7 +40,7 @@ export interface DeleteOneOptions<Schema extends SomeDoc> extends WithTimeout {
4140
* Defaults to `null`, where the order is not guaranteed.
4241
* @defaultValue null
4342
*/
44-
sort?: StrictSort<Schema>,
43+
sort?: Sort,
4544
/**
4645
* An optional vector to use of the appropriate dimensionality to perform an ANN vector search on the collection
4746
* to find the closest matching document.

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

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

15-
import type { StrictProjection, StrictSort } from '@/src/data-api/types';
16-
import { SomeDoc } from '@/src/data-api';
15+
import type { Projection, Sort } from '@/src/data-api/types';
1716
import { WithTimeout } from '@/src/common/types';
1817

1918
/** @internal */
2019
export interface FindOneAndDeleteCommand {
2120
findOneAndDelete: {
2221
filter?: Record<string, unknown>;
23-
sort?: StrictSort<any>;
24-
projection?: StrictProjection<any>;
22+
sort?: Sort;
23+
projection?: Projection;
2524
};
2625
}
2726

@@ -35,7 +34,7 @@ export interface FindOneAndDeleteCommand {
3534
*
3635
* @see Collection.findOneAndDelete
3736
*/
38-
export interface FindOneAndDeleteOptions<Schema extends SomeDoc> extends WithTimeout {
37+
export interface FindOneAndDeleteOptions extends WithTimeout {
3938
/**
4039
* The order in which to apply the update if the filter selects multiple documents.
4140
*
@@ -44,7 +43,7 @@ export interface FindOneAndDeleteOptions<Schema extends SomeDoc> extends WithTim
4443
* Defaults to `null`, where the order is not guaranteed.
4544
* @defaultValue null
4645
*/
47-
sort?: StrictSort<Schema>,
46+
sort?: Sort,
4847
/**
4948
* An optional vector to use of the appropriate dimensionality to perform an ANN vector search on the collection
5049
* to find the closest matching document.
@@ -96,7 +95,7 @@ export interface FindOneAndDeleteOptions<Schema extends SomeDoc> extends WithTim
9695
* console.log(doc.age);
9796
* ```
9897
*/
99-
projection?: StrictProjection<Schema>,
98+
projection?: Projection,
10099
/**
101100
* When true, returns alongside the document, an `ok` field with a value of 1 if the command executed successfully.
102101
*

0 commit comments

Comments
 (0)