Skip to content

Commit 5b360ed

Browse files
committed
put bsonOptions property on parents
1 parent 7e111c1 commit 5b360ed

File tree

10 files changed

+50
-39
lines changed

10 files changed

+50
-39
lines changed

src/bson.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { OperationParent } from './operations/command';
12
// import type * as _BSON from 'bson';
23
// let BSON: typeof _BSON = require('bson');
34
// try {
@@ -71,30 +72,23 @@ export function pluckBSONSerializeOptions(options: BSONSerializeOptions): BSONSe
7172
}
7273

7374
/**
74-
* Merge the given BSONSerializeOptions, preferring options over parentOptions, and optionally
75+
* Merge the given BSONSerializeOptions, preferring options over the parent's options, and
7576
* substituting defaults for values not set.
7677
*
7778
* @internal
78-
*
79-
* @param options - The primary options to use
80-
* @param parentOptions - The secondary options to use
81-
* @param includeDefaultOptions - Whether to include default options for any values not set
8279
*/
83-
export function inheritBSONOptions(
80+
export function resolveBSONOptions(
8481
options?: BSONSerializeOptions,
85-
parentOptions?: BSONSerializeOptions,
86-
includeDefaultOptions?: boolean
82+
parent?: OperationParent
8783
): BSONSerializeOptions {
88-
const defaults = {
89-
raw: false,
90-
promoteLongs: true,
91-
promoteValues: true,
92-
promoteBuffers: false,
93-
ignoreUndefined: false,
94-
serializeFunctions: false,
95-
fieldsAsRaw: {}
84+
const parentOptions = parent?.bsonOptions;
85+
return {
86+
raw: options?.raw ?? parentOptions?.raw ?? false,
87+
promoteLongs: options?.promoteLongs ?? parentOptions?.promoteLongs ?? true,
88+
promoteValues: options?.promoteValues ?? parentOptions?.promoteValues ?? true,
89+
promoteBuffers: options?.promoteBuffers ?? parentOptions?.promoteBuffers ?? false,
90+
ignoreUndefined: options?.ignoreUndefined ?? parentOptions?.ignoreUndefined ?? false,
91+
serializeFunctions: options?.serializeFunctions ?? parentOptions?.serializeFunctions ?? false,
92+
fieldsAsRaw: options?.fieldsAsRaw ?? parentOptions?.fieldsAsRaw ?? {}
9693
};
97-
98-
const base = includeDefaultOptions ? defaults : {};
99-
return pluckBSONSerializeOptions({ ...base, ...parentOptions, ...options });
10094
}

src/collection.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
MongoDBNamespace,
99
Callback
1010
} from './utils';
11-
import { ObjectId, Document, BSONSerializeOptions, inheritBSONOptions } from './bson';
11+
import { ObjectId, Document, BSONSerializeOptions, resolveBSONOptions } from './bson';
1212
import { MongoError } from './error';
1313
import { UnorderedBulkOperation } from './bulk/unordered';
1414
import { OrderedBulkOperation } from './bulk/ordered';
@@ -126,6 +126,7 @@ export interface CollectionPrivate {
126126
options: any;
127127
namespace: MongoDBNamespace;
128128
readPreference?: ReadPreference;
129+
bsonOptions: BSONSerializeOptions;
129130
slaveOk?: boolean;
130131
collectionHint?: Hint;
131132
readConcern?: ReadConcern;
@@ -182,13 +183,11 @@ export class Collection implements OperationParent {
182183
}
183184
},
184185
readPreference: ReadPreference.fromOptions(options),
186+
bsonOptions: resolveBSONOptions(options, db),
185187
readConcern: ReadConcern.fromOptions(options),
186188
writeConcern: WriteConcern.fromOptions(options),
187189
slaveOk: options == null || options.slaveOk == null ? db.slaveOk : options.slaveOk
188190
};
189-
190-
// Modify internal state with inherited BSON options
191-
this.s.options = { ...this.s.options, ...inheritBSONOptions(options, db.s.options, false) };
192191
}
193192

194193
/**
@@ -236,6 +235,10 @@ export class Collection implements OperationParent {
236235
return this.s.readPreference;
237236
}
238237

238+
get bsonOptions(): BSONSerializeOptions {
239+
return this.s.bsonOptions;
240+
}
241+
239242
/**
240243
* The current writeConcern of the collection. If not explicitly defined for
241244
* this collection, will be inherited from the parent DB
@@ -1284,7 +1287,7 @@ export class Collection implements OperationParent {
12841287
options = options || {};
12851288
// Give function's options precedence over session options.
12861289
if (options.ignoreUndefined == null) {
1287-
options.ignoreUndefined = this.s.options.ignoreUndefined;
1290+
options.ignoreUndefined = this.bsonOptions.ignoreUndefined;
12881291
}
12891292

12901293
return new UnorderedBulkOperation(this, options);
@@ -1295,7 +1298,7 @@ export class Collection implements OperationParent {
12951298
options = options || {};
12961299
// Give function's options precedence over session's options.
12971300
if (options.ignoreUndefined == null) {
1298-
options.ignoreUndefined = this.s.options.ignoreUndefined;
1301+
options.ignoreUndefined = this.bsonOptions.ignoreUndefined;
12991302
}
13001303

13011304
return new OrderedBulkOperation(this, options);

src/db.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { deprecate } from 'util';
22
import { emitDeprecatedOptionWarning, Callback } from './utils';
33
import { loadAdmin } from './dynamic_loaders';
44
import { AggregationCursor, CommandCursor } from './cursor';
5-
import { ObjectId, Code, Document, BSONSerializeOptions } from './bson';
5+
import { ObjectId, Code, Document, BSONSerializeOptions, resolveBSONOptions } from './bson';
66
import { ReadPreference, ReadPreferenceLike } from './read_preference';
77
import { MongoError } from './error';
88
import { Collection, CollectionOptions } from './collection';
@@ -94,6 +94,7 @@ export interface DbPrivate {
9494
readPreference?: ReadPreference;
9595
pkFactory: PkFactory;
9696
readConcern?: ReadConcern;
97+
bsonOptions: BSONSerializeOptions;
9798
writeConcern?: WriteConcern;
9899
namespace: MongoDBNamespace;
99100
}
@@ -171,6 +172,8 @@ export class Db implements OperationParent {
171172
logger: new Logger('Db', options),
172173
// Unpack read preference
173174
readPreference: ReadPreference.fromOptions(options),
175+
// Merge bson options TODO: include client bson options, after NODE-2850
176+
bsonOptions: resolveBSONOptions(options),
174177
// Set up the primary key factory or fallback to ObjectId
175178
pkFactory: options?.pkFactory ?? {
176179
createPk() {
@@ -218,6 +221,10 @@ export class Db implements OperationParent {
218221
return this.s.readPreference;
219222
}
220223

224+
get bsonOptions(): BSONSerializeOptions {
225+
return this.s.bsonOptions;
226+
}
227+
221228
// get the write Concern
222229
get writeConcern(): WriteConcern | undefined {
223230
return this.s.writeConcern;

src/mongo_client.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { connect, validOptions } from './operations/connect';
1010
import { PromiseProvider } from './promise_provider';
1111
import { Logger } from './logger';
1212
import { ReadConcern, ReadConcernLevelLike, ReadConcernLike } from './read_concern';
13-
import type { BSONSerializeOptions, Document } from './bson';
13+
import { BSONSerializeOptions, Document, resolveBSONOptions } from './bson';
1414
import type { AutoEncryptionOptions } from './deps';
1515
import type { CompressorName } from './cmap/wire_protocol/compression';
1616
import type { AuthMechanism } from './cmap/auth/defaultAuthProviders';
@@ -222,6 +222,7 @@ export interface MongoClientPrivate {
222222
readConcern?: ReadConcern;
223223
writeConcern?: WriteConcern;
224224
readPreference: ReadPreference;
225+
bsonOptions: BSONSerializeOptions;
225226
namespace: MongoDBNamespace;
226227
logger: Logger;
227228
}
@@ -284,6 +285,7 @@ export class MongoClient extends EventEmitter implements OperationParent {
284285
readConcern: ReadConcern.fromOptions(options),
285286
writeConcern: WriteConcern.fromOptions(options),
286287
readPreference: ReadPreference.fromOptions(options) || ReadPreference.primary,
288+
bsonOptions: resolveBSONOptions(options),
287289
namespace: new MongoDBNamespace('admin'),
288290
logger: options?.logger ?? new Logger('MongoClient')
289291
};
@@ -301,6 +303,10 @@ export class MongoClient extends EventEmitter implements OperationParent {
301303
return this.s.readPreference;
302304
}
303305

306+
get bsonOptions(): BSONSerializeOptions {
307+
return this.s.bsonOptions;
308+
}
309+
304310
get logger(): Logger {
305311
return this.s.logger;
306312
}

src/operations/bulk_write.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { applyRetryableWrites, applyWriteConcern, Callback } from '../utils';
22
import { OperationBase } from './operation';
3-
import { inheritBSONOptions } from '../bson';
3+
import { resolveBSONOptions } from '../bson';
44
import { WriteConcern } from '../write_concern';
55
import type { Collection } from '../collection';
66
import type {
@@ -27,7 +27,7 @@ export class BulkWriteOperation extends OperationBase<BulkWriteOptions, BulkWrit
2727
this.operations = operations;
2828

2929
// Assign BSON serialize options to OperationBase, preferring options over collection options
30-
this.bsonOptions = inheritBSONOptions(options, collection.s.options, true);
30+
this.bsonOptions = resolveBSONOptions(options, collection);
3131
}
3232

3333
execute(server: Server, callback: Callback<BulkWriteResult>): void {

src/operations/command.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { commandSupportsReadConcern } from '../sessions';
77
import { MongoError } from '../error';
88
import type { Logger } from '../logger';
99
import type { Server } from '../sdam/server';
10-
import { BSONSerializeOptions, Document, inheritBSONOptions } from '../bson';
10+
import { BSONSerializeOptions, Document, resolveBSONOptions } from '../bson';
1111
import type { CollationOptions } from '../cmap/wire_protocol/write_command';
1212
import type { ReadConcernLike } from './../read_concern';
1313

@@ -39,12 +39,12 @@ export interface CommandOperationOptions extends OperationOptions, WriteConcernO
3939
export interface OperationParent {
4040
s: {
4141
namespace: MongoDBNamespace;
42-
options?: BSONSerializeOptions;
4342
};
4443
readConcern?: ReadConcern;
4544
writeConcern?: WriteConcern;
4645
readPreference?: ReadPreference;
4746
logger?: Logger;
47+
bsonOptions?: BSONSerializeOptions;
4848
}
4949

5050
/** @internal */
@@ -95,7 +95,7 @@ export abstract class CommandOperation<
9595
}
9696

9797
// Assign BSON serialize options to OperationBase, preferring options over parent options.
98-
this.bsonOptions = inheritBSONOptions(options, parent?.s.options, true);
98+
this.bsonOptions = resolveBSONOptions(options, parent);
9999
}
100100

101101
abstract execute(server: Server, callback: Callback<TResult>): void;

src/operations/common_functions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ export function updateDocuments(
250250
// Do we return the actual result document
251251
// Either use override on the function, or go back to default on either the collection
252252
// level or db
253-
finalOptions.serializeFunctions = options.serializeFunctions || coll.s.options.serializeFunctions;
253+
finalOptions.serializeFunctions =
254+
options.serializeFunctions || coll.bsonOptions.serializeFunctions;
254255

255256
// Execute the operation
256257
const op: Document = { q: selector, u: document };

src/operations/find_one.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { OperationBase } from './operation';
22
import type { Callback } from '../utils';
3-
import { Document, inheritBSONOptions } from '../bson';
3+
import { Document, resolveBSONOptions } from '../bson';
44
import type { Collection } from '../collection';
55
import type { FindOptions } from './find';
66
import { MongoError } from '../error';
@@ -18,7 +18,7 @@ export class FindOneOperation extends OperationBase<FindOptions, Document> {
1818
this.query = query;
1919

2020
// Assign BSON serialize options to OperationBase, preferring options over collection options
21-
this.bsonOptions = inheritBSONOptions(options, collection.s.options, true);
21+
this.bsonOptions = resolveBSONOptions(options, collection);
2222
}
2323

2424
execute(server: Server, callback: Callback<Document>): void {

src/operations/insert_many.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MongoError } from '../error';
44
import { prepareDocs } from './common_functions';
55
import type { Callback } from '../utils';
66
import type { Collection } from '../collection';
7-
import { ObjectId, Document, inheritBSONOptions } from '../bson';
7+
import { ObjectId, Document, resolveBSONOptions } from '../bson';
88
import type { BulkWriteResult, BulkWriteOptions } from '../bulk/common';
99
import type { Server } from '../sdam/server';
1010

@@ -32,7 +32,7 @@ export class InsertManyOperation extends OperationBase<BulkWriteOptions, InsertM
3232
this.docs = docs;
3333

3434
// Assign BSON serialize options to OperationBase, preferring options over collection options
35-
this.bsonOptions = inheritBSONOptions(options, collection.s.options, true);
35+
this.bsonOptions = resolveBSONOptions(options, collection);
3636
}
3737

3838
execute(server: Server, callback: Callback<InsertManyResult>): void {

test/functional/mongo_client.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ describe('MongoClient', function () {
5757

5858
test.equal(true, db.s.options.forceServerObjectId);
5959
test.equal(1, db.s.pkFactory.createPk());
60-
test.equal(true, db.s.options.serializeFunctions);
61-
test.equal(true, db.s.options.raw);
60+
test.equal(true, db.bsonOptions.serializeFunctions);
61+
test.equal(true, db.bsonOptions.raw);
6262
test.equal(10, db.s.options.numberOfRetries);
6363
test.equal(0, db.s.options.bufferMaxEntries);
6464

0 commit comments

Comments
 (0)