Auto CRUD
Auto CRUD
Most web applications that manage data in a database perform at least some form of create, read, update or delete (CRUD) operations on stored records. Moreover, web applications also often expose those operations via a REST API.
Most database tables - corresponding to the entities manged by the application - need their own CRUD controller and set of HTTP endpoints. Because of this, large applications can quickly become tedious to develop and maintain. However, the logic for each table is usually quite similar.
Auto CRUD has been developed to reduce this workload. It consists of an Express.js middleware to which an instance of Prisma client is provided. Thanks to Prisma's ability to infer the data schema from the database, Auto CRUD can automatically create CRUD controller and corresponding HTTP endpoints for each table.
Auto CRUD is provided, among others, as an NPM package which can be installed as follows:
npm install @moreillon/prisma-auto-crud
Once, installed, the package can be used such as in this example code:
import express from "express"
import { PrismaClient } from "@prisma/client"
import autoCrud from "@moreillon/prisma-auto-crud"
const prismaClient = new PrismaClient()
const { PORT = 7070 } = process.env
const app = express()
app.use(express.json())
app.use(autoCrud(prismaClient))
app.listen(PORT, () => {
console.log(`[Express] Listening on port ${PORT}`)
})
This exposes CRUD operations for each table via a REST API. For example, a new record in the table named project can be created using a POST request to the /project
route.
The source code for Auto CRUD is available on GitHub here.