Feature(s) impacted
After I upgraded my project to V7 (following the docs here), I got everything up and running in all my environments with no problems. However, today I had to make some changes in order to include another database in my project. Hence, I followed the instructions here, but I got lost when dealing with some files that were created/modified during the upgrade to V7.
Observed behavior
For now, my /middlewares/forestadmin.js
file looks like this:
const chalk = require('chalk');
const Liana = require('forest-express-mongoose');
// 1. this was added when following instructions to upgrade to V7, but it seems
// that I don't need it anymore (at least regarding 'connections')
const { objectMapping, connections } = require('../models');
const db1 = require('../models/db1/connection');
const db2 = require('../models/db2/connection');
module.exports = async function forestadmin(app) {
app.use(await Liana.init({
envSecret: process.env.FOREST_ENV_SECRET,
authSecret: process.env.FOREST_AUTH_SECRET,
objectMapping, // 2. should I ignore this?
connections: [ // 3. this was changed to include connections to both databases
db1,
db2
]
}));
console.log(chalk.cyan('Your admin panel is available...'));
};
As you can see, the objectMapping
used to be imported from /models/index.js
. But since Iβm using two databases now, I no longer have the index.js
file because my folder architecture became like this:
models/
βββ db1/
β βββ connection.js
β βββ model1.js
β βββ model2.js
βββ db2/
β βββ connection.js
β βββ model3.js
β βββ model4.js
Just for the record, this is how my models/index.js
used to be:
const fs = require('fs');
const path = require('path');
const Mongoose = require('mongoose');
const databasesConfiguration = require('../config/databases');
const connections = {};
const db = {};
databasesConfiguration.forEach((databaseInfo) => {
const connection = Mongoose.createConnection(databaseInfo.connection.url, databaseInfo.connection.options);
connections[databaseInfo.name] = connection;
const modelsDir = databaseInfo.modelsDir || path.join(__dirname, databaseInfo.name);
fs
.readdirSync(modelsDir)
.filter((file) => file.indexOf('.') !== 0 && file !== 'index.js')
.forEach((file) => {
try {
const model = require(path.join(modelsDir, file))(connection, Mongoose);
db[model.modelName] = model;
} catch (error) {
console.error(`Model creation error: ${error}`);
}
});
});
db.objectMapping = Mongoose;
db.connections = connections;
module.exports = db;
Expected behavior
My question is whether I can ignore the objectMapping
. If itβs not the case, how should I change /models/db1/connection.js
and /models/db2/connection.js
to keep my configuration in /middlewares/forestadmin.js
as it is?
// => models/db1/connection.js
const mongoose = require('mongoose');
const connection = mongoose.createConnection(process.env.DB1_DATABASE_URL, { useNewUrlParser: true });
module.exports = connection;
Context
- Package Version: 7.6.0 (forest-express-mongoose)
- Express Version: 4.17.1
- Mongoose Version: 5.8.2
- Project name: parceiros
- Environment name: all
Thanks in advance and hope you all have a nice end of the year!