Skip to content

Commit 26e0bfa

Browse files
mbroadstdaprahamian
authored andcommitted
refactor(command): move common command construction to base cmd op
1 parent 85071c4 commit 26e0bfa

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

lib/operations/aggregate.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,16 @@ class AggregateOperation extends CommandOperationV2 {
6161
execute(server, callback) {
6262
const options = this.options;
6363
const serverWireVersion = maxWireVersion(server);
64-
const inTransaction = this.session && this.session.inTransaction();
65-
6664
const command = { aggregate: this.target, pipeline: this.pipeline };
6765

68-
if (
69-
this.readConcern &&
70-
(!this.hasWriteStage || serverWireVersion >= MIN_WIRE_VERSION_$OUT_READ_CONCERN_SUPPORT) &&
71-
!inTransaction
72-
) {
73-
Object.assign(command, { readConcern: this.readConcern });
66+
if (this.hasWriteStage && serverWireVersion < MIN_WIRE_VERSION_$OUT_READ_CONCERN_SUPPORT) {
67+
this.readConcern = null;
7468
}
7569

7670
if (serverWireVersion >= 5) {
7771
if (this.hasWriteStage && this.writeConcern) {
7872
Object.assign(command, { writeConcern: this.writeConcern });
7973
}
80-
81-
if (options.collation && typeof options.collation === 'object') {
82-
Object.assign(command, { collation: options.collation });
83-
}
8474
}
8575

8676
if (options.bypassDocumentValidation === true) {
@@ -91,10 +81,6 @@ class AggregateOperation extends CommandOperationV2 {
9181
command.allowDiskUse = options.allowDiskUse;
9282
}
9383

94-
if (typeof options.maxTimeMS === 'number') {
95-
command.maxTimeMS = options.maxTimeMS;
96-
}
97-
9884
if (options.hint) {
9985
command.hint = options.hint;
10086
}
@@ -104,10 +90,6 @@ class AggregateOperation extends CommandOperationV2 {
10490
command.explain = options.explain;
10591
}
10692

107-
if (typeof options.comment === 'string') {
108-
command.comment = options.comment;
109-
}
110-
11193
command.cursor = options.cursor || {};
11294
if (options.batchSize && !this.hasWriteStage) {
11395
command.cursor.batchSize = options.batchSize;

lib/operations/command_v2.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22

3+
const Aspect = require('./operation').Aspect;
34
const OperationBase = require('./operation').OperationBase;
45
const resolveReadPreference = require('../utils').resolveReadPreference;
56
const ReadConcern = require('../read_concern');
67
const WriteConcern = require('../write_concern');
8+
const maxWireVersion = require('../core/utils').maxWireVersion;
79

810
class CommandOperationV2 extends OperationBase {
911
constructor(parent, options) {
@@ -31,6 +33,32 @@ class CommandOperationV2 extends OperationBase {
3133
// TODO: consider making this a non-enumerable property
3234
this.server = server;
3335

36+
const options = this.options;
37+
const serverWireVersion = maxWireVersion(server);
38+
const inTransaction = this.session && this.session.inTransaction();
39+
40+
if (this.readConcern && this.hasAspect(Aspect.READ_OPERATION) && !inTransaction) {
41+
Object.assign(cmd, { readConcern: this.readConcern });
42+
}
43+
44+
if (serverWireVersion >= 5) {
45+
if (this.writeConcern && this.hasAspect(Aspect.WRITE_OPERATION)) {
46+
Object.assign(cmd, { writeConcern: this.writeConcern });
47+
}
48+
49+
if (options.collation && typeof options.collation === 'object') {
50+
Object.assign(cmd, { collation: options.collation });
51+
}
52+
}
53+
54+
if (typeof options.maxTimeMS === 'number') {
55+
cmd.maxTimeMS = options.maxTimeMS;
56+
}
57+
58+
if (typeof options.comment === 'string') {
59+
cmd.comment = options.comment;
60+
}
61+
3462
if (this.logger && this.logger.isDebug()) {
3563
this.logger.debug(`executing command ${JSON.stringify(cmd)} against ${this.ns}`);
3664
}

0 commit comments

Comments
 (0)