Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-6019): indexExists always returns false when full is set to true #4034

Merged
Merged
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
2 changes: 1 addition & 1 deletion src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
} from '../error';
import type { Filter, OneOrMore, OptionalId, UpdateFilter, WithoutId } from '../mongo_types';
import type { CollationOptions, CommandOperationOptions } from '../operations/command';
import { maybeAddIdToDocuments } from '../operations/common_functions';
import { DeleteOperation, type DeleteStatement, makeDeleteStatement } from '../operations/delete';
import { executeOperation } from '../operations/execute_operation';
import { InsertOperation } from '../operations/insert';
Expand All @@ -21,6 +20,7 @@ import { makeUpdateStatement, UpdateOperation, type UpdateStatement } from '../o
import type { Server } from '../sdam/server';
import type { Topology } from '../sdam/topology';
import type { ClientSession } from '../sessions';
import { maybeAddIdToDocuments } from '../utils';
import {
applyRetryableWrites,
type Callback,
Expand Down
70 changes: 37 additions & 33 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import type {
} from './mongo_types';
import type { AggregateOptions } from './operations/aggregate';
import { BulkWriteOperation } from './operations/bulk_write';
import type { IndexInformationOptions } from './operations/common_functions';
import { CountOperation, type CountOptions } from './operations/count';
import { CountDocumentsOperation, type CountDocumentsOptions } from './operations/count_documents';
import {
Expand All @@ -49,19 +48,16 @@ import {
FindOneAndUpdateOperation,
type FindOneAndUpdateOptions
} from './operations/find_and_modify';
import {
CreateIndexesOperation,
type CreateIndexesOptions,
CreateIndexOperation,
type DropIndexesOptions,
DropIndexOperation,
type IndexDescription,
IndexesOperation,
IndexExistsOperation,
IndexInformationOperation,
type IndexSpecification,
type ListIndexesOptions
import type {
CreateIndexesOptions,
DropIndexesOptions,
IndexDescription,
IndexDirection,
IndexInformationOptions,
IndexSpecification,
ListIndexesOptions
} from './operations/indexes';
import { CreateIndexesOperation, DropIndexOperation } from './operations/indexes';
import {
InsertManyOperation,
type InsertManyResult,
Expand Down Expand Up @@ -575,15 +571,17 @@ export class Collection<TSchema extends Document = Document> {
indexSpec: IndexSpecification,
options?: CreateIndexesOptions
): Promise<string> {
return executeOperation(
const indexes = await executeOperation(
this.client,
new CreateIndexOperation(
this as TODO_NODE_3286,
CreateIndexesOperation.fromIndexSpecification(
this,
this.collectionName,
indexSpec,
resolveOptions(this, options)
)
);

return indexes[0];
}

/**
Expand Down Expand Up @@ -623,8 +621,8 @@ export class Collection<TSchema extends Document = Document> {
): Promise<string[]> {
return executeOperation(
this.client,
new CreateIndexesOperation(
this as TODO_NODE_3286,
CreateIndexesOperation.fromIndexDescriptionArray(
this,
this.collectionName,
indexSpecs,
resolveOptions(this, { ...options, maxTimeMS: undefined })
Expand Down Expand Up @@ -680,14 +678,14 @@ export class Collection<TSchema extends Document = Document> {
* @param indexes - One or more index names to check.
* @param options - Optional settings for the command
*/
async indexExists(
indexes: string | string[],
options?: IndexInformationOptions
): Promise<boolean> {
return executeOperation(
this.client,
new IndexExistsOperation(this as TODO_NODE_3286, indexes, resolveOptions(this, options))
async indexExists(indexes: string | string[], options?: ListIndexesOptions): Promise<boolean> {
const indexNames: string[] = Array.isArray(indexes) ? indexes : [indexes];
const allIndexes: Set<string> = new Set(
await this.listIndexes(options)
.map(({ name }) => name)
.toArray()
);
return indexNames.every(name => allIndexes.has(name));
}

/**
Expand All @@ -696,10 +694,7 @@ export class Collection<TSchema extends Document = Document> {
* @param options - Optional settings for the command
*/
async indexInformation(options?: IndexInformationOptions): Promise<Document> {
return executeOperation(
this.client,
new IndexInformationOperation(this.s.db, this.collectionName, resolveOptions(this, options))
);
return this.indexes({ ...options, full: options?.full ?? false });
}

/**
Expand Down Expand Up @@ -804,10 +799,19 @@ export class Collection<TSchema extends Document = Document> {
* @param options - Optional settings for the command
*/
async indexes(options?: IndexInformationOptions): Promise<Document[]> {
return executeOperation(
this.client,
new IndexesOperation(this as TODO_NODE_3286, resolveOptions(this, options))
);
const indexes = await this.listIndexes(options).toArray();
const full = options?.full ?? true;
if (full) {
return indexes;
}

const object: Record<
string,
Array<[name: string, direction: IndexDirection]>
> = Object.fromEntries(indexes.map(({ name, key }) => [name, Object.entries(key)]));

// @ts-expect-error TODO(NODE-6029): fix return type of `indexes()` and `indexInformation()`
return object;
}

/**
Expand Down
15 changes: 6 additions & 9 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { MongoClient, PkFactory } from './mongo_client';
import type { TODO_NODE_3286 } from './mongo_types';
import type { AggregateOptions } from './operations/aggregate';
import { CollectionsOperation } from './operations/collections';
import type { IndexInformationOptions } from './operations/common_functions';
import {
CreateCollectionOperation,
type CreateCollectionOptions
Expand All @@ -24,9 +23,9 @@ import {
} from './operations/drop';
import { executeOperation } from './operations/execute_operation';
import {
CreateIndexesOperation,
type CreateIndexesOptions,
CreateIndexOperation,
IndexInformationOperation,
type IndexInformationOptions,
type IndexSpecification
} from './operations/indexes';
import type { CollectionInfo, ListCollectionsOptions } from './operations/list_collections';
Expand Down Expand Up @@ -426,10 +425,11 @@ export class Db {
indexSpec: IndexSpecification,
options?: CreateIndexesOptions
): Promise<string> {
return executeOperation(
const indexes = await executeOperation(
this.client,
new CreateIndexOperation(this, name, indexSpec, resolveOptions(this, options))
CreateIndexesOperation.fromIndexSpecification(this, name, indexSpec, options)
);
return indexes[0];
}

/**
Expand Down Expand Up @@ -480,10 +480,7 @@ export class Db {
* @param options - Optional settings for the command
*/
async indexInformation(name: string, options?: IndexInformationOptions): Promise<Document> {
return executeOperation(
this.client,
new IndexInformationOperation(this, name, resolveOptions(this, options))
);
return this.collection(name).indexInformation(resolveOptions(this, options));
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ export type {
CommandOperationOptions,
OperationParent
} from './operations/command';
export type { IndexInformationOptions } from './operations/common_functions';
export type { CountOptions } from './operations/count';
export type { CountDocumentsOptions } from './operations/count_documents';
export type {
Expand All @@ -466,6 +465,7 @@ export type {
FindOneAndReplaceOptions,
FindOneAndUpdateOptions
} from './operations/find_and_modify';
export type { IndexInformationOptions } from './operations/indexes';
export type {
CreateIndexesOptions,
DropIndexesOptions,
Expand Down
79 changes: 0 additions & 79 deletions src/operations/common_functions.ts

This file was deleted.

9 changes: 7 additions & 2 deletions src/operations/create_collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { PkFactory } from '../mongo_client';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { CommandOperation, type CommandOperationOptions } from './command';
import { CreateIndexOperation } from './indexes';
import { CreateIndexesOperation } from './indexes';
import { Aspect, defineAspects } from './operation';

const ILLEGAL_COMMAND_FIELDS = new Set([
Expand Down Expand Up @@ -167,7 +167,12 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {

if (encryptedFields) {
// Create the required index for queryable encryption support.
const createIndexOp = new CreateIndexOperation(db, name, { __safeContent__: 1 }, {});
const createIndexOp = CreateIndexesOperation.fromIndexSpecification(
db,
name,
{ __safeContent__: 1 },
{}
);
await createIndexOp.execute(server, session);
}

Expand Down
Loading
Loading