1
+ import { type Connection } from '..' ;
1
2
import type { Document } from '../bson' ;
2
3
import {
3
4
MIN_SUPPORTED_QE_SERVER_VERSION ,
4
5
MIN_SUPPORTED_QE_WIRE_VERSION
5
6
} from '../cmap/wire_protocol/constants' ;
7
+ import { MongoDBResponse } from '../cmap/wire_protocol/responses' ;
6
8
import { Collection } from '../collection' ;
7
9
import type { Db } from '../db' ;
8
10
import { MongoCompatibilityError } from '../error' ;
9
11
import type { PkFactory } from '../mongo_client' ;
10
- import type { Server } from '../sdam/server' ;
11
12
import type { ClientSession } from '../sessions' ;
12
13
import { TimeoutContext } from '../timeout' ;
13
- import { CommandOperation , type CommandOperationOptions } from './command' ;
14
+ import { maxWireVersion } from '../utils' ;
15
+ import { type CommandOperationOptions , ModernizedCommandOperation } from './command' ;
14
16
import { executeOperation } from './execute_operation' ;
15
17
import { CreateIndexesOperation } from './indexes' ;
16
18
import { Aspect , defineAspects } from './operation' ;
@@ -110,7 +112,8 @@ const INVALID_QE_VERSION =
110
112
'Driver support of Queryable Encryption is incompatible with server. Upgrade server to use Queryable Encryption.' ;
111
113
112
114
/** @internal */
113
- export class CreateCollectionOperation extends CommandOperation < Collection > {
115
+ export class CreateCollectionOperation extends ModernizedCommandOperation < Collection > {
116
+ override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse ;
114
117
override options : CreateCollectionOptions ;
115
118
db : Db ;
116
119
name : string ;
@@ -127,25 +130,19 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
127
130
return 'create' as const ;
128
131
}
129
132
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 ) ) {
141
- if ( value != null && typeof value !== 'function' && ! ILLEGAL_COMMAND_FIELDS . has ( option ) ) {
142
- cmd [ option ] = value ;
143
- }
144
- }
133
+ override buildCommandDocument ( _connection : Connection , _session ?: ClientSession ) : Document {
134
+ const isOptionValid = ( [ k , v ] : [ k : string , v : unknown ] ) =>
135
+ v != null && typeof v !== 'function' && ! ILLEGAL_COMMAND_FIELDS . has ( k ) ;
136
+ return {
137
+ create : this . name ,
138
+ ...Object . fromEntries ( Object . entries ( this . options ) . filter ( isOptionValid ) )
139
+ } ;
140
+ }
145
141
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 ) ;
149
146
}
150
147
}
151
148
@@ -167,23 +164,17 @@ export async function createCollections<TSchema extends Document>(
167
164
168
165
if ( encryptedFields ) {
169
166
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 {
177
168
if (
178
- ! server . loadBalanced &&
179
- server . description . maxWireVersion < MIN_SUPPORTED_QE_WIRE_VERSION
169
+ ! connection . description . loadBalanced &&
170
+ maxWireVersion ( connection ) < MIN_SUPPORTED_QE_WIRE_VERSION
180
171
) {
181
172
throw new MongoCompatibilityError (
182
173
`${ INVALID_QE_VERSION } The minimum server version required is ${ MIN_SUPPORTED_QE_SERVER_VERSION } `
183
174
) ;
184
175
}
185
176
186
- return super . execute ( server , session , timeoutContext ) ;
177
+ return super . buildCommandDocument ( connection , session ) ;
187
178
}
188
179
}
189
180
0 commit comments