Automatic CRUD endpoints with automatic creation of zod validators and typing from Drizzle-orm schema (drizzle-orm/pg-core) #3076
Replies: 8 comments 15 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.
-
Hey there!! Have you guys advanced in any directions concerning this topic? I am creating an API with Hono + Drizzle that need a few CRUD endpoints and a generics class would be helpful! |
Beta Was this translation helpful? Give feedback.
-
wow, wasn't aware of #3869 amazing resource thank you, will join in. I'm in
similar situation with typescript issues, but actually at another project
we have huge monorepo (3 year old) with many microservices so that
structure forced us to generate types and export them into frontend, which
means we followed good typescript patterns from the start, so while it is
"slow", it is quite manageable, especially combined with turborepo the
build times are quite manageable and dx actually works if i work inside
single service since tss only work on those files and packages that the
service imports...
Another issue with type export is that I need to keep routes really clean..
I don't want to leak my db object to the frontend, so I don't install the
backend package into the frontend, but rather just generate types and then
copy them into the frontend. But this type file, the route that I am
exporting from must contain just route definitions, no injected servies or
stuff like that or I have to export these as well...
well it forces you to have some clean structure, so far so good. and with
ts 10x improvement in go next year life will be good :D
i completely derailed the convo at this point haha, but it is really nice
to randomly find people dealing with similar niche stuff as you haha
…On Wed, 26 Mar 2025 at 17:57, Flip van Haaren ***@***.***> wrote:
haha great. Yeah I like the stack too! Seems like we are on the same page
on many things indeed, we even share a focus on the education market it
seems. Thats cool. And yeah I also agree its perhaps not the most
worthwhile effort to automate generating new CRUD modules, with the rise of
AI working with/on multiple files at the same time. And yes, about a slow
TS, as you probably know there is a discussion on it here: #3869
<#3869>. I want to consider moving
to ArkType, but now is not a good time for me to do another refactor for
that, also because drizzle is looking at a new validation layer I heard and
also the dev behind hono-openapi is working on this:
https://github.com/standard-community/standard-openapi. So focusing
instead on what is already in cella, which is a lot of work by itself
—
Reply to this email directly, view it on GitHub
<#3076 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADWTG2Z3TUPD5B3EH3KP7FL2WLFANAVCNFSM6AAAAABVUW2VAKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTENRTGEZTKNY>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
This approach look ideal to achieve some similar to zenstack but with drizzle. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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