Fastify Plugin to create CRUD routes. This plugin is good for fast prototyping.
npm i @kenzhemir/fastify-crud
import Fastify from 'fastify'
import crudPlugin from 'fastify-crud-plugin'
const fastify = Fastify()
fastify.register(crudPlugin, {
baseUrl: '/api/v1/books',
create: async (resource) => {/* Insert resource into DB */},
read: async (resourceId) => {/* Select resource from DB */},
update: async (resourceId, resource) => {/* Update resource in DB */},
delete: async (resourceId) => {/* Delete resource from */},
schemas: {
IdParam: {/*...*/},
CreateBody: {/*...*/},
UpdateBody: {/*...*/}
}
})
fastify.listen(3000)
-
baseUrl
(required): This URL is used to create the routes. -
create
: This function is used to create a resource for the POST {baseUrl} route. -
read
: This function is used to read a resource for the GET {baseUrl}/:id route. -
update
: This function is used to update a resource for the PUT {baseUrl}/:id route. -
delete
: This function is used to delete a resource for the DELETE {baseUrl}/:id route. -
schemas
: Optional validation schemas-
IdParam
: Schema for validating :id parameter. Adding validation schema can impact the id argument. -
CreateBody
: Schema for validating resource forCreate
. -
UpdateBody
: Schema for validating resource forUpdate
.
-
-
defaultRouteParams
: Object that will be used as a base for all the Route objects. This is an escape hatch in case you want to minimally configure the Routes' behaviour