Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 38 additions & 61 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
const Query = require('./query');
const { applyGlobalMaxTimeMS, applyGlobalDiskUse } = require('./helpers/query/applyGlobalOption');
const clone = require('./helpers/clone');
const castSort = require('./helpers/query/castSort');
const getConstructorName = require('./helpers/getConstructorName');

Check failure on line 13 in lib/aggregate.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

'getConstructorName' is assigned a value but never used
const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscriminatorPipeline');
const stringifyFunctionOperators = require('./helpers/aggregate/stringifyFunctionOperators');
const utils = require('./utils');
Expand Down Expand Up @@ -99,7 +100,7 @@
* @api private
*/

Aggregate.prototype._optionsForExec = function() {
Aggregate.prototype._optionsForExec = function () {

Check failure on line 103 in lib/aggregate.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
const options = this.options || {};

const asyncLocalStorage = this.model()?.db?.base.transactionAsyncLocalStorage?.getStore();
Expand Down Expand Up @@ -127,7 +128,7 @@
* @api public
*/

Aggregate.prototype.model = function(model) {
Aggregate.prototype.model = function (model) {

Check failure on line 131 in lib/aggregate.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
if (arguments.length === 0) {
return this._model;
}
Expand Down Expand Up @@ -163,7 +164,7 @@
* @api public
*/

Aggregate.prototype.append = function() {
Aggregate.prototype.append = function () {

Check failure on line 167 in lib/aggregate.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
const args = (arguments.length === 1 && Array.isArray(arguments[0]))
? arguments[0]
: [...arguments];
Expand Down Expand Up @@ -200,7 +201,7 @@
* @return {Aggregate}
* @api public
*/
Aggregate.prototype.addFields = function(arg) {
Aggregate.prototype.addFields = function (arg) {

Check failure on line 204 in lib/aggregate.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
if (typeof arg !== 'object' || arg === null || Array.isArray(arg)) {
throw new Error('Invalid addFields() argument. Must be an object');
}
Expand Down Expand Up @@ -239,15 +240,15 @@
* @api public
*/

Aggregate.prototype.project = function(arg) {
Aggregate.prototype.project = function (arg) {

Check failure on line 243 in lib/aggregate.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
const fields = {};

if (typeof arg === 'object' && !Array.isArray(arg)) {
Object.keys(arg).forEach(function(field) {
Object.keys(arg).forEach(function (field) {

Check failure on line 247 in lib/aggregate.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
fields[field] = arg[field];
});
} else if (arguments.length === 1 && typeof arg === 'string') {
arg.split(/\s+/).forEach(function(field) {
arg.split(/\s+/).forEach(function (field) {

Check failure on line 251 in lib/aggregate.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
if (!field) {
return;
}
Expand Down Expand Up @@ -402,7 +403,7 @@
* @api public
*/

Aggregate.prototype.near = function(arg) {
Aggregate.prototype.near = function (arg) {

Check failure on line 406 in lib/aggregate.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
if (arg == null) {
throw new MongooseError('Aggregate `near()` must be called with non-nullish argument');
}
Expand All @@ -423,8 +424,8 @@
* define methods
*/

'group match skip limit out densify fill'.split(' ').forEach(function($operator) {
Aggregate.prototype[$operator] = function(arg) {
'group match skip limit out densify fill'.split(' ').forEach(function ($operator) {

Check failure on line 427 in lib/aggregate.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
Aggregate.prototype[$operator] = function (arg) {
const op = {};
op['$' + $operator] = arg;
return this.append(op);
Expand All @@ -449,7 +450,7 @@
* @api public
*/

Aggregate.prototype.unwind = function() {
Aggregate.prototype.unwind = function () {
const args = [...arguments];

const res = [];
Expand Down Expand Up @@ -488,7 +489,7 @@
* @api public
*/

Aggregate.prototype.replaceRoot = function(newRoot) {
Aggregate.prototype.replaceRoot = function (newRoot) {
let ret;

if (typeof newRoot === 'string') {
Expand Down Expand Up @@ -517,7 +518,7 @@
* @api public
*/

Aggregate.prototype.count = function(fieldName) {
Aggregate.prototype.count = function (fieldName) {
return this.append({ $count: fieldName });
};

Expand All @@ -539,7 +540,7 @@
* @api public
*/

Aggregate.prototype.sortByCount = function(arg) {
Aggregate.prototype.sortByCount = function (arg) {
if (arg && typeof arg === 'object') {
return this.append({ $sortByCount: arg });
} else if (typeof arg === 'string') {
Expand All @@ -565,7 +566,7 @@
* @api public
*/

Aggregate.prototype.lookup = function(options) {
Aggregate.prototype.lookup = function (options) {
return this.append({ $lookup: options });
};

Expand All @@ -585,7 +586,7 @@
* @api public
*/

Aggregate.prototype.graphLookup = function(options) {
Aggregate.prototype.graphLookup = function (options) {
const cloneOptions = {};
if (options) {
if (!utils.isObject(options)) {
Expand Down Expand Up @@ -618,7 +619,7 @@
* @api public
*/

Aggregate.prototype.sample = function(size) {
Aggregate.prototype.sample = function (size) {
return this.append({ $sample: { size: size } });
};

Expand All @@ -641,35 +642,11 @@
* @api public
*/

Aggregate.prototype.sort = function(arg) {
// TODO refactor to reuse the query builder logic

const sort = {};

if (getConstructorName(arg) === 'Object') {
const desc = ['desc', 'descending', -1];
Object.keys(arg).forEach(function(field) {
// If sorting by text score, skip coercing into 1/-1
if (arg[field] instanceof Object && arg[field].$meta) {
sort[field] = arg[field];
return;
}
sort[field] = desc.indexOf(arg[field]) === -1 ? 1 : -1;
});
} else if (arguments.length === 1 && typeof arg === 'string') {
arg.split(/\s+/).forEach(function(field) {
if (!field) {
return;
}
const ascend = field[0] === '-' ? -1 : 1;
if (ascend === -1) {
field = field.substring(1);
}
sort[field] = ascend;
});
} else {
Aggregate.prototype.sort = function (arg) {
if (arguments.length > 1) {
throw new TypeError('Invalid sort() argument. Must be a string or object.');
}
const sort = castSort(arg);

return this.append({ $sort: sort });
};
Expand All @@ -687,7 +664,7 @@
* @api public
*/

Aggregate.prototype.unionWith = function(options) {
Aggregate.prototype.unionWith = function (options) {
return this.append({ $unionWith: options });
};

Expand All @@ -706,7 +683,7 @@
* @see mongodb https://www.mongodb.com/docs/manual/applications/replication/#read-preference
*/

Aggregate.prototype.read = function(pref, tags) {
Aggregate.prototype.read = function (pref, tags) {
read.call(this, pref, tags);
return this;
};
Expand All @@ -724,7 +701,7 @@
* @api public
*/

Aggregate.prototype.readConcern = function(level) {
Aggregate.prototype.readConcern = function (level) {
readConcern.call(this, level);
return this;
};
Expand Down Expand Up @@ -756,7 +733,7 @@
* @api public
*/

Aggregate.prototype.redact = function(expression, thenExpr, elseExpr) {
Aggregate.prototype.redact = function (expression, thenExpr, elseExpr) {
if (arguments.length === 3) {
if ((typeof thenExpr === 'string' && !validRedactStringValues.has(thenExpr)) ||
(typeof elseExpr === 'string' && !validRedactStringValues.has(elseExpr))) {
Expand Down Expand Up @@ -836,7 +813,7 @@
* @see mongodb https://www.mongodb.com/docs/manual/reference/command/aggregate/
*/

Aggregate.prototype.allowDiskUse = function(value) {
Aggregate.prototype.allowDiskUse = function (value) {
this.options.allowDiskUse = value;
return this;
};
Expand All @@ -853,7 +830,7 @@
* @see mongodb https://www.mongodb.com/docs/manual/reference/command/aggregate/
*/

Aggregate.prototype.hint = function(value) {
Aggregate.prototype.hint = function (value) {
this.options.hint = value;
return this;
};
Expand All @@ -871,7 +848,7 @@
* @see mongodb https://www.mongodb.com/docs/manual/reference/command/aggregate/
*/

Aggregate.prototype.session = function(session) {
Aggregate.prototype.session = function (session) {
if (session == null) {
delete this.options.session;
} else {
Expand All @@ -898,7 +875,7 @@
* @api public
*/

Aggregate.prototype.option = function(value) {
Aggregate.prototype.option = function (value) {
for (const key in value) {
this.options[key] = value[key];
}
Expand All @@ -925,7 +902,7 @@
* @see mongodb https://mongodb.github.io/node-mongodb-native/4.9/classes/AggregationCursor.html
*/

Aggregate.prototype.cursor = function(options) {
Aggregate.prototype.cursor = function (options) {
this._optionsForExec();
this.options.cursor = options || {};
return new AggregationCursor(this); // return this;
Expand All @@ -944,7 +921,7 @@
* @see mongodb https://mongodb.github.io/node-mongodb-native/4.9/interfaces/CollationOptions.html
*/

Aggregate.prototype.collation = function(collation) {
Aggregate.prototype.collation = function (collation) {
this.options.collation = collation;
return this;
};
Expand All @@ -967,7 +944,7 @@
* @api public
*/

Aggregate.prototype.facet = function(options) {
Aggregate.prototype.facet = function (options) {
return this.append({ $facet: options });
};

Expand All @@ -993,7 +970,7 @@
* @api public
*/

Aggregate.prototype.search = function(options) {
Aggregate.prototype.search = function (options) {
return this.append({ $search: options });
};

Expand All @@ -1008,7 +985,7 @@
* @api public
*/

Aggregate.prototype.pipeline = function() {
Aggregate.prototype.pipeline = function () {
return this._pipeline;
};

Expand Down Expand Up @@ -1093,7 +1070,7 @@
* @param {Function} [reject] errorCallback
* @return {Promise}
*/
Aggregate.prototype.then = function(resolve, reject) {
Aggregate.prototype.then = function (resolve, reject) {
return this.exec().then(resolve, reject);
};

Expand All @@ -1108,7 +1085,7 @@
* @api public
*/

Aggregate.prototype.catch = function(reject) {
Aggregate.prototype.catch = function (reject) {
return this.exec().then(null, reject);
};

Expand All @@ -1123,7 +1100,7 @@
* @api public
*/

Aggregate.prototype.finally = function(onFinally) {
Aggregate.prototype.finally = function (onFinally) {
return this.exec().finally(onFinally);
};

Expand All @@ -1145,7 +1122,7 @@
* @api public
*/

Aggregate.prototype[Symbol.asyncIterator] = function() {
Aggregate.prototype[Symbol.asyncIterator] = function () {
return this.cursor({ useMongooseAggCursor: true }).transformNull()._transformForAsyncIterator();
};

Expand Down
Loading