Skip to content

Commit

Permalink
fix: reuse existing hydratedPopulatedDocs option re: #14503
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Apr 23, 2024
1 parent a4e782d commit bd8fa0d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lib/model.js
Expand Up @@ -3959,8 +3959,7 @@ Model.hydrate = function(obj, projection, options) {
obj = applyProjection(obj, projection);
}
const document = require('./queryHelpers').createModel(this, obj, projection);
options = options || {};
document.$init(obj, { ...options, hydrate: true });
document.$init(obj, options);
return document;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/schema.js
Expand Up @@ -2317,7 +2317,7 @@ Schema.prototype.virtual = function(name, options) {
_v == null ? [] : [_v];
}

if (opts?.hydrate && !options.count) {
if (opts?.hydratedPopulatedDocs && !options.count) {
const modelNames = virtual._getModelNamesForPopulate(this);
const populatedVal = this.$$populatedVirtuals[name];
if (!Array.isArray(populatedVal) && !populatedVal.$__ && modelNames?.length === 1) {
Expand Down
4 changes: 3 additions & 1 deletion test/model.hydrate.test.js
Expand Up @@ -153,7 +153,9 @@ describe('model', function() {

const populated = await User.findOne({ name: 'Alex' }).populate(['stories', 'storiesCount']).lean();
const hydrated = User.hydrate(
JSON.parse(JSON.stringify(populated))
JSON.parse(JSON.stringify(populated)),
null,
{ hydratedPopulatedDocs: true }
);

assert.equal(hydrated.stories[0]._id.toString(), story1._id.toString());
Expand Down

0 comments on commit bd8fa0d

Please sign in to comment.