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.

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:

app.js
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);
});

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, our envSecret is 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 the default keyword 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://...");

Last updated