Plug multiple NoSQL databases
Below you'll find a couple steps to plug multiple mongoose databases into a single Forest Admin project.
Forest Admin supports MongoDB databases. The ORM used is Mongoose.

1. Install Forest Admin with 1 database

Even if you want to use multiple databases, install with 1 of them. You'll plug your other databases later.

2. Move your models

Move the content of the models folder into a new sub-folder that matches your db name (db1):
1
models/
2
├── db1/
3
│ ├── index.js
4
│ ├── model1.js
5
│ └── model2.js
Copied!

3. Create a new connection.js file

In your new db1 folder, add a new connection.js file with the following content:
1
const mongoose = require('mongoose');
2
const connection = mongoose.createConnection(process.env.DATABASE_URL, { useNewUrlParser: true });
3
4
module.exports = connection;
Copied!
This will generate one of the several connections we'll need.

4. Update your models

Update your existing models by adding the created connection:
1
const mongoose = require('mongoose');
2
const connection = require('./connection');
3
4
const schema = mongoose.Schema({
5
// Your existing model
6
});
7
8
module.exports = connection.model('model1', schema, 'model1');
Copied!
You should remove the index.js file, which is now unnecessary.

5. Repeat steps 2 to 4

For each new database you need to plug into your project, repeat above steps 2 to 4.
In connection.js, be sure to use the DATABASE_URL of the database to plug, instead of process.env.DATABASE_URL(which is the DATABASE_URL of the database you used to install)
After adding a second database, your folder architecture should look like this:
1
models/
2
├── db1/
3
│ ├── connection.js
4
│ ├── model1.js
5
│ └── model2.js
6
├── db2/
7
│ ├── connection.js
8
│ ├── model3.js
9
│ └── model4.js
Copied!

6. Final step: declare your connections

Finally, update your middlewares/forestadmin/index.js to include the 2 connections:
1
...
2
const db1 = require('../../models/db1/connection');
3
const db2 = require('../../models/db2/connection');
4
5
module.exports = function (app) {
6
require('lumber-forestadmin').run(app, {
7
modelsDir: __dirname + '/../../models',
8
envSecret: process.env.FOREST_ENV_SECRET,
9
authSecret: process.env.FOREST_AUTH_SECRET,
10
mongoose: require('mongoose'),
11
connections: [
12
db1,
13
db2,
14
],
15
});
16
17
...
18
};
Copied!
Last modified 1yr ago