Nabin saud
Nabin saud
PPrisma
Created by Nabin saud on 2/18/2025 in #help-and-questions
Multischema prisma dynamic support issues
and the User controller look like this Note: for the sake of testing i have called the prisma service in controller -
export class UserController {
constructor(
private readonly userService: UserService,
private readonly prismaClientManager: PrismaClientManager,
) {}

@Post()
async create(
@Body() createUserDto: CreateUserDto,
@Request() request: ExpRequest,
) {
const client = this.prismaClientManager.getClient(request);
console.log('customers', await client.customer.findMany());
return this.userService.create(createUserDto);
}
export class UserController {
constructor(
private readonly userService: UserService,
private readonly prismaClientManager: PrismaClientManager,
) {}

@Post()
async create(
@Body() createUserDto: CreateUserDto,
@Request() request: ExpRequest,
) {
const client = this.prismaClientManager.getClient(request);
console.log('customers', await client.customer.findMany());
return this.userService.create(createUserDto);
}
when submit the api endpoint request
curl --location 'localhost:3000/user' \
--header 'x-tenant-id: abc_tenant' \
--header 'Content-Type: application/json' \
--data '{
"name":"abc client"
}'
curl --location 'localhost:3000/user' \
--header 'x-tenant-id: abc_tenant' \
--header 'Content-Type: application/json' \
--data '{
"name":"abc client"
}'
the and when logged it actually return the base schema data expected result: should return this schema specific data
database original env postgresql://postgres:postgres@localhost:5433/tenant_prisma?schema=public
database url postgresql://postgres:postgres@localhost:5433/tenant_prisma?schema=abc_tenant
no client so creating a new client
clients count 1
customers [
{
id: 'a3cbd0df-0c97-4b91-ac9f-6c379e858e43',
name: 'Nabin saud',
createdAt: 2025-02-18T09:43:01.369Z,
updatedAt: 2025-02-18T09:43:01.369Z
}
]
database original env postgresql://postgres:postgres@localhost:5433/tenant_prisma?schema=public
database url postgresql://postgres:postgres@localhost:5433/tenant_prisma?schema=abc_tenant
no client so creating a new client
clients count 1
customers [
{
id: 'a3cbd0df-0c97-4b91-ac9f-6c379e858e43',
name: 'Nabin saud',
createdAt: 2025-02-18T09:43:01.369Z,
updatedAt: 2025-02-18T09:43:01.369Z
}
]
even creating more it create data in base_schema_template not in abc schema template the url of db is changing but not working how to fix this issues @jonfanz @ryanchenkie ```
5 replies
PPrisma
Created by Nabin saud on 2/18/2025 in #help-and-questions
Multischema prisma dynamic support issues
@Injectable()
export class PrismaClientManager implements OnModuleDestroy {
// the client instances cache object
private clients: { [key: string]: PrismaClient } = {};

getTenantId(request: Request): string {
console.log('req', request.headers['x-tenant-id']);
if (request.headers['x-tenant-id']) {
return request.headers['x-tenant-id'] as string;
}
return '';
}

getClient(request: Request): PrismaClient {
const tenantId = this.getTenantId(request);
const client = this.clients[tenantId];
console.log('already existing client ', client);

console.log('database original env', process.env.DATABASE_URL);
const databaseUrl = process.env.DATABASE_URL.replace('public', tenantId);
console.log('database url', databaseUrl);
if (!client) {
console.log('no client so creating a new client');
const newClient = new PrismaClient({
datasources: {
db: {
url: databaseUrl,
},
},
});
// console.log('new client', newClient);
this.clients[tenantId] = newClient;
console.log('clients count', Object.keys(this.clients).length);
return newClient;
}
return client;
}

async onModuleDestroy() {
await Promise.all(
Object.values(this.clients).map((client) => client.$disconnect()),
);
}
}
@Injectable()
export class PrismaClientManager implements OnModuleDestroy {
// the client instances cache object
private clients: { [key: string]: PrismaClient } = {};

getTenantId(request: Request): string {
console.log('req', request.headers['x-tenant-id']);
if (request.headers['x-tenant-id']) {
return request.headers['x-tenant-id'] as string;
}
return '';
}

getClient(request: Request): PrismaClient {
const tenantId = this.getTenantId(request);
const client = this.clients[tenantId];
console.log('already existing client ', client);

console.log('database original env', process.env.DATABASE_URL);
const databaseUrl = process.env.DATABASE_URL.replace('public', tenantId);
console.log('database url', databaseUrl);
if (!client) {
console.log('no client so creating a new client');
const newClient = new PrismaClient({
datasources: {
db: {
url: databaseUrl,
},
},
});
// console.log('new client', newClient);
this.clients[tenantId] = newClient;
console.log('clients count', Object.keys(this.clients).length);
return newClient;
}
return client;
}

async onModuleDestroy() {
await Promise.all(
Object.values(this.clients).map((client) => client.$disconnect()),
);
}
}
5 replies
PPrisma
Created by Nabin saud on 10/3/2024 in #help-and-questions
How to extends the client for adding soft delete features currently this is not working how to fix ?
how to solve that ?
6 replies