ISSUE: Plug multiple NoSQL databases

Hey :grinning: I’ve integrate forest admin to my proyect with MongoDB which creates a new backend application connected to my NoSQL database.

All that works perfectly. The problem is when I want to connect a second NoSQL database. I followed the steps here: https://docs.forestadmin.com/documentation/how-tos/databases/plug-multiple-nosql-databases but there is no way it works. It seems to me that the document is not updated because it does not say anything about what to do with the file config/database.js (attached below) and in step 6 it says to declare the connections in middlewares/forestadmin/index.js folder and file that does not exist.

I would appreciate if you could help me to connect the second database to forest as any more specifc guide.

Currently I only have one connected as it is configured by following the steps to connect your project to forest with MongoDB by running the commands:

  • forest projects:create “hjb” --databaseConnectionURL “mongodb://localhost:27017/db1” --applicationHost “localhost” --applicationPort “3310”
  • cd “hjb”
  • npm install -s

file config/database.js:

folders created by forest config:

Thanks!

Set up information:
“express”: “~4.17.1”,
“forest-express-mongoose”: “^8.0.0”,
“mongoose”: “~5.8.2”,

Hello @usr1vang,

It seems we miss a doc update.
Here is what you have to do to add second mongo database to your project:

  • (We assume you have a project working with the first database)
  • remove the models files under /models except index.js (they will be regenerated later)
  • Inside .env file add another connection string definition (for example DATABASE_URL_2=...) pointing to you second database
  • Inside config/databases.js
    • update modelsDir value: ‘…/models/db1’ (append /db1)
    • you can update the name if you wish
    • add a new entry to the exported array (you can clone the first one)
      • replace db1 by db2
      • change the name
  • run forest schema:update from the project root

Thank for pointing the issue, we will fix it :pray:

1 Like

Hi @Sliman_Medini thanks you for your quickly response. I have followed your steps and everything seems to be working until I go to https://app.forestadmin.com/projects.

The problem now is that in Forest only shows the collections of the second database (db2) and it is not that they are not visible, they do not even appear.

What could be wrong here? maybe I am missing something inside middlewares/forestadmin.js related to the database connections? below i attached how my project folders looks like and the middlewares/forestadmin.js file.

I look forward to your comments.
Thanks!

Ok,

You can double-check the collections from database A are missing by looking at the collections list defined in forestadmin-schema.json.
Assuming this is the case, you may have one of this problem:

  • the names of the databases are the same in config/database.js ?
  • the content of the models/index.js has changed ? (the old documentation asks to alter this file)

Do not hesitate to share to config/database.js if you need.

The content of index.js should 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;

Regards

Hi,

Indeed i had a problem with the name field in the config/databases.js file. I modified that and it is working, thank you very much for your help.

Regards.

1 Like