Sycamore Willow
Explore posts from serversHow can I use multiple DBs with trpc in T3 app?
I'm cross posting to T3 Discord as well. Here's the Q:
Hey all, I'm using T3 for a proof of concept at the startup I work at. I'm really really digging this stack and want to pitch it to our team to replace our current stack.
I've chosen to work with Drizzle, can explain more why if necessary but will wave my hands at that for a moment.
One key thing I need to demonstrate in the POC is that I can run queries on multiple DBs that live on different hosts. I did not think I'd get stuck on this bit! But I'm stumped. Here's what I've done:
I have config and schema defined for two DBs. I'll call them DB1 and DB1. I'm able to connect to them separately with Drizzle Studio, so I know I have the connections working.
I assumed I could just put DB2 into the TRPC context here (src/server/api) :
const createInnerTRPCContext = (opts: CreateContextOptions) => {
return {
session: opts.session,
db1,
db2,
};
};
Then in an rpc when I use db2 like so:
const res = await ctx.db2.query.foo.findFirst();
I get the error relation "foo" does not exist. foo does exist on the schema for db2. Code completion knows about it and like I mentioned Drizzle Studio connects to db2 and I can see the foo table.
One more weirdsie!
Let's say there's a table called bar in db1.
I can call this successfully:
const data = await ctx.db2.select().from(bar).limit(10);
Clearly I'm doing something wrong and it isn't obvious to me how I get the second DB into context! Thanks in advance!
9 replies
TTCTheo's Typesafe Cult
•Created by Sycamore Willow on 5/3/2024 in #questions
Using Multiple DBs with Drizzle
Hey all, I'm using T3 for a proof of concept at the startup I work at. I'm really really digging this stack and want to pitch it to our team to replace our current stack.
I've chosen to work with Drizzle, can explain more why if necessary but will wave my hands at that for a moment.
One key thing I need to demonstrate in the POC is that I can run queries on multiple DBs that live on different hosts. I did not think I'd get stuck on this bit! But I'm stumped. Here's what I've done:
I have config and schema defined for two DBs. I'll call them DB1 and DB1. I'm able to connect to them separately with Drizzle Studio, so I know I have the connections working.
I assumed I could just put DB2 into the TRPC context here (
src/server/api
) :
Then in an rpc when I use db2 like so:
I get the error relation "foo" does not exist
. foo
does exist on the schema for db2. Code completion knows about it and like I mentioned Drizzle Studio connects to db2 and I can see the foo
table.
One more weirdsie!
Let's say there's a table called bar
in db1.
I can call this successfully:
Clearly I'm doing something wrong and it isn't obvious to me how I get the second DB into context! Thanks in advance!2 replies