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
Neto16mo 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_.
._perry_.16mo ago
i'm fetching on the server with db.select
whatplan
whatplan16mo 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_.
._perry_.16mo 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
whatplan16mo 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_.
._perry_.16mo ago
I'm making a utility method I mean select in general Not just the user ID or something like that
whatplan
whatplan16mo ago
you want to cache every possible select call? I dont think you can, or would really even want to
._perry_.
._perry_.16mo ago
With prisma I could just do cache(prisma.type.thing) Not necessarily all
whatplan
whatplan16mo ago
can you explain what you were doing with prisma with an example
._perry_.
._perry_.16mo 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
whatplan16mo ago
https://orm.drizzle.team/docs/rqb just use relational queries and do the same thing
._perry_.
._perry_.16mo 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
whatplan16mo 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
whatplan16mo ago
caching everything under one mega function is a way to do it
whatplan
whatplan16mo ago
but probably not the best --- could you send some examples of the kinds of queries youll be writing you want cached
._perry_.
._perry_.16mo ago
You're right I should male one cache function for each page I'm so dumb
whatplan
whatplan16mo ago
@.perry. are you using planetscale serverless driver with drizzle
._perry_.
._perry_.16mo ago
i'm using neondb and the pg driver because it's what lucia-auth supports
whatplan
whatplan16mo 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_.
._perry_.16mo ago
i'm doing that
Want results from more Discord servers?
Add your server