Extend a route

Extending a route is a clean way to achieve more by building on top of Forest Admin's existing routes.

To extend a route, simply add your own logic before the next() statement:

SQL
Mongodb
/routes/companies.js
const express = require('express');
const { PermissionMiddlewareCreator } = require('forest-express-sequelize');
const router = express.Router();
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('companies');
...
// Create a Action Approval - Check out our documentation for more details: https://docs.forestadmin.com/documentation/v/v4/reference-guide/routes/default-routes#create-a-record
router.post('/companies', permissionMiddlewareCreator.create(), (req, res, next) => {
// >> Add your logic here <<
next();
});
...
module.exports = router;
/routes/companies.js
const express = require('express');
const { PermissionMiddlewareCreator } = require('forest-express-mongoose');
const router = express.Router();
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('companies');
...
// Create a Action Approval - Check out our documentation for more details: https://docs.forestadmin.com/documentation/v/v4/reference-guide/routes/default-routes#create-a-record
router.post('/companies', permissionMiddlewareCreator.create(), (req, res, next) => {
// >> Add your logic here <<
next();
});
...
module.exports = router;

Adding logic with an API call

The most simple way to trigger your business app's (or any external app's) logic is with an API call!

In the following example, we override the CREATE route so that a credit card is created whenever a new customer is created in Forest Admin:

/routes/customers.js
...
// Require superagent once you've installed it (npm install superagent)
const superagent = require('superagent');
...
router.post('/customers', permissionMiddlewareCreator.create(), (req, res, next) => {
// Prepare the API call using the Forest Admin's posted data
superagent
.post('https://my-company/create-card')
.set('accept', 'json')
.end((err, res) => {
// Call next() to execute Forest Admin's default behavior
next();
});
});
...
module.exports = router;

Adding logic with a message broker

Using a message broker - such as RabbitMQ or Kafka - to broadcast events is current practice.

Here is how you could be using RabbitMQ to handle orders synchronization across multiple channels:

/routes/orders.js
...
const amqp = require('amqplib/callback_api');
...
router.put('/orders/:orderId', permissionMiddlewareCreator.update(), (req, res, next) => {
// Prepare your message from Forest Admin's updated data
var orderId = req.body.data.id;
var orderStatus = req.body.data.attributes.shipping_status;
var message = 'Order ' + orderId + ' shipping status is now: ' + orderStatus;
var queue = 'orders_sync_queue';
// Connect to your Rabbitmq remote instance and publish your message
amqp.connect('amqp://{your_rabbitmq_host}', function(error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}
channel.assertQueue(queue, {
durable: false
});
channel.sendToQueue(queue, Buffer.from(message));
});
setTimeout(function() {
connection.close();
}, 500);
});
// Call next() to execute Forest Admin's default behavior
next();
});
...
module.exports = router;

Adding logic after Forest Admin's default behavior

At some point, you may want to trigger Forest Admin's logic after your remote logic.

To achieve this, you can manually recreate next()'s behavior by using the snippets of default routes, then append your own logic.

Changing or replacing Forest Admin's behavior

Changing Forest Admin's behavior

To achieve this, use existing snippets of default routes and modify them according to your needs.

Here are a few examples:

Use extended search by default

...
// Get a list of Companies - Check out our documentation for more details: https://docs.forestadmin.com/documentation/v/v4/reference-guide/routes/default-routes#get-a-list-of-records
router.get('/companies', permissionMiddlewareCreator.list(), (request, response, next) => {
const recordsGetter = new RecordsGetter(companies);
const params = request.query;
params.searchExtended = true;
recordsGetter.getAll(params)
.then(records => recordsGetter.serialize(companies))
.then(recordsSerialized => response.send(recordsSerialized))
.catch(next);
});
// Get a number of Companies - Check out our documentation for more details: https://docs.forestadmin.com/documentation/v/v4/reference-guide/routes/default-routes#get-a-number-of-records
router.get('/companies/count', permissionMiddlewareCreator.list(), (request, response, next) => {
const recordsCounter = new RecordsCounter(companies);
const params = request.query;
params.searchExtended = true;
recordsCounter.count(params)
.then(count => response.send({ count }))
.catch(next);
});
...

With this snippet, only the companies collection would use extended search by default.

Using extended search is less performant than default search. Use this wisely.

Protect a specific record

router.delete('/companies/:recordId', permissionMiddlewareCreator.delete(), (request, response, next) => {
if (request.params.recordId === 82) {
next(new Error('This record is protected, you cannot remove it.'));
return;
}
const recordRemover = new RecordRemover(companies);
recordRemover.remove(request.params.recordId)
.then(() => response.status(204).send())
.catch(next);
});

Replacing Forest Admin's behavior

To achieve this, simply remove the next() statement of any route:

...
// Create a Action Approval - Check out our documentation for more details: https://docs.forestadmin.com/documentation/v/v4/reference-guide/routes/default-routes#create-a-record
router.post('/companies', permissionMiddlewareCreator.create(), (req, res, next) => {
// >> Add your own logic here <<
});
...