Mongoose model diagram in a separate module

Disclaimer: I am new to mongoose / node, so please excuse me if I misunderstand some basic things. Yes, I found several posts about in this section , but could not adapt it to my needs.

I structured my main project into many separate projects. One separation is the "app-core" project, which will contain the main models and "modules" that must be implemented in each other project (the core application is configured as a dependency in the package.json file of each project).

A (simplified) model in the application core now looks like this:

var mongoose = require('mongoose'); var Schema = mongoose.Schema; var IndustrySchema = new Schema({ name: { type: String, required: true } }); module.exports = mongoose.model('Industry', IndustrySchema); 

The wep-app includes this model:

 var Industry = require('app-core/models/Industry'); 

and creates a MongoDB connection as follows:

 var DB_URL = 'mongodb://localhost/hellowins'; var mongoose = require('mongoose'); var mongooseClient = mongoose.connect(DB_URL); mongooseClient.connection.on('connected',function () { console.log("MongoDB is connected"); }); 

Now I have a problem that the model will not use the mongo connection defined in the app-web project, rather, it will consider the connection configured in the application core.

Due to the encapsulation design and responsibility, I definitely don't want the kernel to define connections for every possible application (which may include the main application). So I need to specify the circuit only in the kernel.

I already read that I should not require the model itself (/ app-core / models / Industry) and use the mongoose model instead

 var Industry = mongoose.model("Industry"); 

But then I get an error

 MissingSchemaError: Schema hasn't been registered for model "Test" 

To fix this, I have to register the models manually, for example, in the first link (at the top of my publication). But for some reason, I do not like this approach, because I will need to expand it every time the application uses a new model.

And I also need a mongo connection even in the main application - at least to run mocha tests.

So, I'm a little confused about how to structure the architecture in this case.

UPDATE # 1

Now I have found one working solution. But, unfortunately, this does not fully meet my requirements, because it is rather difficult (for example, ugly) to extend the model with hooks (for example, TestSchema.pre ("save" ..)).

Model (core application)

 exports.model = { name: { type: String, required: true } }; 

models.js (network application that runs once at startup)

 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var models = ['Test']; // add many exports.initialize = function() { var l = models.length; for (var i = 0; i < l; i++) { var model = require('hw-core/models/' + models[i]); var ModelSchema = new Schema(model.model); module.exports = mongoose.model(models[i], ModelSchema); } 

};

app.js (web application)

 require('./conf/app_models.js').initialize(); 

Then I can get the model as follows

 var mongoose = require('mongoose'); var TestModel = mongoose.model("Test"); var Test = new TestModel(); 
+4
source share
1 answer

Why don't you try exporting the mongoose instance from your application module and use it later in the web application to connect to the database

app-core index.js

 var mongoose = require('mongoose'); module.exports = { mongooseInstance: mongoose }; 

web-app index.js

 var core = require('app-core'), mongoose = core.mongooseInstance, mongooseClient = mongoose.connect(DB_URL); // and so on 

This can work as long as you need your models in the controllers that are initialized after the code from index.js. I hope my answer will be helpful.

0
source

Source: https://habr.com/ru/post/1488259/


All Articles