Skip to content

Commit 53ff294

Browse files
remove EnsureIndexOperation and CreateIndexOperation
1 parent 51e1678 commit 53ff294

File tree

6 files changed

+52
-89
lines changed

6 files changed

+52
-89
lines changed

src/collection.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import {
5252
import {
5353
CreateIndexesOperation,
5454
type CreateIndexesOptions,
55-
CreateIndexOperation,
5655
type DropIndexesOptions,
5756
DropIndexOperation,
5857
type IndexDescription,
@@ -575,15 +574,17 @@ export class Collection<TSchema extends Document = Document> {
575574
indexSpec: IndexSpecification,
576575
options?: CreateIndexesOptions
577576
): Promise<string> {
578-
return executeOperation(
577+
const indexes = await executeOperation(
579578
this.client,
580-
new CreateIndexOperation(
579+
CreateIndexesOperation.fromIndexSpecification(
581580
this as TODO_NODE_3286,
582581
this.collectionName,
583582
indexSpec,
584583
resolveOptions(this, options)
585584
)
586585
);
586+
587+
return indexes[0];
587588
}
588589

589590
/**
@@ -623,7 +624,7 @@ export class Collection<TSchema extends Document = Document> {
623624
): Promise<string[]> {
624625
return executeOperation(
625626
this.client,
626-
new CreateIndexesOperation(
627+
CreateIndexesOperation.fromIndexDescriptionArray(
627628
this as TODO_NODE_3286,
628629
this.collectionName,
629630
indexSpecs,

src/db.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
import { executeOperation } from './operations/execute_operation';
2626
import {
2727
type CreateIndexesOptions,
28-
CreateIndexOperation,
2928
IndexInformationOperation,
3029
type IndexSpecification
3130
} from './operations/indexes';
@@ -426,10 +425,7 @@ export class Db {
426425
indexSpec: IndexSpecification,
427426
options?: CreateIndexesOptions
428427
): Promise<string> {
429-
return executeOperation(
430-
this.client,
431-
new CreateIndexOperation(this, name, indexSpec, resolveOptions(this, options))
432-
);
428+
return this.collection(name).createIndex(indexSpec, options);
433429
}
434430

435431
/**

src/operations/create_collection.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { PkFactory } from '../mongo_client';
1010
import type { Server } from '../sdam/server';
1111
import type { ClientSession } from '../sessions';
1212
import { CommandOperation, type CommandOperationOptions } from './command';
13-
import { CreateIndexOperation } from './indexes';
13+
import { CreateIndexesOperation } from './indexes';
1414
import { Aspect, defineAspects } from './operation';
1515

1616
const ILLEGAL_COMMAND_FIELDS = new Set([
@@ -167,7 +167,14 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
167167

168168
if (encryptedFields) {
169169
// Create the required index for queryable encryption support.
170-
const createIndexOp = new CreateIndexOperation(db, name, { __safeContent__: 1 }, {});
170+
// We could use `this.db.collection(name).createIndex()` to create the index,
171+
// but we can use the same session & server to avoid an extra server selection.
172+
const createIndexOp = CreateIndexesOperation.fromIndexSpecification(
173+
db,
174+
name,
175+
{ __safeContent__: 1 },
176+
{}
177+
);
171178
await createIndexOp.execute(server, session);
172179
}
173180

src/operations/indexes.ts

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,12 @@ export class IndexesOperation extends AbstractOperation<Document[]> {
204204
}
205205

206206
/** @internal */
207-
export class CreateIndexesOperation<
208-
T extends string | string[] = string[]
209-
> extends CommandOperation<T> {
207+
export class CreateIndexesOperation extends CommandOperation<string[]> {
210208
override options: CreateIndexesOptions;
211209
collectionName: string;
212210
indexes: ReadonlyArray<Omit<IndexDescription, 'key'> & { key: Map<string, IndexDirection> }>;
213211

214-
constructor(
212+
private constructor(
215213
parent: OperationParent,
216214
collectionName: string,
217215
indexes: IndexDescription[],
@@ -239,11 +237,34 @@ export class CreateIndexesOperation<
239237
});
240238
}
241239

240+
static fromIndexDescriptionArray(
241+
parent: OperationParent,
242+
collectionName: string,
243+
indexes: IndexDescription[],
244+
options?: CreateIndexesOptions
245+
): CreateIndexesOperation {
246+
return new CreateIndexesOperation(parent, collectionName, indexes, options);
247+
}
248+
249+
static fromIndexSpecification(
250+
parent: OperationParent,
251+
collectionName: string,
252+
indexSpec: IndexSpecification,
253+
options?: CreateIndexesOptions
254+
): CreateIndexesOperation {
255+
return new CreateIndexesOperation(
256+
parent,
257+
collectionName,
258+
[makeIndexSpec(indexSpec, options)],
259+
options
260+
);
261+
}
262+
242263
override get commandName() {
243264
return 'createIndexes';
244265
}
245266

246-
override async execute(server: Server, session: ClientSession | undefined): Promise<T> {
267+
override async execute(server: Server, session: ClientSession | undefined): Promise<string[]> {
247268
const options = this.options;
248269
const indexes = this.indexes;
249270

@@ -266,61 +287,7 @@ export class CreateIndexesOperation<
266287
await super.executeCommand(server, session, cmd);
267288

268289
const indexNames = indexes.map(index => index.name || '');
269-
return indexNames as T;
270-
}
271-
}
272-
273-
/** @internal */
274-
export class CreateIndexOperation extends CreateIndexesOperation<string> {
275-
constructor(
276-
parent: OperationParent,
277-
collectionName: string,
278-
indexSpec: IndexSpecification,
279-
options?: CreateIndexesOptions
280-
) {
281-
super(parent, collectionName, [makeIndexSpec(indexSpec, options)], options);
282-
}
283-
284-
override async execute(server: Server, session: ClientSession | undefined): Promise<string> {
285-
const indexNames = await super.execute(server, session);
286-
return indexNames[0];
287-
}
288-
}
289-
290-
/** @internal */
291-
export class EnsureIndexOperation extends CreateIndexOperation {
292-
db: Db;
293-
294-
constructor(
295-
db: Db,
296-
collectionName: string,
297-
indexSpec: IndexSpecification,
298-
options?: CreateIndexesOptions
299-
) {
300-
super(db, collectionName, indexSpec, options);
301-
302-
this.readPreference = ReadPreference.primary;
303-
this.db = db;
304-
this.collectionName = collectionName;
305-
}
306-
307-
override get commandName() {
308-
return 'listIndexes';
309-
}
310-
311-
override async execute(server: Server, session: ClientSession | undefined): Promise<string> {
312-
const indexName = this.indexes[0].name;
313-
const indexes = await this.db
314-
.collection(this.collectionName)
315-
.listIndexes({ session })
316-
.toArray()
317-
.catch(error => {
318-
if (error instanceof MongoError && error.code === MONGODB_ERROR_CODES.NamespaceNotFound)
319-
return [];
320-
throw error;
321-
});
322-
if (indexName && indexes.some(index => index.name === indexName)) return indexName;
323-
return super.execute(server, session);
290+
return indexNames;
324291
}
325292
}
326293

@@ -470,6 +437,4 @@ defineAspects(ListIndexesOperation, [
470437
Aspect.CURSOR_CREATING
471438
]);
472439
defineAspects(CreateIndexesOperation, [Aspect.WRITE_OPERATION]);
473-
defineAspects(CreateIndexOperation, [Aspect.WRITE_OPERATION]);
474-
defineAspects(EnsureIndexOperation, [Aspect.WRITE_OPERATION]);
475440
defineAspects(DropIndexOperation, [Aspect.WRITE_OPERATION]);

test/integration/crud/abstract_operation.test.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,23 +146,12 @@ describe('abstract operation', async function () {
146146
subclassType: mongodb.IndexesOperation,
147147
correctCommandName: 'listIndexes'
148148
},
149-
{
150-
subclassCreator: () => new mongodb.CreateIndexesOperation(db, 'bar', [{ key: { a: 1 } }]),
151-
subclassType: mongodb.CreateIndexesOperation,
152-
correctCommandName: 'createIndexes'
153-
},
154149
{
155150
subclassCreator: () =>
156-
new mongodb.CreateIndexOperation(db, 'collectionName', 'indexDescription'),
157-
subclassType: mongodb.CreateIndexOperation,
151+
mongodb.CreateIndexesOperation.fromIndexDescriptionArray(db, 'bar', [{ key: { a: 1 } }]),
152+
subclassType: mongodb.CreateIndexesOperation,
158153
correctCommandName: 'createIndexes'
159154
},
160-
{
161-
subclassCreator: () =>
162-
new mongodb.EnsureIndexOperation(db, 'collectionName', 'indexDescription'),
163-
subclassType: mongodb.EnsureIndexOperation,
164-
correctCommandName: 'listIndexes'
165-
},
166155
{
167156
subclassCreator: () => new mongodb.DropIndexOperation(collection, 'a', {}),
168157
subclassType: mongodb.DropIndexOperation,

test/unit/operations/indexes.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { expect } from 'chai';
22

33
import {
4+
CreateIndexesOperation,
45
type CreateIndexesOptions,
5-
CreateIndexOperation,
66
type IndexDirection,
77
ns
88
} from '../../mongodb';
99

10-
describe('class CreateIndexOperation', () => {
10+
describe('class CreateIndexesOperation', () => {
1111
const testCases = [
1212
{
1313
description: 'single string',
@@ -101,7 +101,12 @@ describe('class CreateIndexOperation', () => {
101101
];
102102

103103
const makeIndexOperation = (input, options: CreateIndexesOptions = {}) =>
104-
new CreateIndexOperation({ s: { namespace: ns('a.b') } }, 'b', input, options);
104+
CreateIndexesOperation.fromIndexSpecification(
105+
{ s: { namespace: ns('a.b') } },
106+
'b',
107+
input,
108+
options
109+
);
105110

106111
describe('#constructor()', () => {
107112
for (const { description, input, mapData, name } of testCases) {

0 commit comments

Comments
 (0)