Skip to content

Commit

Permalink
Merge pull request #5766 from wlingke/master
Browse files Browse the repository at this point in the history
5706
  • Loading branch information
vkarpov15 authored Oct 29, 2017
2 parents 411db61 + b73af8d commit ee01370
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var EventEmitter = require('events').EventEmitter;
var MongooseDocumentArray = require('../types/documentarray');
var SchemaType = require('../schematype');
var Subdocument = require('../types/embedded');
var applyHooks = require('../services/model/applyHooks');
var discriminator = require('../services/model/discriminator');
var util = require('util');
var utils = require('../utils');
Expand Down Expand Up @@ -119,6 +120,8 @@ DocumentArray.prototype.discriminator = function(name, schema) {

this.casterConstructor.discriminators[name] = EmbeddedDocument;

applyHooks(EmbeddedDocument, schema);

return this.casterConstructor.discriminators[name];
};

Expand Down
4 changes: 4 additions & 0 deletions lib/schema/embedded.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var $exists = require('./operators/exists');
var EventEmitter = require('events').EventEmitter;
var SchemaType = require('../schematype');
var Subdocument = require('../types/subdocument');
var applyHooks = require('../services/model/applyHooks');
var castToNumber = require('./operators/helpers').castToNumber;
var discriminator = require('../services/model/discriminator');
var geospatial = require('./operators/geospatial');
Expand Down Expand Up @@ -253,5 +254,8 @@ Embedded.prototype.discriminator = function(name, schema) {
discriminator(this.caster, name, schema);

this.caster.discriminators[name] = _createConstructor(schema);

applyHooks(this.caster.discriminators[name], schema);

return this.caster.discriminators[name];
};
2 changes: 1 addition & 1 deletion lib/schema/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var SchemaType = require('../schematype'),
*/

function ObjectId(key, options) {
var isKeyHexStr = typeof key === 'string' && /^a-f0-9$/i.test(key);
var isKeyHexStr = typeof key === 'string' && key.length === 24 && /^a-f0-9$/i.test(key);
var suppressWarning = options && options.suppressWarning;
if ((isKeyHexStr || typeof key === 'undefined') && !suppressWarning) {
console.warn('mongoose: To create a new ObjectId please try ' +
Expand Down
121 changes: 121 additions & 0 deletions test/model.discriminator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,5 +842,126 @@ describe('model', function() {
}).
catch(done);
});
describe('embedded discriminators + hooks (gh-5706)', function(){
var counters = {
eventPreSave: 0,
eventPostSave: 0,
purchasePreSave: 0,
purchasePostSave: 0,
eventPreValidate: 0,
eventPostValidate: 0,
purchasePreValidate: 0,
purchasePostValidate: 0,
};
var eventSchema = new Schema(
{ message: String },
{ discriminatorKey: 'kind', _id: false }
);
eventSchema.pre('validate', (next) => {
counters.eventPreValidate++;
next();
});

eventSchema.post('validate', (doc) => {
counters.eventPostValidate++;
});

eventSchema.pre('save', (next) => {
counters.eventPreSave++;
next();
});

eventSchema.post('save', (doc) => {
counters.eventPostSave++;
});

var purchasedSchema = new Schema({
product: String,
}, { _id: false });

purchasedSchema.pre('validate', (next) => {
counters.purchasePreValidate++;
next();
});

purchasedSchema.post('validate', (doc) => {
counters.purchasePostValidate++;
});

purchasedSchema.pre('save', (next) => {
counters.purchasePreSave++;
next();
});

purchasedSchema.post('save', (doc) => {
counters.purchasePostSave++;
});

beforeEach(function() {
Object.keys(counters).forEach(function(i){
counters[i]=0;
})
});

it('should call the hooks on the embedded document defined by both the parent and discriminated schemas', function(done){
var trackSchema = new Schema({
event: eventSchema,
});

var embeddedEventSchema = trackSchema.path('event');
embeddedEventSchema.discriminator('Purchased', purchasedSchema)

var TrackModel = db.model('Track', trackSchema);
var doc = new TrackModel({
event: {
message: 'Test',
kind: 'Purchased'
}
});
doc.save(function(err){
assert.ok(!err);
assert.equal(doc.event.message, 'Test')
assert.equal(doc.event.kind, 'Purchased')
Object.keys(counters).forEach(function(i){
assert.equal(counters[i], 1);
});
done();
})
})

it('should call the hooks on the embedded document in an embedded array defined by both the parent and discriminated schemas', function(done){
var trackSchema = new Schema({
events: [eventSchema],
});

var embeddedEventSchema = trackSchema.path('events');
embeddedEventSchema.discriminator('Purchased', purchasedSchema)

var TrackModel = db.model('Track2', trackSchema);
var doc = new TrackModel({
events: [
{
message: 'Test',
kind: 'Purchased'
},
{
message: 'TestAgain',
kind: 'Purchased'
}
]
});
doc.save(function(err){
assert.ok(!err);
assert.equal(doc.events[0].kind, 'Purchased');
assert.equal(doc.events[0].message, 'Test');
assert.equal(doc.events[1].kind, 'Purchased');
assert.equal(doc.events[1].message, 'TestAgain');
Object.keys(counters).forEach(function(i){
assert.equal(counters[i], 2);
});
done();
})
})
})
});
});

0 comments on commit ee01370

Please sign in to comment.