Skip to content

Commit

Permalink
refactor(command): support full response as command op option
Browse files Browse the repository at this point in the history
We were mutating the passed in `options` in order to convey that
a given operation should return the full server response. Now this
is passed in through an option in the operation constructor.
  • Loading branch information
mbroadst authored and daprahamian committed Aug 13, 2019
1 parent 6acef6d commit 778928f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
4 changes: 1 addition & 3 deletions lib/operations/aggregate.js
Expand Up @@ -12,9 +12,7 @@ const MIN_WIRE_VERSION_$OUT_READ_CONCERN_SUPPORT = 8;

class AggregateOperation extends CommandOperationV2 {
constructor(parent, pipeline, options) {
// ensure we receive an unchanged raw response from the server (for cursor logic)
options.full = true;
super(parent, options);
super(parent, options, { fullResponse: true });

this.target =
parent.s.namespace && parent.s.namespace.collection
Expand Down
9 changes: 6 additions & 3 deletions lib/operations/command_v2.js
Expand Up @@ -12,7 +12,7 @@ const MongoError = require('../error').MongoError;
const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5;

class CommandOperationV2 extends OperationBase {
constructor(parent, options) {
constructor(parent, options, operationOptions) {
super(options);

this.ns = parent.s.namespace.withCollection('$cmd');
Expand All @@ -21,6 +21,10 @@ class CommandOperationV2 extends OperationBase {
this.writeConcern = resolveWriteConcern(parent, this.options);
this.explain = false;

if (operationOptions && typeof operationOptions.fullResponse === 'boolean') {
this.fullResponse = true;
}

// TODO: A lot of our code depends on having the read preference in the options. This should
// go away, but also requires massive test rewrites.
this.options.readPreference = this.readPreference;
Expand Down Expand Up @@ -78,14 +82,13 @@ class CommandOperationV2 extends OperationBase {
this.logger.debug(`executing command ${JSON.stringify(cmd)} against ${this.ns}`);
}

let fullResponse = this.options.full;
server.command(this.ns.toString(), cmd, this.options, (err, result) => {
if (err) {
callback(err, null);
return;
}

if (fullResponse) {
if (this.fullResponse) {
callback(null, result);
return;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/operations/list_collections.js
Expand Up @@ -26,8 +26,7 @@ function listCollectionsTransforms(databaseName) {

class ListCollectionsOperation extends CommandOperationV2 {
constructor(db, filter, options) {
super(db, options);
this.options.full = true;
super(db, options, { fullResponse: true });

this.db = db;
this.filter = filter;
Expand Down
3 changes: 1 addition & 2 deletions lib/operations/list_indexes.js
Expand Up @@ -9,8 +9,7 @@ const LIST_INDEXES_WIRE_VERSION = 3;

class ListIndexesOperation extends CommandOperationV2 {
constructor(collection, options) {
super(collection, options);
this.options.full = true;
super(collection, options, { fullResponse: true });

this.collectionNamespace = collection.s.namespace;
}
Expand Down

0 comments on commit 778928f

Please sign in to comment.