Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
import {
CreateIndexesOperation,
type CreateIndexesOptions,
type DriverIndexesOptions,
type DropIndexesOptions,
DropIndexOperation,
type IndexDescription,
Expand Down Expand Up @@ -635,15 +636,17 @@ export class Collection<TSchema extends Document = Document> {
*/
async createIndex(
indexSpec: IndexSpecification,
options?: CreateIndexesOptions
options?: CreateIndexesOptions,
driverOptions?: DriverIndexesOptions
): Promise<string> {
const indexes = await executeOperation(
this.client,
CreateIndexesOperation.fromIndexSpecification(
this,
this.collectionName,
indexSpec,
resolveOptions(this, options)
resolveOptions(this, options),
driverOptions
)
);

Expand Down Expand Up @@ -683,15 +686,17 @@ export class Collection<TSchema extends Document = Document> {
*/
async createIndexes(
indexSpecs: IndexDescription[],
options?: CreateIndexesOptions
options?: CreateIndexesOptions,
driverOptions?: DriverIndexesOptions
): Promise<string[]> {
return await executeOperation(
this.client,
CreateIndexesOperation.fromIndexDescriptionArray(
this,
this.collectionName,
indexSpecs,
resolveOptions(this, { ...options, maxTimeMS: undefined })
resolveOptions(this, { ...options, maxTimeMS: undefined }),
driverOptions
)
);
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ export type {
export type { IndexInformationOptions } from './operations/indexes';
export type {
CreateIndexesOptions,
DriverIndexesOptions,
DropIndexesOptions,
IndexDescription,
IndexDescriptionCompact,
Expand Down
55 changes: 43 additions & 12 deletions src/operations/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ export interface CreateIndexesOptions extends Omit<CommandOperationOptions, 'wri
hidden?: boolean;
}

/** @public */
export interface DriverIndexesOptions {
/**
* Validate `CreateIndexesOptions` in the driver before passing them to the server.
* Setting to false allows new options supported by the server to be used right away,
* regardless of whether the driver has added support for them yet.
* This will be set to false by default in a future release.
* */
validateOptions: boolean;
}

function isSingleIndexTuple(t: unknown): t is [string, IndexDirection] {
return Array.isArray(t) && t.length === 2 && isIndexDirection(t[1]);
}
Expand Down Expand Up @@ -201,15 +212,22 @@ function constructIndexDescriptionMap(indexSpec: IndexSpecification): Map<string
* from the description and has mapped the `version` option to the `v` option.
*/
function resolveIndexDescription(
description: IndexDescription
description: IndexDescription,
validateOptions: boolean
): Omit<ResolvedIndexDescription, 'key'> {
const validProvidedOptions = Object.entries(description).filter(([optionName]) =>
VALID_INDEX_OPTIONS.has(optionName)
);
let options: [string, any][];

if (validateOptions) {
options = Object.entries(description).filter(([optionName]) =>
VALID_INDEX_OPTIONS.has(optionName)
);
} else {
options = Object.entries(description);
}

return Object.fromEntries(
// we support the `version` option, but the `createIndexes` command expects it to be the `v`
validProvidedOptions.map(([name, value]) => (name === 'version' ? ['v', value] : [name, value]))
// we support the `version` option, but the `createIndexes` command expects it to be `v`
options.map(([name, value]) => (name === 'version' ? ['v', value] : [name, value]))
);
}

Expand Down Expand Up @@ -251,7 +269,8 @@ export class CreateIndexesOperation extends CommandOperation<string[]> {
parent: OperationParent,
collectionName: string,
indexes: IndexDescription[],
options?: CreateIndexesOptions
options?: CreateIndexesOptions,
driverOptions?: DriverIndexesOptions
) {
super(parent, options);

Expand All @@ -264,7 +283,11 @@ export class CreateIndexesOperation extends CommandOperation<string[]> {
const key =
userIndex.key instanceof Map ? userIndex.key : new Map(Object.entries(userIndex.key));
const name = userIndex.name ?? Array.from(key).flat().join('_');
const validIndexOptions = resolveIndexDescription(userIndex);
const validIndexOptions = resolveIndexDescription(
userIndex,
// TODO(seanrmilligan): set to false in a future release
driverOptions?.validateOptions ?? true
);
return {
...validIndexOptions,
name,
Expand All @@ -278,20 +301,28 @@ export class CreateIndexesOperation extends CommandOperation<string[]> {
parent: OperationParent,
collectionName: string,
indexes: IndexDescription[],
options?: CreateIndexesOptions
options?: CreateIndexesOptions,
driverOptions?: DriverIndexesOptions
): CreateIndexesOperation {
return new CreateIndexesOperation(parent, collectionName, indexes, options);
return new CreateIndexesOperation(parent, collectionName, indexes, options, driverOptions);
}

static fromIndexSpecification(
parent: OperationParent,
collectionName: string,
indexSpec: IndexSpecification,
options: CreateIndexesOptions = {}
options: CreateIndexesOptions = {},
driverOptions?: DriverIndexesOptions
): CreateIndexesOperation {
const key = constructIndexDescriptionMap(indexSpec);
const description: IndexDescription = { ...options, key };
return new CreateIndexesOperation(parent, collectionName, [description], options);
return new CreateIndexesOperation(
parent,
collectionName,
[description],
options,
driverOptions
);
}

override get commandName() {
Expand Down
Loading
Loading