is there a way to use Redis in drizzle

is there a way to use Redis in drizzle
2 Replies
zachr
zachr3mo ago
I don't believe so, at least according to their docs. What are you using Redis for that would require something like Drizzle? Are you just trying to cache sql query results while using Redis as middleware? I use the redis npm library with a few simple get, set and delete functions that cover my use cases, using Redis as a caching database. I would imagine there's ways to set it up to cache your drizzle sql queries with helper functions, but you would lose all of the drizzle type-safe benefits.
Dane
Dane3mo ago
You would need to wrap your query in a some sort of caching function, then return as the tables inferSelect type:
export function getUser(id: number) {
const cachedUser = await redis.get(`user:${id}`)
if (cachedUser) return JSON.parse(cachedUser) as userTable.$inferSelect
const user = await db.query.userTable.findFirst({ where: eq(userTable.id, id) })
redis.setex(`user:${id}`, 100, JSON.stringify(user))
return user
}
export function getUser(id: number) {
const cachedUser = await redis.get(`user:${id}`)
if (cachedUser) return JSON.parse(cachedUser) as userTable.$inferSelect
const user = await db.query.userTable.findFirst({ where: eq(userTable.id, id) })
redis.setex(`user:${id}`, 100, JSON.stringify(user))
return user
}
You can't use connect to redis from the drizzle-orm though
Want results from more Discord servers?
Add your server