getPost() / getPosts() / ... what's your opinion? + any TS magician here :) ?
Hello!
I was thinking of creating functions which will execute my queries, instead of putting all queries in my components. With that in place, I would be able to switch between Drizzle with any other query build / orm if I would like. To put it simply:
1. What do you think about this approach? i think it's convenient, abstraction does not seem overkilled, and I think it's quiet common, but I don't have seen it in tutorials / courses, but I think it's cleaner and more maintanable than putting queries everywhere in your code
2. I needed to add some types (id param + function return) to make it better to use, but I still don't know how or if it would be possible to type the options object which should take all params accepted in the findFirst method (such as columns, where...)
12 Replies
I think this is a great pattern. Is your component running on the server, the client or both?
Having ui code and database code in the same function seems weird to me
Why would I have that kind of interlocking
Im using Next.js with React Server Components, so my code example in this case would run on the server
I actually go beyond that, I created a facade https://medium.com/drizzle-stories/how-i-migrated-from-prisma-to-drizzleorm-with-absolutely-no-hassle-and-zero-downtime-9f5f0881fc04
Medium
How I migrated from Prisma to DrizzleORM with absolutely no hassle ...
If you are reading this, I take it that you looking into moving from Prisma to Drizzle, I didn’t go over why I did it, but if you want me…
But a React component can be turned into a client component, so it could potentially runs on the client (again, in the example, its on the server there)
I don't let any of my ui code call any db directly
You could do it in pet projects but you need modularity in bigger/more proffesional environment
Anyway, don't worry about option 2, that's pretty easy as well
im a bit confused "i dont let any of my ui code call any db directly", do you mean thats what im doing in my example? because thats also what I try to avoid 😄
No, that's exacly what you're avoiding in you example.
im going through your article, great stuff! i was also thinking about creating a facade, Im improving my skills on a pet project so I try not to go too far, but at the same time, I try to make my code be "elegant", and thats kind of frustating to not have many resources showing such pattern in tutorials / courses
Anyway, don't worry about option 2, that's pretty easy as wellany help? 😄 I don't think you speak about it in your article, except if I missed something?
Check this question here: https://github.com/drizzle-team/drizzle-orm/discussions/1483
GitHub
Relations input · drizzle-team drizzle-orm · Discussion #1483
I have a schema that looks like somewhat like this: export const employeesSchema = pgTable('employees', { firstName: varchar('first_name', { length: 256 }), lastName: varchar('l...
Check this one as well https://discord.com/channels/1043890932593987624/1186342202952138835
thanks i'll read that!
hoping drizzle team will provide more convenient type helpers soon!
I was working on a PR for that last night. Just waiting for the drizzle team to confirm it's a good API