Skip to main content

Middlewares

Middleware functions are intermediary functions that sit between the client's request and the server's response. They have access to the request object (req) and the response object (res).

As Manifest works with ExpressJS, Manifest middlewares are ExpressJS middlewares enhanced with Manifest SDK attached to let you interact with your data with ease.

Middleware use cases

Here are some examples of middleware use cases:

  • Patch the request body before storing it in the DB
  • Call an external API or any other custom logic
  • Hide some data from the response
  • Trigger an internal or external action on an event (see also webhooks)

Syntax

manifest/backend.yml
entities:
Project 🗂️:
properties:
- name
- { name: date, type: date }
middlewares:
beforeCreate:
- handler: setDate
afterCreate:
- handler: sendEmail

This example triggers the handler located at /manifest/handlers/setDate.js before the item is created and stored to the database, and triggers /manifest/handlers/sendEmail.js after.

manifest/handlers/setDate.js
module.exports = async (req, res) => {
console.log('Hello from the handler!')

req.body['date'] = new Date()
}
tip

You can add several middlewares for an event. They will be processed sequentially in your order.

Use your data with the Manifest backend SDK

Manifest passes the JS SDK to handler functions as third argument. You can use it to fetch or write data.

manifest/handlers/patchDocumentNameIfEmpty.js
module.exports = async (req, res, manifest) => {
// If the 'name' property of the item is empty.
if (!req.body['name']) {
// Get the user from the request body.
const user = await manifest.from('users').findOneById(req.body['userId'])

// Set a custom name based on the user.
req.body['name'] = `${user.name}'s untitled document`
}
}

Events

This is the list and description of the 6 events available where you can attach middlewares. All of them are related to an entity

NameDescription
beforeCreateBefore creating a record
afterCreateAfter creating a record
beforeUpdateBefore updating a record
afterUpdateAfter updating a record
beforeDeleteBefore deleting a record
afterDeleteAfter deleting a record