API
Middlewares

Middlewares

Middlewares are executed right after your data has been retrieved from the database. They are functions that accepts the following arguments:

  • context as the first argument. It's an object will the following shape:
export type TMiddlewareContext<T> = {
  req: TInternalRequest
  result: T | T[] // the data retrieved from the database
}
  • a function as the second argument, which is used to execute the next middleware in the stack.

You can use middlewares to play with your data, for example if you want to wrap your data in a specific key you'd do the following:

middlewares: [
  (ctx, next) => {
    ctx.result = {
      myCustomKey: ctx.result,
    }
    next()
  },
]

Note: the following won't work, because you need to mutate directly the context object

middlewares: [
  ({ result }, next) => {
    result = {
      myCustomKey: result,
    }
    next()
  },
]