Skip to content

Commit

Permalink
fix(schema+timestamps): handle insertMany() with timestamps and dis…
Browse files Browse the repository at this point in the history
…criminators

Fix #12150
  • Loading branch information
vkarpov15 committed Aug 3, 2022
1 parent 07e36aa commit 25e4580
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/schema/SubdocumentPath.js
Expand Up @@ -47,6 +47,7 @@ function SubdocumentPath(schema, path, options) {
this.caster.prototype.$basePath = path;
this.schema = schema;
this.$isSingleNested = true;
this.base = schema.base;
SchemaType.call(this, path, options, 'Embedded');
}

Expand Down
1 change: 1 addition & 0 deletions lib/schema/documentarray.js
Expand Up @@ -147,6 +147,7 @@ function _createConstructor(schema, options, baseClass) {
EmbeddedDocument.prototype.constructor = EmbeddedDocument;
EmbeddedDocument.$isArraySubdocument = true;
EmbeddedDocument.events = new EventEmitter();
EmbeddedDocument.base = schema.base;

// apply methods
for (const i in schema.methods) {
Expand Down
35 changes: 35 additions & 0 deletions test/timestamps.test.js
Expand Up @@ -995,6 +995,41 @@ describe('timestamps', function() {
assert.ok(res.profile.conditions[0].createdAt);
assert.ok(res.profile.conditions[0].updatedAt);
});

it('works with insertMany() and embedded discriminators (gh-12150)', async function() {
const AssetSchema = new Schema({ url: String, size: String }, { timestamps: true });
const HeaderSectionSchema = new Schema({
title: String,
image: AssetSchema
});

// Abstract section
const BaseSectionSchema = new Schema({
isVisible: Boolean
}, { discriminatorKey: 'kind' });

// Main Schema
const PageSchema = new Schema({
sections: [BaseSectionSchema] // Same error without the array "sections: BaseSectionSchema"
}, { timestamps: true });

const sections = PageSchema.path('sections');
sections.discriminator('header', HeaderSectionSchema);

const Test = db.model('Test', PageSchema);

await Test.insertMany([{
sections: {
isVisible: true,
kind: 'header',
title: 'h1'
}
}]);

const doc = await Test.findOne();
assert.equal(doc.sections.length, 1);
assert.equal(doc.sections[0].title, 'h1');
});
});

async function delay(ms) {
Expand Down

0 comments on commit 25e4580

Please sign in to comment.