Using cache with drizzle

How exactly would I use cache with the select from drizzle? if it was just the method it would be simple, but it returns from that returns where and there's all the other methods. How the hell am I supposed to do this?
Solution:
caching everything under one mega function is a way to do it
Jump to solution
21 Replies
Neto
Neto2y ago
React query with keys and no refetch Any new combination would be fetch on demand Any old combination would be csched already and there would be no need to fetch again
Perry
PerryOP2y ago
i'm fetching on the server with db.select
whatplan
whatplan2y ago
import { cache } from 'react'

export const getUser = cache(async (id: string) => {
const user = await db.user.findUnique({ id })
return user
})

export default async function Page({
params: { id },
}: {
params: { id: string }
}) {
const user = await getUser(id)
// ...
}
import { cache } from 'react'

export const getUser = cache(async (id: string) => {
const user = await db.user.findUnique({ id })
return user
})

export default async function Page({
params: { id },
}: {
params: { id: string }
}) {
const user = await getUser(id)
// ...
}
Perry
PerryOP2y ago
That's for prisma I know how to use cache I don't know how to use cache with the select method from drizzle
whatplan
whatplan2y ago
what does that mean? is it not the exact same thing
export const getUser = cache(async (id: string) => {
const user = await db.select().from(users).where(eq(users.id, id));
return user
})
export const getUser = cache(async (id: string) => {
const user = await db.select().from(users).where(eq(users.id, id));
return user
})
Perry
PerryOP2y ago
I'm making a utility method I mean select in general Not just the user ID or something like that
whatplan
whatplan2y ago
you want to cache every possible select call? I dont think you can, or would really even want to
Perry
PerryOP2y ago
With prisma I could just do cache(prisma.type.thing) Not necessarily all
whatplan
whatplan2y ago
can you explain what you were doing with prisma with an example
Perry
PerryOP2y ago
cache(prisma.user.findFirst)
cache(prisma.user.findFirst)
It would cover every single findFirst for the type I can cover every single from manually
whatplan
whatplan2y ago
https://orm.drizzle.team/docs/rqb just use relational queries and do the same thing
Perry
PerryOP2y ago
But the problem is the select and where methods I'll try Brb i'm using lucia-auth and i'm not sure relations are supported
whatplan
whatplan2y ago
heres the thing though you should only be caching one set query at a time once you start adding different where and limit whatever things its just a completely different query
Solution
whatplan
whatplan2y ago
caching everything under one mega function is a way to do it
whatplan
whatplan2y ago
but probably not the best --- could you send some examples of the kinds of queries youll be writing you want cached
Perry
PerryOP2y ago
You're right I should male one cache function for each page I'm so dumb
whatplan
whatplan2y ago
@.perry. are you using planetscale serverless driver with drizzle
Perry
PerryOP2y ago
i'm using neondb and the pg driver because it's what lucia-auth supports
whatplan
whatplan2y ago
ive been rereading the next docs and playing around with a test project my mental mode for cache was a bit off its only purpose is to dedupe calls to the same underlying code when called from multiple components in a render step so for querying a database client its only necessary if you are making the exact same query (like every single aspect) the same in multiple places
Perry
PerryOP2y ago
i'm doing that
Want results from more Discord servers?
Add your server