Skip to content

Commit

Permalink
fix(events): mongoose event registration
Browse files Browse the repository at this point in the history
As of `[email protected]` schema events should be registered before the model is compiled

fixes #2479
  • Loading branch information
kingcody authored and Awk34 committed May 14, 2017
1 parent b5f2194 commit 79bdeed
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
17 changes: 10 additions & 7 deletions templates/app/server/api/user(auth)/user.events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

'use strict';

import {EventEmitter} from 'events';<% if (filters.mongooseModels) { %>
import User from './user.model';<% } if (filters.sequelizeModels) { %>
import {EventEmitter} from 'events';<% if (filters.sequelizeModels) { %>
import {User} from '../../sqldb';<% } %>
var UserEvents = new EventEmitter();

Expand All @@ -24,10 +23,12 @@ var events = {
};<% } %>

// Register the event emitter to the model events
for(var e in events) {
let event = events[e];<% if (filters.mongooseModels) { %>
User.schema.post(e, emitEvent(event));<% } if (filters.sequelizeModels) { %>
User.hook(e, emitEvent(event));<% } %>
function registerEvents(User) {
for(var e in events) {
let event = events[e];<% if (filters.mongooseModels) { %>
User.post(e, emitEvent(event));<% } if (filters.sequelizeModels) { %>
User.hook(e, emitEvent(event));<% } %>
}
}

function emitEvent(event) {
Expand All @@ -37,5 +38,7 @@ function emitEvent(event) {
done(null);<% } %>
}
}

<% if (filters.sequelizeModels) { %>
registerEvents(User);<% } if (filters.mongooseModels) { %>
export {registerEvents};<% } %>
export default UserEvents;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/*eslint no-invalid-this:0*/
import crypto from 'crypto';
mongoose.Promise = require('bluebird');
import mongoose, {Schema} from 'mongoose';<% if(filters.oauth) { %>
import mongoose, {Schema} from 'mongoose';
import {registerEvents} from './user.events';<% if(filters.oauth) { %>

const authTypes = ['github', 'twitter', 'facebook', 'google'];<% } %>

Expand Down Expand Up @@ -257,4 +258,5 @@ UserSchema.methods = {
}
};

registerEvents(UserSchema);
export default mongoose.model('User', UserSchema);
17 changes: 10 additions & 7 deletions templates/endpoint/basename.events(models).js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

'use strict';

import {EventEmitter} from 'events';<% if(filters.mongooseModels) { %>
import <%= classedName %> from './<%= basename %>.model';<% } if(filters.sequelizeModels) { %>
import {EventEmitter} from 'events';<% if(filters.sequelizeModels) { %>
var <%= classedName %> = require('<%= relativeRequire(config.get('registerModelsFile')) %>').<%= classedName %>;<% } %>
var <%= classedName %>Events = new EventEmitter();

Expand All @@ -27,10 +26,12 @@ var events = {
<%_ } -%>

// Register the event emitter to the model events
for(var e in events) {
let event = events[e];<% if(filters.mongooseModels) { %>
<%= classedName %>.schema.post(e, emitEvent(event));<% } if(filters.sequelizeModels) { %>
<%= classedName %>.hook(e, emitEvent(event));<% } %>
function registerEvents(<%= classedName %>) {
for(var e in events) {
let event = events[e];<% if(filters.mongooseModels) { %>
<%= classedName %>.post(e, emitEvent(event));<% } if(filters.sequelizeModels) { %>
<%= classedName %>.hook(e, emitEvent(event));<% } %>
}
}

function emitEvent(event) {
Expand All @@ -40,5 +41,7 @@ function emitEvent(event) {
done(null);<% } %>
};
}

<% if (filters.sequelizeModels) { %>
registerEvents(<%= classedName %>);<% } if (filters.mongooseModels) { %>
export {registerEvents};<% } %>
export default <%= classedName %>Events;
2 changes: 2 additions & 0 deletions templates/endpoint/basename.model(mongooseModels).js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

import mongoose from 'mongoose';
import {registerEvents} from './<%= basename %>.events';

var <%= classedName %>Schema = new mongoose.Schema({
name: String,
info: String,
active: Boolean
});

registerEvents(<%= classedName %>Schema);
export default mongoose.model('<%= classedName %>', <%= classedName %>Schema);

0 comments on commit 79bdeed

Please sign in to comment.