Middleware

Behind the scenes, getters and setters are registered as HTTP GET and HTTP POST routes, respectively, via Express. As a result, if necessary, custom middleware can be defined on an individual getter or setter.

/api/books/getters.js

import rate_limiter_middleware from 'example-rate-limiter';

const getters = {
  books: {
    middleware: [
      rate_limiter_middleware,
    ],
    authorized: (input = {}, context = {}) => {
      return !!context?.user;
    },
    get: (input = {}, context = {}) => {
      return process.databases.mongodb.collection('books').find({
        user_id: context?.user?._id,
        category: input?.category,
      }).toArray();
    }
  }
};

export default getters;

You can provide as many middleware functions as you'd like on a getter or setter, but it's important to be mindful of your middleware's effect on response performance. It's recommended to only use middleware when absolutely necessary to avoid surprise bottlenecks for your users.