Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/beliven-it/fastify-crud-generator
A plugin to rapidly generate CRUD routes for any entity
https://github.com/beliven-it/fastify-crud-generator
crud fastify generator plugin rest routes
Last synced: about 2 months ago
JSON representation
A plugin to rapidly generate CRUD routes for any entity
- Host: GitHub
- URL: https://github.com/beliven-it/fastify-crud-generator
- Owner: beliven-it
- License: mit
- Created: 2020-08-14T15:43:17.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-03-27T04:28:41.000Z (10 months ago)
- Last Synced: 2024-04-24T19:48:26.923Z (9 months ago)
- Topics: crud, fastify, generator, plugin, rest, routes
- Language: JavaScript
- Homepage:
- Size: 1.59 MB
- Stars: 21
- Watchers: 4
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-crud-generator
A plugin to rapidly generate CRUD routes for any entity
[![Node.js CI](https://github.com/beliven-it/fastify-crud-generator/actions/workflows/node.js.yml/badge.svg)](https://github.com/beliven-it/fastify-crud-generator/actions/workflows/node.js.yml)
## Install
```bash
npm i --save fastify-crud-generator
```## Usage
```js
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:
```js
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:```js
{
prefix: '/products'
}
```The `prefix` option can also be used to define API version for the generated routes:
```js
{
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.*)```js
{
prefix: '/products',
controller: productController
}
```A `controller` object should implement the following interface:
```js
{
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](https://www.fastify.io/docs/latest/Request/) and
[reply](https://www.fastify.io/docs/latest/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](https://www.fastify.io/docs/latest/Routes/#options)
for more details.## Test
```bash
npm test
```## Acknowledgements
This project is inspired by:
* [fastify-autocrud](https://www.npmjs.com/package/fastify-autocrud)
And kindly sponsored by:
[![Beliven](https://assets.beliven.com/brand/logo_pos_color.svg)](https://www.beliven.com)
## License
Licensed under [MIT](./LICENSE)