Loading route schemas
Before implementing the schemas, let’s add a dedicated folder to organize our code base better. We can do it inside the ./routes/todos/
path. Moreover, we want to load them automatically from the schemas
folder. To be able to do that, we need the following:
- A dedicated plugin inside the
schemas
folder - A definition of the schemas we wish to use
- An autohooks plugin that will load everything automatically when the
todos
module is registered on the Fastify instance
We will discuss these in detail in the following subsections.
Schemas loader
Starting with the first item of the list we just discussed, we want to create a ./routes/todos/schemas/loader.js
file. We can check the content of the file in the following code snippet:
'use strict' const fp = require('fastify-plugin') module.exports = fp(async function schemaLoaderPlugin (fastify, opts) { // [1] fastify.addSchema(require('./list-query...