Express packages
Please be sure of your agent type and version and pick the right documentation accordingly.
This is the documentation of the forest-express-sequelize and forest-express-mongoose Node.js agents that will soon reach end-of-support.
forest-express-sequelize v9 and forest-express-mongoose v9 are replaced by @forestadmin/agent v1.
Please check your agent type and version and read on or switch to the right documentation.
This is still the latest Ruby on Rails documentation of the forest_liana agent, you’re at the right place, please read on.
This is the documentation of the django-forestadmin Django agent that will soon reach end-of-support.
If you’re using a Django agent, notice that django-forestadmin v1 is replaced by forestadmin-agent-django v1.
If you’re using a Flask agent, go to the forestadmin-agent-flask v1 documentation.
Please check your agent type and version and read on or switch to the right documentation.
This is the documentation of the forestadmin/laravel-forestadmin Laravel agent that will soon reach end-of-support.
If you’re using a Laravel agent, notice that forestadmin/laravel-forestadmin v1 is replaced by forestadmin/laravel-forestadmin v3.
If you’re using a Symfony agent, go to the forestadmin/symfony-forestadmin v1 documentation.
Please check your agent type and version and read on or switch to the right documentation.
Express packages
The ForestAdmin Express stack is built around three packages:
Forest Express
Forest Express Mongoose
Forest Express Sequelize
Forest Express provides the Skeleton of the features, and Forest Express Mongoose or Forest Express Sequelize (depending on your database) are interfaces to your data. As a user, you never interact with Forest Express, you should always be using the interfaces in your code. To make Forest Admin work in an Express project, you basically need three things:
A running Express application with a connection to your database
Models, to work with your database using JavaScript
Initialization of the
forest-express-[mongoose/sequelize]package to start Forest Admin in your project.
Initialize ForestAdmin
Let's walkthrough the following snippet to understand how to initialize Forest Admin in an Express project:
const forest = require('forest-express-sequelize');
const Sequelize = require('sequelize');
const connection = require('./path/to/your/sequelize/connection');
...
forest.init({
envSecret: process.env.FOREST_ENV_SECRET,
authSecret: process.env.FOREST_AUTH_SECRET,
objectMapping: Sequelize,
connections: { default: connection },
}).then((FAMiddleware) => {
app.use(FAMiddleware);
});const forest = require('forest-express-mongoose');
const Mongoose = require('mongoose');
const connection = require('./path/to/your/mongoose/connection');
...
forest.init({
envSecret: process.env.FOREST_ENV_SECRET,
authSecret: process.env.FOREST_AUTH_SECRET,
objectMapping: Mongoose,
connections: { default: connection },
}).then((FAMiddleware) => {
app.use(FAMiddleware);
});As you can see at line 11, Forest Admin initialization basically returns a middleware, used to intercept every calls starting with /forest/* . This is the reserved path used by Forest Admin to communicate with your project. This simple piece of code unlocks everything that is needed to benefit from Forest Admin features.
Configure ForestAdmin
To initialize the middleware, here are the requirements (refer to the code snippet above):
line 7
envSecret: This is the place to set the secret key provided to you when you onboard. This identifies your environment and your project. In this snippet, ourenvSecretis stored as an environment variable.line 8
authSecret: This is another secret key used to perform authentication.line 9
objectMapping: This one is basically your ORM (Sequelize) or ODM (Mongoose).line 10
connections: This wraps up the connections that Forest Admin should use to manipulate your data. We use thedefaultkeyword here to specify that we will be using only one connection. If you don't know where to pick up this connection object from, here are examples of what it look like in your code:
Do note that the connection you want to provide to ForestAdmin should be initialized with the models you want to work with.
const Sequelize = require('Sequelize');
...
// The connection object is the one to pass to the init function
const connection = new Sequelize("postgres://...");const mongoose = require('mongoose');
...
mongoose.connect('mongodb://...');
// The connection object is the one to pass to the init function
const connection = mongoose.connection;
OR
// The connection object is the one to pass to the init function
const connection = mongoose.createConnection('mongodb://...');Last updated
Was this helpful?