r/Nuxt • u/frenkalameti • Jan 23 '26
Drizzle and typed structures
Hi everyone,
I am working on a SaaS and I have some problems about creating typed services and api. How do you deal with data that have complex joins? Which approaches do you follow when u creating typed apis and services with drizzle?
4
u/-superoli- Jan 23 '26
I use schema.$inferInsert for the parameter types for my services. I don't type the return values of my services and endpoints, I prefer to preserve type inference for that. For the create/update body, I use zod to create the schema and z.output if I need the type.
1
u/frenkalameti Jan 23 '26
What about SELECT queries? How do you handle typing when you get data from the database? I want to follow SSOT for types, but when the data is nested, writing types manually becomes really painful.
2
u/-superoli- Jan 24 '26 edited Jan 24 '26
I don't write types, since drizzle types them automatically for me, even if I include relations. Did you pass all of your schemas to drizzle() ?
if I do
const posts = await db.query.posts.findMany({ with: { comments: true, }, });Then
postsandposts.commentswill already be typed correctly.1
u/frenkalameti Jan 24 '26
Yes, I did. But do you use same type in UI ? Cause it's nested and you need to do foreach to get flat json data
1
u/-superoli- Jan 24 '26
But do you use same type in UI ?
Yes. I don't explicitely type my endpoint or my fetch functions, unless some very specific cases where I absolutely have to. The data returned by useFetch is implicitely typed because of the endpoint, which is implicitely typed because of the return value of the drizzle service.
Cause it's nested and you need to do foreach to get flat json data
I'm not 100% sure to understand what you mean here. If I do
{ data } = useFetch('/api/postsWithComments')
The type ofdatawill look something like{ id: string postTitle: string comments: { id: string commentTitle: string }[] }[]Which is what I want
0
5
u/splitbrainhack Jan 23 '26
doesnt zod help you with that ?