node.js - Is there a way in mongoose create global instance call for all Schemas? -
i know possible create instance method each schema, there way of creating instance call relates schemas.
you may want create function , function want use schema.
if have understood requirement need create function in separate file , add file schema plugin.
for example:
in lastupdate.js file create function function use different schema
module.exports = exports = function lastupdateplugin (schema) { schema.add({ updatetime: date }) schema.pre('save', function (next) { this.updatetime= new date next() }); }
in user.js schema file:
var lastupdate = require('./lastupdate');//load lastupdate.js file var user= new schema({name: string, ... }); user.plugin(lastupdate);
in bank.js schema file:
var lastupdate = require('./lastupdate');//load lastupdate.js file var bank= new schema({bankname: string, ... }); bank.plugin(lastupdate);
then when save user
, bank
automatically update updatetime
each schema
Comments
Post a Comment