# fastify-crud-generator

> A plugin to rapidly generate CRUD routes for any entity

Latest version **1.1.0** (published 2024-03-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install fastify-crud-generator
pnpm add fastify-crud-generator
yarn add fastify-crud-generator
bun add fastify-crud-generator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2024-03-23 |
| First published | 2020-08-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 39.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Beliven |
| Maintainers | beliven.dev |
| Keywords | fastify, plugin, crud, generator, routes |

## Links

- npm: https://www.npmjs.com/package/fastify-crud-generator
- Repository: https://github.com/beliven-it/fastify-crud-generator
- Homepage: https://github.com/beliven-it/fastify-crud-generator#readme
- Issues: https://github.com/beliven-it/fastify-crud-generator/issues
- npm.io page: https://npm.io/package/fastify-crud-generator

## Dependencies (1)

- [@fastify/error](https://npm.io/package/@fastify/error.md) ^3.4.0

## Recent versions

- 1.1.0 (latest) — 2024-03-23
- 1.0.1 — 2022-10-13
- 1.0.0 — 2021-11-18
- 0.1.2 — 2021-10-22
- 0.1.1 — 2021-10-22
- 0.0.3 — 2020-08-19
- 0.0.2 — 2020-08-19
- 0.0.1 — 2020-08-19

## 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)

---
_Source: https://npm.io/package/fastify-crud-generator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
