Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Island rhythms/deeply hydrate doc #14352

Merged
merged 8 commits into from Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions lib/document.js
Expand Up @@ -694,7 +694,6 @@ Document.prototype.$__init = function(doc, opts) {
init(this, doc, this._doc, opts);

markArraySubdocsPopulated(this, opts.populated);

this.$emit('init', this);
this.constructor.emit('init', this);

Expand All @@ -703,7 +702,6 @@ Document.prototype.$__init = function(doc, opts) {
null;

applyDefaults(this, this.$__.selected, this.$__.exclude, hasIncludedChildren, false, this.$__.skipDefaults);

return this;
};

Expand Down Expand Up @@ -746,7 +744,6 @@ function init(self, obj, doc, opts, prefix) {
}
path = prefix + i;
schemaType = docSchema.path(path);

// Should still work if not a model-level discriminator, but should not be
// necessary. This is *only* to catch the case where we queried using the
// base model and the discriminated model has a projection
Expand All @@ -770,15 +767,14 @@ function init(self, obj, doc, opts, prefix) {
}
} else {
// Retain order when overwriting defaults
if (doc.hasOwnProperty(i) && obj[i] !== void 0) {
if (doc.hasOwnProperty(i) && obj[i] !== void 0 && !opts.hydratedPopulatedDocs) {
delete doc[i];
}
if (obj[i] === null) {
doc[i] = schemaType._castNullish(null);
} else if (obj[i] !== undefined) {
const wasPopulated = obj[i].$__ == null ? null : obj[i].$__.wasPopulated;

if (schemaType && !wasPopulated) {
if ((schemaType && !wasPopulated) && !opts.hydratedPopulatedDocs) {
try {
if (opts && opts.setters) {
// Call applySetters with `init = false` because otherwise setters are a noop
Expand Down
2 changes: 1 addition & 1 deletion lib/model.js
Expand Up @@ -3830,6 +3830,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
* @param {Object|String|String[]} [projection] optional projection containing which fields should be selected for this document
* @param {Object} [options] optional options
* @param {Boolean} [options.setters=false] if true, apply schema setters when hydrating
* @param {Boolean} [options.hydratedPopulatedDocs=false] if true, populates the docs if passing pre-populated data
* @return {Document} document instance
* @api public
*/
Expand All @@ -3843,7 +3844,6 @@ Model.hydrate = function(obj, projection, options) {
}
obj = applyProjection(obj, projection);
}

const document = require('./queryHelpers').createModel(this, obj, projection);
document.$init(obj, options);
return document;
Expand Down
18 changes: 18 additions & 0 deletions test/model.hydrate.test.js
Expand Up @@ -99,5 +99,23 @@ describe('model', function() {
assert.equal(hydrated.test, 'test');
assert.deepEqual(hydrated.schema.tree, C.schema.tree);
});
it('should deeply hydrate the document with the `hydratedPopulatedDocs` option (gh-4727)', async function() {
const userSchema = new Schema({
name: String
});
const companySchema = new Schema({
name: String,
users: [{ ref: 'User', type: Schema.Types.ObjectId }]
});

db.model('UserTestHydrate', userSchema);
const Company = db.model('CompanyTestHyrdrate', companySchema);

const users = [{ _id: new mongoose.Types.ObjectId(), name: 'Val' }];
const company = { _id: new mongoose.Types.ObjectId(), name: 'Booster', users: [users[0]] };

const C = Company.hydrate(company, null, { hydratedPopulatedDocs: true });
assert.equal(C.users[0].name, 'Val');
});
});
});