Skip to content

Commit

Permalink
Merge pull request #2498 from kingcody/fix/mongoose-events
Browse files Browse the repository at this point in the history
fix(events): mongoose event registration
  • Loading branch information
Awk34 authored Feb 11, 2017
2 parents af9cdb6 + e3ed67f commit cb81d6f
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 cb81d6f

Please sign in to comment.