Skip to content

Commit 948725a

Browse files
committed
wip
1 parent 298f6eb commit 948725a

File tree

13 files changed

+167
-124
lines changed

13 files changed

+167
-124
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/operations/add_user.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as crypto from 'crypto';
22
import { Aspect, defineAspects } from './operation';
33
import { CommandOperation, CommandOperationOptions } from './command';
44
import { MongoError } from '../error';
5-
import { Callback, getTopology } from '../utils';
5+
import { Callback, deepFreeze, getTopology } from '../utils';
66
import type { Document } from '../bson';
77
import type { Server } from '../sdam/server';
88
import type { Db } from '../db';
@@ -22,8 +22,23 @@ export class AddUserOperation extends CommandOperation<AddUserOptions, Document>
2222
db: Db;
2323
username: string;
2424
password?: string;
25+
private roles;
26+
private customData;
27+
28+
getOptions(): Readonly<AddUserOptions> {
29+
return deepFreeze({
30+
...super.getOptions(),
31+
roles: this.roles,
32+
customData: this.customData
33+
});
34+
}
2535

26-
constructor(db: Db, username: string, password: string | undefined, options?: AddUserOptions) {
36+
constructor(
37+
db: Db,
38+
username: string,
39+
password: string | undefined,
40+
options: AddUserOptions = {}
41+
) {
2742
super(db, options);
2843

2944
// Special case where there is no password ($external users)
@@ -35,23 +50,23 @@ export class AddUserOperation extends CommandOperation<AddUserOptions, Document>
3550
this.db = db;
3651
this.username = username;
3752
this.password = password;
53+
this.roles = options.roles;
54+
this.customData = options.customData;
55+
56+
// Error out if digestPassword set
57+
if (options?.digestPassword != null) {
58+
throw new MongoError(
59+
'The digestPassword option is not supported via add_user. ' +
60+
"Please use db.command('createUser', ...) instead for this option."
61+
);
62+
}
3863
}
3964

4065
execute(server: Server, callback: Callback<Document>): void {
4166
const db = this.db;
4267
const username = this.username;
4368
const password = this.password;
44-
const options = this.options;
45-
46-
// Error out if digestPassword set
47-
if (options.digestPassword != null) {
48-
return callback(
49-
new MongoError(
50-
'The digestPassword option is not supported via add_user. ' +
51-
"Please use db.command('createUser', ...) instead for this option."
52-
)
53-
);
54-
}
69+
const options = this.getOptions();
5570

5671
// Get additional values
5772
let roles: string[] = [];

src/operations/aggregate.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CommandOperation, CommandOperationOptions, OperationParent } from './command';
22
import { ReadPreference } from '../read_preference';
33
import { MongoError } from '../error';
4-
import { maxWireVersion } from '../utils';
4+
import { deepFreeze, maxWireVersion } from '../utils';
55
import { Aspect, defineAspects, Hint } from './operation';
66
import type { Callback } from '../utils';
77
import type { Document } from '../bson';
@@ -40,8 +40,34 @@ export class AggregateOperation<T = Document> extends CommandOperation<Aggregate
4040
target: string | typeof DB_AGGREGATE_COLLECTION;
4141
pipeline: Document[];
4242
hasWriteStage: boolean;
43+
protected allowDiskUse;
44+
protected batchSize;
45+
protected bypassDocumentValidation;
46+
protected cursor;
47+
protected explain;
48+
protected maxTimeMS;
49+
protected maxAwaitTimeMS;
50+
protected collation;
51+
protected hint;
52+
protected out;
53+
54+
getOptions(): Readonly<AggregateOptions> {
55+
return deepFreeze({
56+
...super.getOptions(),
57+
batchSize: this.batchSize,
58+
allowDiskUse: this.allowDiskUse,
59+
cursor: this.cursor,
60+
bypassDocumentValidation: this.bypassDocumentValidation,
61+
maxTimeMS: this.maxTimeMS,
62+
explain: this.explain,
63+
collation: this.collation,
64+
maxAwaitTimeMS: this.maxAwaitTimeMS,
65+
out: this.out,
66+
hint: this.hint
67+
});
68+
}
4369

44-
constructor(parent: OperationParent, pipeline: Document[], options?: AggregateOptions) {
70+
constructor(parent: OperationParent, pipeline: Document[], options: AggregateOptions = {}) {
4571
super(parent, options);
4672

4773
this.target =
@@ -76,6 +102,17 @@ export class AggregateOperation<T = Document> extends CommandOperation<Aggregate
76102
if (options?.cursor != null && typeof options.cursor !== 'object') {
77103
throw new MongoError('cursor options must be an object');
78104
}
105+
106+
this.batchSize = options.batchSize;
107+
this.allowDiskUse = options.allowDiskUse;
108+
this.cursor = options.cursor;
109+
this.bypassDocumentValidation = options.bypassDocumentValidation;
110+
this.maxTimeMS = options.maxTimeMS;
111+
this.explain = options.explain;
112+
this.collation = options.collation;
113+
this.maxAwaitTimeMS = options.maxAwaitTimeMS;
114+
this.out = options.out;
115+
this.hint = options.hint;
79116
}
80117

81118
get canRetryRead(): boolean {
@@ -87,7 +124,7 @@ export class AggregateOperation<T = Document> extends CommandOperation<Aggregate
87124
}
88125

89126
execute(server: Server, callback: Callback<T>): void {
90-
const options: AggregateOptions = this.options;
127+
const options = this.getOptions();
91128
const serverWireVersion = maxWireVersion(server);
92129
const command: Document = { aggregate: this.target, pipeline: this.pipeline };
93130

src/operations/bulk_write.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { applyRetryableWrites, applyWriteConcern, Callback } from '../utils';
1+
import { applyRetryableWrites, applyWriteConcern, Callback, deepFreeze } from '../utils';
22
import { OperationBase } from './operation';
33
import { resolveBSONOptions } from '../bson';
44
import { WriteConcern } from '../write_concern';
@@ -15,6 +15,18 @@ import type { Server } from '../sdam/server';
1515
export class BulkWriteOperation extends OperationBase<BulkWriteOptions, BulkWriteResult> {
1616
collection: Collection;
1717
operations: AnyBulkWriteOperation[];
18+
protected bypassDocumentValidation;
19+
protected ordered;
20+
protected forceServerObjectId;
21+
22+
getOptions(): Readonly<BulkWriteOptions> {
23+
return deepFreeze({
24+
...super.getOptions(),
25+
bypassDocumentValidation: this.bypassDocumentValidation,
26+
ordered: this.ordered,
27+
forceServerObjectId: this.forceServerObjectId
28+
});
29+
}
1830

1931
constructor(
2032
collection: Collection,
@@ -28,12 +40,15 @@ export class BulkWriteOperation extends OperationBase<BulkWriteOptions, BulkWrit
2840

2941
// Assign BSON serialize options to OperationBase, preferring options over collection options
3042
this.bsonOptions = resolveBSONOptions(options, collection);
43+
this.bypassDocumentValidation = options.bypassDocumentValidation;
44+
this.ordered = options.ordered;
45+
this.forceServerObjectId = options.forceServerObjectId;
3146
}
3247

3348
execute(server: Server, callback: Callback<BulkWriteResult>): void {
3449
const coll = this.collection;
3550
const operations = this.operations;
36-
const options = { ...this.options, ...this.bsonOptions };
51+
const options = this.getOptions();
3752

3853
// Create the bulk operation
3954
const bulk: BulkOperationBase =

src/operations/collections.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { OperationBase, OperationOptions } from './operation';
2-
import { loadCollection } from '../dynamic_loaders';
3-
import type { Callback } from '../utils';
2+
import { Callback, deepFreeze } from '../utils';
43
import type { Db } from '../db';
54

65
// eslint-disable-next-line
7-
import type { Collection } from '../collection';
6+
import { Collection } from '../collection';
87
import type { Server } from '../sdam/server';
98

109
export interface CollectionsOptions extends OperationOptions {
@@ -14,20 +13,26 @@ export interface CollectionsOptions extends OperationOptions {
1413
/** @internal */
1514
export class CollectionsOperation extends OperationBase<CollectionsOptions, Collection[]> {
1615
db: Db;
16+
protected nameOnly;
17+
18+
getOptions(): Readonly<CollectionsOptions> {
19+
return deepFreeze({
20+
...super.getOptions(),
21+
nameOnly: this.nameOnly
22+
});
23+
}
1724

1825
constructor(db: Db, options: CollectionsOptions) {
1926
super(options);
2027

2128
this.db = db;
29+
this.nameOnly = options.nameOnly ?? true;
2230
}
2331

2432
execute(server: Server, callback: Callback<Collection[]>): void {
2533
const db = this.db;
26-
let options: CollectionsOptions = this.options;
27-
28-
const Collection = loadCollection();
34+
const options = this.getOptions();
2935

30-
options = Object.assign({}, options, { nameOnly: true });
3136
// Let's get the collection names
3237
db.listCollections({}, options).toArray((err, documents) => {
3338
if (err || !documents) return callback(err);

src/operations/command.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,16 @@ export abstract class CommandOperation<
6868
protected retryWrites;
6969
protected noResponse;
7070

71-
get builtOptions(): Readonly<CommandOperationOptions> {
71+
getOptions(): Readonly<CommandOperationOptions> {
7272
return deepFreeze({
73-
...super.builtOptions,
73+
...super.getOptions(),
7474
collation: this.collation,
7575
maxTimeMS: this.maxTimeMS,
7676
comment: this.comment,
7777
retryWrites: this.retryWrites,
7878
noResponse: this.noResponse,
79-
fullResponse: this.fullResponse
79+
fullResponse: this.fullResponse,
80+
fullResult: !!this.fullResponse // this is the prop that server.command expects, we keep this here so not to refreeze
8081
// Override with proper type
8182
// writeConcern: this.writeConcern,
8283
// readConcern: this.readConcern
@@ -139,6 +140,7 @@ export abstract class CommandOperation<
139140
executeCommand(server: Server, cmd: Document, callback: Callback): void {
140141
// TODO: consider making this a non-enumerable property
141142
this.server = server;
143+
const options = this.getOptions();
142144

143145
const serverWireVersion = maxWireVersion(server);
144146
const inTransaction = this.session && this.session.inTransaction();
@@ -147,7 +149,7 @@ export abstract class CommandOperation<
147149
Object.assign(cmd, { readConcern: this.readConcern });
148150
}
149151

150-
if (this.builtOptions.collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
152+
if (options.collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
151153
callback(
152154
new MongoError(
153155
`Server ${server.name}, which reports wire version ${serverWireVersion}, does not support collation`
@@ -161,29 +163,24 @@ export abstract class CommandOperation<
161163
Object.assign(cmd, { writeConcern: this.writeConcern });
162164
}
163165

164-
if (this.builtOptions.collation && typeof this.builtOptions.collation === 'object') {
165-
Object.assign(cmd, { collation: this.builtOptions.collation });
166+
if (options.collation && typeof options.collation === 'object') {
167+
Object.assign(cmd, { collation: options.collation });
166168
}
167169
}
168170

169-
if (typeof this.builtOptions.maxTimeMS === 'number') {
170-
cmd.maxTimeMS = this.builtOptions.maxTimeMS;
171+
if (typeof options.maxTimeMS === 'number') {
172+
cmd.maxTimeMS = options.maxTimeMS;
171173
}
172174

173-
if (typeof this.builtOptions.comment === 'string') {
174-
cmd.comment = this.builtOptions.comment;
175+
if (typeof options.comment === 'string') {
176+
cmd.comment = options.comment;
175177
}
176178

177179
if (this.logger && this.logger.isDebug()) {
178180
this.logger.debug(`executing command ${JSON.stringify(cmd)} against ${this.ns}`);
179181
}
180182

181-
server.command(
182-
this.ns.toString(),
183-
cmd,
184-
{ fullResult: !!this.fullResponse, ...this.builtOptions },
185-
callback
186-
);
183+
server.command(this.ns.toString(), cmd, options, callback);
187184
}
188185
}
189186

0 commit comments

Comments
 (0)