How can I share mongoose models between 2 apps?

后端 未结 6 514
不思量自难忘°
不思量自难忘° 2021-02-04 06:26

I have 2 apps, each in a different folder and they need to share the same models.

I want to symlink the models folder from app A to a models folder in app B.

I\'

6条回答
  •  野性不改
    2021-02-04 06:59

    You have share your mongoose instance around by using doing something like this

    var mongoose = require('mongoose');
    module.exports.mongoose = mongoose;
    
    var user = require('./lib/user');
    

    Now inside of "lib/user.js"

    var mongoose = module.parent.mongoose;
    var model = mongoose.model('User', new mongoose.Schema({ ... });
    module.exports = model;
    

    So doing it like that you can require "lib/user.js" in other applications

提交回复
热议问题