extension-read-replicas icon indicating copy to clipboard operation
extension-read-replicas copied to clipboard

NestJS example needed

Open kdawgwilk opened this issue 2 years ago • 6 comments

Prisma extensions are fairly new and there are not many examples of how to use them with the NestJS framework. I think this official extension would be a great place to have some documentation on how you could use this extension with NestJS.

kdawgwilk avatar Nov 30 '23 08:11 kdawgwilk

On nestjs we usually call PrismaService that is automatically injected globally on the framework. For me i didn't use this extension and just create a PrismaReadservice


import { Injectable } from '@nestjs/common';
import { Prisma, PrismaClient } from '@prisma/client';
import { logger } from '../logger/logger';

@Injectable()
export class PrismaReadService extends PrismaClient<
  Prisma.PrismaClientOptions,
  'query' | 'info' | 'warn' | 'error'
> {
  constructor() {
    let dbReadUrl = process.env.DATABASE_READ_URL;

    if (!dbReadUrl) {
        throw new Error('NO DATABASE URL DEFINED');
    }

    super({
      datasourceUrl: dbReadUrl
    });
  }
}

So i just use it as prismaService for write and PrismaReadService. It works but is not like this extension.

@Injectable()
export class AuthService {
  constructor(
    private readonly prisma: PrismaService,
    private readonly prismaRead: PrismaReadService,

Ofc will your responsibility in your code to not using read service for writing something, the code will not complain.

Bonafede avatar Dec 12 '23 14:12 Bonafede

On nestjs we usually call PrismaService that is automatically injected globally on the framework. For me i didn't use this extension and just create a PrismaReadservice


import { Injectable } from '@nestjs/common';
import { Prisma, PrismaClient } from '@prisma/client';
import { logger } from '../logger/logger';

@Injectable()
export class PrismaReadService extends PrismaClient<
  Prisma.PrismaClientOptions,
  'query' | 'info' | 'warn' | 'error'
> {
  constructor() {
    let dbReadUrl = process.env.DATABASE_READ_URL;

    if (!dbReadUrl) {
        throw new Error('NO DATABASE URL DEFINED');
    }

    super({
      datasourceUrl: dbReadUrl
    });
  }
}

So i just use it as prismaService for write and PrismaReadService. It works but is not like this extension.

@Injectable()
export class AuthService {
  constructor(
    private readonly prisma: PrismaService,
    private readonly prismaRead: PrismaReadService,

Ofc will your responsibility in your code to not using read service for writing something, the code will not complain.

It worked well for me, thank you!

paulopgss avatar Dec 14 '23 13:12 paulopgss

Here is a working example:

https://github.com/prisma/prisma/issues/18628#issuecomment-1611889400

cody-ax avatar Mar 16 '24 07:03 cody-ax

Implemented an example for NestJS: https://github.com/dminglv/nestjs-prisma-postgres-cluster

dminglv avatar Apr 13 '24 15:04 dminglv

Here is a working example:

prisma/prisma#18628 (comment)

I have tried most of the solutions commented in that issue, but when trying to create a custom Prisma service with the readReplicas extension, I get a "maximum stack call exceeded" error that points to the extension code. I don't get the same error with other extensions.

@Injectable()
export class DatabaseService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
  readonly extendedClient = prismaExtendedClient(this)

  constructor() {
    super()

    new Proxy(this, {
      get: (target, property) => Reflect.get(property in this.extendedClient ? this.extendedClient : target, property),
    })
  }

  async onModuleInit() {
    await this.$connect()
  }

  async onModuleDestroy() {
    await this.$disconnect()
  }
}

MVitelli avatar Jun 13 '24 16:06 MVitelli