Skip to content

Commit 3970985

Browse files
create collection
1 parent 40c54ae commit 3970985

File tree

2 files changed

+55
-38
lines changed

2 files changed

+55
-38
lines changed

src/operations/create_collection.ts

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
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 { maxWireVersion } from '../utils';
15+
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
1416
import { executeOperation } from './execute_operation';
1517
import { CreateIndexesOperation } from './indexes';
1618
import { Aspect, defineAspects } from './operation';
@@ -110,7 +112,8 @@ const INVALID_QE_VERSION =
110112
'Driver support of Queryable Encryption is incompatible with server. Upgrade server to use Queryable Encryption.';
111113

112114
/** @internal */
113-
export class CreateCollectionOperation extends CommandOperation<Collection> {
115+
export class CreateCollectionOperation extends ModernizedCommandOperation<Collection> {
116+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
114117
override options: CreateCollectionOptions;
115118
db: Db;
116119
name: string;
@@ -127,25 +130,19 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
127130
return 'create' as const;
128131
}
129132

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+
}
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+
maxWireVersion(connection) < 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

test/integration/node-specific/bson-options/use_bigint_64.test.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,20 @@ describe('useBigInt64 option', function () {
171171
beforeEach(async function () {
172172
client = await this.configuration.newClient().connect();
173173
db = client.db('bsonOptions', { promoteLongs: false, useBigInt64: true });
174+
175+
await db.createCollection('foo');
176+
await db.createCollection('bar');
177+
});
178+
179+
afterEach(async function () {
180+
await db.dropDatabase();
174181
});
175182

176183
it('throws a BSONError', async function () {
177-
const e = await db.createCollection('bsonError').catch(e => e);
184+
const e = await db
185+
.listCollections()
186+
.toArray()
187+
.catch(e => e);
178188
expect(e).to.be.instanceOf(BSON.BSONError);
179189
});
180190
});
@@ -186,8 +196,11 @@ describe('useBigInt64 option', function () {
186196
});
187197

188198
it('throws a BSONError', async function () {
189-
const e = await db
190-
.createCollection('bsonError', { promoteLongs: false, useBigInt64: true })
199+
const collection = db.collection('bsonError', { promoteLongs: false, useBigInt64: true });
200+
201+
const e = await collection
202+
.insertOne({ name: 'bailey ' })
203+
.then(() => null)
191204
.catch(e => e);
192205
expect(e).to.be.instanceOf(BSON.BSONError);
193206
});
@@ -229,11 +242,21 @@ describe('useBigInt64 option', function () {
229242
describe('when set at DB level', function () {
230243
beforeEach(async function () {
231244
client = await this.configuration.newClient().connect();
232-
db = client.db('bsonOptions', { promoteValues: false, useBigInt64: true });
245+
db = client.db('bsonOptions', { promoteLongs: false, useBigInt64: true });
246+
247+
await db.createCollection('foo');
248+
await db.createCollection('bar');
249+
});
250+
251+
afterEach(async function () {
252+
await db.dropDatabase();
233253
});
234254

235255
it('throws a BSONError', async function () {
236-
const e = await db.createCollection('bsonError').catch(e => e);
256+
const e = await db
257+
.listCollections()
258+
.toArray()
259+
.catch(e => e);
237260
expect(e).to.be.instanceOf(BSON.BSONError);
238261
});
239262
});
@@ -245,8 +268,11 @@ describe('useBigInt64 option', function () {
245268
});
246269

247270
it('throws a BSONError', async function () {
248-
const e = await db
249-
.createCollection('bsonError', { promoteValues: false, useBigInt64: true })
271+
const collection = db.collection('bsonError', { promoteValues: false, useBigInt64: true });
272+
273+
const e = await collection
274+
.insertOne({ name: 'bailey ' })
275+
.then(() => null)
250276
.catch(e => e);
251277
expect(e).to.be.instanceOf(BSON.BSONError);
252278
});

0 commit comments

Comments
 (0)