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) :
const createInnerTRPCContext = (opts: CreateContextOptions) => {
return {
session: opts.session,
db1,
db2,
};
};
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();
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);
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!
1 Reply
Sycamore Willow
Sycamore WillowOP8mo ago
I'm fairly certain the issue is where the db connection is establish in a globalThis. Currecntly focused on messing with that.
Want results from more Discord servers?
Add your server