Automatic CRUD endpoints with automatic creation of zod validators and typing from Drizzle-orm schema (drizzle-orm/pg-core) #3076
Replies: 5 comments 9 replies
-
It's better to replace my experimental "tableToZodSchema" with the drizzle extension drizzle-zod, createInsertSchema. i.e.: import { createInsertSchema } from 'drizzle-zod'
const schema = createInsertSchema(table) So the whole |
Beta Was this translation helpful? Give feedback.
-
Hi @Meess Interesting! I've also tried to use Drizzle and Zod stack: https://github.com/yusukebe/testing-d1-app-with-types That was a good experience; it will automatically add the validator and the client types if the database schema is set. |
Beta Was this translation helpful? Give feedback.
-
Nice work @Meess, I am interested in trying to help figure out how this can help the community! I am working on a template with hono in it, and also using zod, drizzle, pg. Beyond that we also use zod-openapi and scalar to generate docs like this: https://api.cellajs.com/docs. Cella could benefit from having an easy way to add CRUD endpoints for sure! Would love to know if you would be interested in adapting this for more complex scenarios: so that it's also usable in that way that cella uses hono zod-openapi middleware? And secondly, how to allow for customization of the business logic as you inevitably always want to customize endpoints eventually? Here is a link to one of our endpoints and you see that we also have the ability to add endpoint specific middleware for each route: That said, Maybe I am making it too complicated and larger applications are not the target you want to focus on, but just something to consider perhaps. |
Beta Was this translation helpful? Give feedback.
-
Hey everyone, so for the past few months I was also working on a solution like this in a private repo. I just open-sourced it, you can check it out here - https://github.com/rhinobase/honohub and the package is
Feature planned, these are things that I am still cleaning up / building and will be adding to the package soon -
I am planning to finish these by mid-July. The API is still quite fresh and a lot of trial and error is required to make it a robust lib, but it's a start. Give it a try if you can (I know I have to work on the docs more but there is an example - https://github.com/rhinobase/honohub/blob/main/apps/sandbox/src/index.ts) and let me know what are your thoughts on this and how can we improve it. |
Beta Was this translation helpful? Give feedback.
-
Nice work. I did similar with trpc and even auto ui generator on tanstack router. But was too hard to have something prod ready. Can share if interested if you can extend the code to work with hono openapi zod it would be even better |
Beta Was this translation helpful? Give feedback.
-
As a long time Django (Python MVC framework) dev I've always liked django-admin and DRF (Django Rest Framework) which work very closely with their ORM to manage data and automatically create endpoints and authorization.
Now using hono I really liked the hono/client (type inference from the API to the frontend something that's not possible when using Python atm), but I prefer to not do things manually when they can be automated. As Drizzle provides proper typing (and attributes from which you can derive how the schema looks), I wanted to automatically create validators for a defined schema that are directly reflected in the client typings (no more mismatch between backend and frontend typings, or manual updating).
let
user/schema.ts
With two lines of code all crud endpoints can be created, which are Zod validated via zValidator and thus the types of the request body are directly reflected in the client (via hono/client).
And when looking in the client:
It's just a POC to see if we could harness the power of normally tRPC but lightweight with automated validation through zValidator, with hono/client to directly reflect it in the client 💪. Curious what other people's thoughts on it are.
Code that you can simply copy and use, only have to change the db import (side note, now only focussed on postgres):
And the part that is not Hono related, but extracts the zod schema's from the Drizzle pg table:
Beta Was this translation helpful? Give feedback.
All reactions