Skip to content

Commit

Permalink
Merge pull request #14240 from Automattic/vkarpov15/gh-14236
Browse files Browse the repository at this point in the history
avoid double-calling query transform() with findOne()
  • Loading branch information
vkarpov15 authored Jan 7, 2024
2 parents 3d4274e + 3b2a138 commit 889a41d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
38 changes: 8 additions & 30 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2498,12 +2498,12 @@ Query.prototype._findOne = async function _findOne() {
// don't pass in the conditions because we already merged them in
const doc = await this.mongooseCollection.findOne(this._conditions, options);
return new Promise((resolve, reject) => {
this._completeOne(doc, null, _wrapThunkCallback(this, (err, res) => {
this._completeOne(doc, null, (err, res) => {
if (err) {
return reject(err);
}
resolve(res);
}));
});
});
};

Expand Down Expand Up @@ -3303,12 +3303,12 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
const doc = !options.includeResultMetadata ? res : res.value;

return new Promise((resolve, reject) => {
this._completeOne(doc, res, _wrapThunkCallback(this, (err, res) => {
this._completeOne(doc, res, (err, res) => {
if (err) {
return reject(err);
}
resolve(res);
}));
});
});
};

Expand Down Expand Up @@ -3399,12 +3399,12 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
const doc = !includeResultMetadata ? res : res.value;

return new Promise((resolve, reject) => {
this._completeOne(doc, res, _wrapThunkCallback(this, (err, res) => {
this._completeOne(doc, res, (err, res) => {
if (err) {
return reject(err);
}
resolve(res);
}));
});
});
};

Expand Down Expand Up @@ -3553,12 +3553,12 @@ Query.prototype._findOneAndReplace = async function _findOneAndReplace() {

const doc = !includeResultMetadata ? res : res.value;
return new Promise((resolve, reject) => {
this._completeOne(doc, res, _wrapThunkCallback(this, (err, res) => {
this._completeOne(doc, res, (err, res) => {
if (err) {
return reject(err);
}
resolve(res);
}));
});
});
};

Expand Down Expand Up @@ -4382,28 +4382,6 @@ function _executePreHooks(query, op) {
});
}

/*!
* ignore
*/

function _wrapThunkCallback(query, cb) {
return function(error, res) {
if (error != null) {
return cb(error);
}

for (const fn of query._transforms) {
try {
res = fn(res);
} catch (error) {
return cb(error);
}
}

return cb(null, res);
};
}

/**
* Executes the query returning a `Promise` which will be
* resolved with either the doc(s) or rejected with the error.
Expand Down
6 changes: 4 additions & 2 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2328,18 +2328,20 @@ describe('Query', function() {
});
});

it('map (gh-7142)', async function() {
it('transform (gh-14236) (gh-7142)', async function() {
const Model = db.model('Test', new Schema({ name: String }));


let called = 0;
await Model.create({ name: 'test' });
const now = new Date();
const res = await Model.findOne().transform(res => {
res.loadedAt = now;
++called;
return res;
});

assert.equal(res.loadedAt, now);
assert.strictEqual(called, 1);
});

describe('orFail (gh-6841)', function() {
Expand Down

0 comments on commit 889a41d

Please sign in to comment.