Skip to content

beliven-it/fastify-crud-generator

Repository files navigation

fastify-crud-generator

A plugin to rapidly generate CRUD routes for any entity

Node.js CI

Install

npm i --save fastify-crud-generator

Usage

fastify
  .register(require('fastify-crud-generator'), {
    prefix: '/products',
    controller: ...
  })
  .after(() => console.log(fastify.printRoutes()))

It can be registered as many times as you need, with different prefix:

const crud = require('fastify-crud-generator')

fastify
  .register(crud, {
    prefix: '/products',
    controller: ...
  })
  .register(crud, {
    prefix: '/orders',
    controller: ...
  })
  .after(() => console.log(fastify.printRoutes()))

By default, the following routes are supported:

GET    (prefix)/
POST   (prefix)/
GET    (prefix)/:id
PATCH  (prefix)/:id
DELETE (prefix)/:id

Options

When registering the plugin in your app, you can pass the following options to fine-tuning the CRUD routes generation:

Name Description
prefix Add a prefix to all generated routes.
controller (MANDATORY) A controller object providing handlers for each route.
list Route options for list action.
create Route options for create action.
view Route options for view action.
update Route options for update action.
delete Route options for delete action.

Prefix

This option can be used to prefix all routes with a common path, usually the plural name of the entity according to REST best practices:

{
  prefix: '/products'
}

The prefix option can also be used to define API version for the generated routes:

{
  prefix: '/v1/products'
}

NOTE: if no prefix is specified, all routes will be added at the root level.

Controller

This is the only mandatory option during the plugin registration.

A controller object provides the route handlers used to implement the classic CRUD actions for the registered entity (list, create, view, update, delete).

Passing an external controller object allows the maximum flexibility in terms of business logic and underlying data layer for any entity (e.g. SQL, NoSQL, file storage, etc.)

{
  prefix: '/products',
  controller: productController
}

A controller object should implement the following interface:

{
  list: async (req, reply) => { ... },
  create: async (req, reply) => { ... },
  view: async (req, reply) => { ... },
  update: async (req, reply) => { ... },
  delete: async (req, reply) => { ... }
}

Please note you're not forced to implement all supported methods: you can choose to implement only those needed by your resource.

Not implementing a method of the interface will automatically disable the corresponding route.

All methods accept a req / reply argument pair, which are the original request and reply objects passed to the route.

Route options

The list, create, view, update and delete options allow to fine tune the generated routes according to the available configuration provided by Fastify.

Take a look at the official documentation for more details.

Test

npm test

Acknowledgements

This project is inspired by:

And kindly sponsored by:

Beliven

License

Licensed under MIT