Am I making too many PrismaClient objects?

I am working on a multi-tenant application.

I have two separate routers. One for cross-tenant queries and mutations named admin and one for individual facility data access named facility.

I created a Prisma extension to insert a where clause in all queries. The extension needs a facilityId from each request.

Because of this, I am calling the following code inside of createTRPCContext.

const facilityIdPrisma = new PrismaClient().$extends(
    createFacilityExtension(user.facilityId)
  ) as PrismaClient;


Does createTRPCContext run on every request to the API?

Am I creating a new Prisma client on every request?

My goal is to have cross-tenant data safety baked into the application. I want the API interaction from the front-end code to be very explicit.

api.admin.users.getAll() // Obviously a cross-tenant API call

api.user.getAll() // Obviously a single tenant API call

The admin router uses an adminProcedure that checks the database to ensure the user is actually an admin.

This application will be in a regulated environment so I will be hosting on AWS ECS and RDS.

Another option I am considering is Supabase and using RLS.

Thank you.
Was this page helpful?