Skip to content

Commit cf2d3ed

Browse files
Revert "Revert "create collection""
This reverts commit dacd888.
1 parent dacd888 commit cf2d3ed

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

src/operations/create_collection.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
import { type Connection } from '..';
12
import type { Document } from '../bson';
23
import {
34
MIN_SUPPORTED_QE_SERVER_VERSION,
45
MIN_SUPPORTED_QE_WIRE_VERSION
56
} from '../cmap/wire_protocol/constants';
7+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
68
import { Collection } from '../collection';
79
import type { Db } from '../db';
810
import { MongoCompatibilityError } from '../error';
911
import type { PkFactory } from '../mongo_client';
10-
import type { Server } from '../sdam/server';
1112
import type { ClientSession } from '../sessions';
1213
import { TimeoutContext } from '../timeout';
13-
import { CommandOperation, type CommandOperationOptions } from './command';
14+
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
1415
import { executeOperation } from './execute_operation';
1516
import { CreateIndexesOperation } from './indexes';
1617
import { Aspect, defineAspects } from './operation';
@@ -110,7 +111,8 @@ const INVALID_QE_VERSION =
110111
'Driver support of Queryable Encryption is incompatible with server. Upgrade server to use Queryable Encryption.';
111112

112113
/** @internal */
113-
export class CreateCollectionOperation extends CommandOperation<Collection> {
114+
export class CreateCollectionOperation extends ModernizedCommandOperation<Collection> {
115+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
114116
override options: CreateCollectionOptions;
115117
db: Db;
116118
name: string;
@@ -127,25 +129,20 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
127129
return 'create' as const;
128130
}
129131

130-
override async execute(
131-
server: Server,
132-
session: ClientSession | undefined,
133-
timeoutContext: TimeoutContext
134-
): Promise<Collection> {
135-
const db = this.db;
136-
const name = this.name;
137-
const options = this.options;
138-
139-
const cmd: Document = { create: name };
140-
for (const [option, value] of Object.entries(options)) {
132+
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
133+
const cmd: Document = { create: this.name };
134+
for (const [option, value] of Object.entries(this.options)) {
141135
if (value != null && typeof value !== 'function' && !ILLEGAL_COMMAND_FIELDS.has(option)) {
142136
cmd[option] = value;
143137
}
144138
}
139+
return cmd;
140+
}
145141

146-
// otherwise just execute the command
147-
await super.executeCommand(server, session, cmd, timeoutContext);
148-
return new Collection(db, name, options);
142+
override handleOk(
143+
_response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
144+
): Collection<Document> {
145+
return new Collection(this.db, this.name, this.options);
149146
}
150147
}
151148

@@ -167,23 +164,17 @@ export async function createCollections<TSchema extends Document>(
167164

168165
if (encryptedFields) {
169166
class CreateSupportingFLEv2CollectionOperation extends CreateCollectionOperation {
170-
override execute(
171-
server: Server,
172-
session: ClientSession | undefined,
173-
timeoutContext: TimeoutContext
174-
): Promise<Collection> {
175-
// Creating a QE collection required min server of 7.0.0
176-
// TODO(NODE-5353): Get wire version information from connection.
167+
override buildCommandDocument(connection: Connection, session?: ClientSession): Document {
177168
if (
178-
!server.loadBalanced &&
179-
server.description.maxWireVersion < MIN_SUPPORTED_QE_WIRE_VERSION
169+
!connection.description.loadBalanced &&
170+
Number(connection.description.maxWireVersion) < MIN_SUPPORTED_QE_WIRE_VERSION
180171
) {
181172
throw new MongoCompatibilityError(
182173
`${INVALID_QE_VERSION} The minimum server version required is ${MIN_SUPPORTED_QE_SERVER_VERSION}`
183174
);
184175
}
185176

186-
return super.execute(server, session, timeoutContext);
177+
return super.buildCommandDocument(connection, session);
187178
}
188179
}
189180

0 commit comments

Comments
 (0)