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:
// lib/fetchers/post.ts
export async function getPost(id: Post["id"], options = {}): Promise<Post | undefined> {
const data = await db.query.post.findFirst({
where: eq(post.id, id),
...options
})

return data
}


// component.ts
export async function Component() {
const post = await getPost(1);

return <h1>{post.title}</h1>
}
// lib/fetchers/post.ts
export async function getPost(id: Post["id"], options = {}): Promise<Post | undefined> {
const data = await db.query.post.findFirst({
where: eq(post.id, id),
...options
})

return data
}


// component.ts
export async function Component() {
const post = await getPost(1);

return <h1>{post.title}</h1>
}
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
Angelelz
Angelelz•9mo ago
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
couincouin007
couincouin007•9mo ago
Im using Next.js with React Server Components, so my code example in this case would run on the server
Angelelz
Angelelz•9mo ago
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…
couincouin007
couincouin007•9mo ago
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)
Angelelz
Angelelz•9mo ago
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
couincouin007
couincouin007•9mo ago
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 😄
Angelelz
Angelelz•9mo ago
No, that's exacly what you're avoiding in you example.
couincouin007
couincouin007•9mo ago
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 well
any help? 😄 I don't think you speak about it in your article, except if I missed something?
Angelelz
Angelelz•9mo ago
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...
couincouin007
couincouin007•9mo ago
thanks i'll read that! hoping drizzle team will provide more convenient type helpers soon!
Angelelz
Angelelz•9mo ago
I was working on a PR for that last night. Just waiting for the drizzle team to confirm it's a good API
Want results from more Discord servers?
Add your server