npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-crud-generator

v1.1.0

Published

A plugin to rapidly generate CRUD routes for any entity

Downloads

17

Readme

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