PrismaModule as globle in neatjs with prisma service and dburl

How to #parametrize db url if i want to always pass this url from my request. I want like db per tanant model...
2 Replies
RaphaelEtim
RaphaelEtim4w ago
Hi @hari_narayan You can create a new PrismaClient instance for each request, passing the tenant-specific database URL:
import { PrismaClient } from '@prisma/client'

// Assuming you have the tenant's database URL in the request
const tenantDbUrl = req.headers['x-tenant-db-url'] // or however you're passing it

const prisma = new PrismaClient({
datasources: {
db: {
url: tenantDbUrl,
},
},
})
import { PrismaClient } from '@prisma/client'

// Assuming you have the tenant's database URL in the request
const tenantDbUrl = req.headers['x-tenant-db-url'] // or however you're passing it

const prisma = new PrismaClient({
datasources: {
db: {
url: tenantDbUrl,
},
},
})
See this documentation: https://www.prisma.io/docs/orm/reference/prisma-client-reference#programmatically-override-a-datasource-url
Prisma Client API | Prisma Documentation
API reference documentation for Prisma Client.
RaphaelEtim
RaphaelEtim3w ago
This is another example from the documentation that you can reference. https://www.prisma.io/docs/orm/more/help-and-troubleshooting/nextjs-help#handling-dynamic-scenarios
Comprehensive Guide to Using Prisma ORM with Next.js | Prisma Docum...
Learn best practices, monorepo strategies, and dynamic usage techniques for Prisma ORM in Next.js applications.

Did you find this page helpful?