-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(model): abstract model events to a standard EventEmitter
- Loading branch information
Showing
4 changed files
with
108 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* User model events | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var EventEmitter = require('events').EventEmitter;<% if (filters.mongooseModels) { %> | ||
var User = require('./user.model');<% } if (filters.sequelizeModels) { %> | ||
var User = require('../../sqldb').User;<% } %> | ||
var UserEvents = new EventEmitter(); | ||
|
||
// Set max event listeners (0 == unlimited) | ||
UserEvents.setMaxListeners(0); | ||
|
||
// Model events<% if (filters.mongooseModels) { %> | ||
var events = { | ||
'save': 'save', | ||
'remove': 'remove' | ||
};<% } if (filters.sequelizeModels) { %> | ||
var events = { | ||
'afterCreate': 'save', | ||
'afterUpdate': 'save', | ||
'afterDestroy': 'remove' | ||
};<% } %> | ||
|
||
// Register the event emitter to the model events | ||
for (var e in events) { | ||
var event = events[e];<% if (filters.mongooseModels) { %> | ||
User.schema.post(e, emitEvent(event));<% } if (filters.sequelizeModels) { %> | ||
User.hook(e, emitEvent(event));<% } %> | ||
} | ||
|
||
function emitEvent(event) { | ||
return function(doc<% if (filters.sequelizeModels) { %>, options, done<% } %>) { | ||
UserEvents.emit(event + ':' + doc._id, doc); | ||
UserEvents.emit(event, doc);<% if (filters.sequelizeModels) { %> | ||
done(null);<% } %> | ||
} | ||
} | ||
|
||
module.exports = UserEvents; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* <%= classedName %> model events | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var EventEmitter = require('events').EventEmitter;<% if (filters.mongooseModels) { %> | ||
var <%= classedName %> = require('./<%= name %>.model');<% } if (filters.sequelizeModels) { %> | ||
var <%= classedName %> = require('../../sqldb').<%= classedName %>;<% } %> | ||
var <%= classedName %>Events = new EventEmitter(); | ||
|
||
// Set max event listeners (0 == unlimited) | ||
<%= classedName %>Events.setMaxListeners(0); | ||
|
||
// Model events<% if (filters.mongooseModels) { %> | ||
var events = { | ||
'save': 'save', | ||
'remove': 'remove' | ||
};<% } if (filters.sequelizeModels) { %> | ||
var events = { | ||
'afterCreate': 'save', | ||
'afterUpdate': 'save', | ||
'afterDestroy': 'remove' | ||
};<% } %> | ||
|
||
// Register the event emitter to the model events | ||
for (var e in events) { | ||
var event = events[e];<% if (filters.mongooseModels) { %> | ||
<%= classedName %>.schema.post(e, emitEvent(event));<% } if (filters.sequelizeModels) { %> | ||
<%= classedName %>.hook(e, emitEvent(event));<% } %> | ||
} | ||
|
||
function emitEvent(event) { | ||
return function(doc<% if (filters.sequelizeModels) { %>, options, done<% } %>) { | ||
<%= classedName %>Events.emit(event + ':' + doc._id, doc); | ||
<%= classedName %>Events.emit(event, doc);<% if (filters.sequelizeModels) { %> | ||
done(null);<% } %> | ||
} | ||
} | ||
|
||
module.exports = <%= classedName %>Events; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters