Skip to content

Instantly share code, notes, and snippets.

@aherve
Created March 12, 2016 11:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aherve/ad89a0ddb684277f97dd to your computer and use it in GitHub Desktop.
Save aherve/ad89a0ddb684277f97dd to your computer and use it in GitHub Desktop.
'use strict'
import async from 'async'
export default function postCreatePlugin (schema) {
schema.addPostCreate = function (f) {
schema.postCreateListeners = schema.postCreateListeners || []
schema.postCreateListeners.push(f)
}
schema.pre('save', function (next) {
this._wasNew = this.isNew
next()
})
schema.post('save', function (doc) {
if (doc._wasNew) {
async.parallel(
schema.postCreateListeners.map(f => f.bind(null, doc)),
(err) => {
if (err) { console.error(err) }
doc._wasNew = false
}
)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment