tRPCttRPC
Powered by
Sycamore WillowS
tRPC•2y ago•
8 replies
Sycamore Willow

How 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!
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Can I use tRPC on multiple platforms?
IGMTCryptoIIGMTCrypto / ❓-help
12mo ago
Error in tRPC with Clerk in t3-app router
CsarChvzCCsarChvz / ❓-help
3y ago
T3 app tRPC external calls
MadaShindeInaiMMadaShindeInai / ❓-help
3y ago