Skip to content

Commit 595aae4

Browse files
remove 'IndexInformationOperation'
1 parent 935f24a commit 595aae4

File tree

4 files changed

+25
-57
lines changed

4 files changed

+25
-57
lines changed

src/collection.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import {
5555
type DropIndexesOptions,
5656
DropIndexOperation,
5757
type IndexDescription,
58-
IndexInformationOperation,
5958
type IndexSpecification,
6059
type ListIndexesOptions
6160
} from './operations/indexes';
@@ -684,7 +683,7 @@ export class Collection<TSchema extends Document = Document> {
684683
indexes: string | string[],
685684
options?: IndexInformationOptions
686685
): Promise<boolean> {
687-
const indexNames: Set<string> = new Set([indexes].flat());
686+
const indexNames: string[] = [indexes].flat();
688687
const allIndexes: string[] = await this.listIndexes(options)
689688
.map(({ name }) => name)
690689
.toArray();
@@ -698,10 +697,12 @@ export class Collection<TSchema extends Document = Document> {
698697
* @param options - Optional settings for the command
699698
*/
700699
async indexInformation(options?: IndexInformationOptions): Promise<Document> {
701-
return executeOperation(
702-
this.client,
703-
new IndexInformationOperation(this.s.db, this.collectionName, resolveOptions(this, options))
704-
);
700+
const resolvedOptions: IndexInformationOptions = {
701+
readPreference: options?.readPreference,
702+
session: options?.session,
703+
full: false
704+
};
705+
return this.indexes(resolvedOptions);
705706
}
706707

707708
/**
@@ -806,7 +807,19 @@ export class Collection<TSchema extends Document = Document> {
806807
* @param options - Optional settings for the command
807808
*/
808809
async indexes(options?: IndexInformationOptions): Promise<Document[]> {
809-
return this.listIndexes(options).toArray();
810+
const indexes = await this.listIndexes(options).toArray();
811+
const full = options?.full === true;
812+
if (full) {
813+
return indexes;
814+
}
815+
816+
const info: Record<string, Array<[string, unknown]>> = {};
817+
for (const { name, key } of indexes) {
818+
info[name] = Object.entries(key);
819+
}
820+
821+
// @ts-expect-error The return type is broken
822+
return info;
810823
}
811824

812825
/**

src/db.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ import {
2323
type DropDatabaseOptions
2424
} from './operations/drop';
2525
import { executeOperation } from './operations/execute_operation';
26-
import {
27-
type CreateIndexesOptions,
28-
IndexInformationOperation,
29-
type IndexSpecification
30-
} from './operations/indexes';
26+
import { type CreateIndexesOptions, type IndexSpecification } from './operations/indexes';
3127
import type { CollectionInfo, ListCollectionsOptions } from './operations/list_collections';
3228
import { ProfilingLevelOperation, type ProfilingLevelOptions } from './operations/profiling_level';
3329
import { RemoveUserOperation, type RemoveUserOptions } from './operations/remove_user';
@@ -476,10 +472,7 @@ export class Db {
476472
* @param options - Optional settings for the command
477473
*/
478474
async indexInformation(name: string, options?: IndexInformationOptions): Promise<Document> {
479-
return executeOperation(
480-
this.client,
481-
new IndexInformationOperation(this, name, resolveOptions(this, options))
482-
);
475+
return this.collection(name).indexInformation(resolveOptions(this, options));
483476
}
484477

485478
/**

src/operations/indexes.ts

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { Document } from '../bson';
22
import type { Collection } from '../collection';
3-
import type { Db } from '../db';
4-
import { MongoCompatibilityError, MONGODB_ERROR_CODES, MongoError } from '../error';
3+
import { MongoCompatibilityError } from '../error';
54
import { type OneOrMore } from '../mongo_types';
6-
import { ReadPreference } from '../read_preference';
75
import type { Server } from '../sdam/server';
86
import type { ClientSession } from '../sessions';
97
import { isObject, maxWireVersion, type MongoDBNamespace } from '../utils';
@@ -13,8 +11,7 @@ import {
1311
type CommandOperationOptions,
1412
type OperationParent
1513
} from './command';
16-
import { indexInformation, type IndexInformationOptions } from './common_functions';
17-
import { AbstractOperation, Aspect, defineAspects } from './operation';
14+
import { Aspect, defineAspects } from './operation';
1815

1916
const VALID_INDEX_OPTIONS = new Set([
2017
'background',
@@ -337,35 +334,6 @@ export class ListIndexesOperation extends CommandOperation<Document> {
337334
}
338335
}
339336

340-
/** @internal */
341-
export class IndexInformationOperation extends AbstractOperation<Document> {
342-
override options: IndexInformationOptions;
343-
db: Db;
344-
name: string;
345-
346-
constructor(db: Db, name: string, options?: IndexInformationOptions) {
347-
super(options);
348-
this.options = options ?? {};
349-
this.db = db;
350-
this.name = name;
351-
}
352-
353-
override get commandName() {
354-
return 'listIndexes' as const;
355-
}
356-
357-
override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
358-
const db = this.db;
359-
const name = this.name;
360-
361-
return indexInformation(db, name, {
362-
...this.options,
363-
readPreference: this.readPreference,
364-
session
365-
});
366-
}
367-
}
368-
369337
defineAspects(ListIndexesOperation, [
370338
Aspect.READ_OPERATION,
371339
Aspect.RETRYABLE,

test/integration/crud/abstract_operation.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ describe('abstract operation', async function () {
2020
'IsCappedOperation',
2121
'BulkWriteOperation',
2222
'IndexOperation',
23-
'CollectionsOperation',
24-
'IndexInformationOperation'
23+
'CollectionsOperation'
2524
];
2625

2726
const sameServerOnlyOperationSubclasses = ['GetMoreOperation', 'KillCursorsOperation'];
@@ -156,11 +155,6 @@ describe('abstract operation', async function () {
156155
subclassType: mongodb.ListIndexesOperation,
157156
correctCommandName: 'listIndexes'
158157
},
159-
{
160-
subclassCreator: () => new mongodb.IndexInformationOperation(db, 'a', {}),
161-
subclassType: mongodb.IndexInformationOperation,
162-
correctCommandName: 'listIndexes'
163-
},
164158
{
165159
subclassCreator: () =>
166160
new mongodb.InsertOperation(collection.fullNamespace, [{ a: 1 }], {}),

0 commit comments

Comments
 (0)