Create typesafe proxy servers/clients for your PrismaClient
npm i prisma-proxy-fetch-client
npm i prisma-proxy-express-server
See individual libraries for more detailed demo & readme
// schema.prisma
model Post {
id Int @id @default(autoincrement())
title String
content String?
}
// server.ts
createPrismaExpressProxy({
app: express(),
prisma: new PrismaClient(),
apiPrefix: 'prisma',
// default middleware returns
// status 403 for ALL routes
// unless explicitely overriden
middleware: {
post: {
// unrestricted
findMany: (req, res, next) => next(),
// restricted
create: (req, res, next) => {
// do some custom auth logic
if (isPermitted(req)) {
next();
} else {
next(new Error());
}
},
},
},
});
// client.ts
const baseURL = 'http://localhost:3333';
const api = createFetchClient<PrismaClient>(baseURL);
// makes a post request to:
// http://localhost:3333/post/findMany
// { "where": { "title": "My First Post" } }
const posts: Post[] = await api.post.findMany({
where: { title: 'My First Post' },
});
Adds an express api route to your express app that acts as a proxy for your prisma client All requests return 403: Forbidden until you explicitly enable them with middleware
Creates a tiny (454 bytes minified & gzipped), typesafe api client for your express server proxy
# install dependencies
npm install
# generate the prisma client
npx prisma generate
# serve the api and web project
npm run serve