Skip to content

Commit 341b238

Browse files
authored
Merge pull request Automattic#15094 from IchirokuXVI/fix-aggregate-cursor-als
Fix aggregate cursor als
2 parents 2531768 + 4b8a25e commit 341b238

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

lib/aggregate.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ function Aggregate(pipeline, model) {
8787

8888
Aggregate.prototype.options;
8989

90+
/**
91+
* Returns default options for this aggregate.
92+
*
93+
* @param {Model} model
94+
* @api private
95+
*/
96+
97+
Aggregate.prototype._optionsForExec = function() {
98+
const options = this.options || {};
99+
100+
const asyncLocalStorage = this.model()?.db?.base.transactionAsyncLocalStorage?.getStore();
101+
if (!options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
102+
options.session = asyncLocalStorage.session;
103+
}
104+
105+
return options;
106+
};
107+
90108
/**
91109
* Get/set the model that this aggregation will execute on.
92110
*
@@ -914,6 +932,7 @@ Aggregate.prototype.option = function(value) {
914932
*/
915933

916934
Aggregate.prototype.cursor = function(options) {
935+
this._optionsForExec();
917936
this.options.cursor = options || {};
918937
return new AggregationCursor(this); // return this;
919938
};
@@ -1022,10 +1041,7 @@ Aggregate.prototype.exec = async function exec() {
10221041
applyGlobalMaxTimeMS(this.options, model.db.options, model.base.options);
10231042
applyGlobalDiskUse(this.options, model.db.options, model.base.options);
10241043

1025-
const asyncLocalStorage = this.model()?.db?.base.transactionAsyncLocalStorage?.getStore();
1026-
if (!this.options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
1027-
this.options.session = asyncLocalStorage.session;
1028-
}
1044+
this._optionsForExec();
10291045

10301046
if (this.options && this.options.cursor) {
10311047
return new AggregationCursor(this);
@@ -1052,6 +1068,7 @@ Aggregate.prototype.exec = async function exec() {
10521068
}
10531069

10541070
const options = clone(this.options || {});
1071+
10551072
let result;
10561073
try {
10571074
const cursor = await collection.aggregate(this._pipeline, options);

0 commit comments

Comments
 (0)