Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * MongooseJS plugin to auto-populate the _createdBy and _updatedBy document values (If they exist in the schema)
- */
- 'use strict'
- import _ from 'moar-lodash'
- import * as appRoot from 'app-root-path'
- import Mongoose from 'mongoose'
- const log = appRoot.require('./dist/lib/utils/logger')({ plugin: 'Mongoose ByAccount'})
- const accountHelper = appRoot.require('./dist/lib/helpers/account')
- module.exports = ( schema, options ) => {
- // If the schema has a _createdBy..
- if( _.isObject( schema.paths._createdBy ) ) {
- log.debug( `Schema item _createdBy found` )
- schema.pre( 'save', function( next ) {
- if( ! accountHelper.getAccount( '_id' ) ) {
- log.warn( `Unable to populate _createdBy - No account ID set in the Account Helper` )
- }
- else {
- this._createdBy = accountHelper.getAccount( '_id' )
- this.markModified( '_createdBy' )
- }
- next()
- } )
- }
- // If the schema has a _updatedBy...
- if( _.isObject( schema.paths._updatedBy ) ) {
- log.debug( `Schema item _updatedBy found` )
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement