Skip to content

fix(events): mongoose event registration #2498

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

Merged
merged 1 commit into from
Feb 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);