Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NestJS example needed #32

Open
kdawgwilk opened this issue Nov 30, 2023 · 4 comments
Open

NestJS example needed #32

kdawgwilk opened this issue Nov 30, 2023 · 4 comments
Labels
documentation Improvements or additions to documentation

Comments

@kdawgwilk
Copy link

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.

@Bonafede
Copy link

Bonafede commented Dec 12, 2023

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.

@paulopgss
Copy link

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!

@janpio janpio added the documentation Improvements or additions to documentation label Feb 20, 2024
@cody-ax
Copy link

cody-ax commented Mar 16, 2024

Here is a working example:

prisma/prisma#18628 (comment)

@dminglv
Copy link

dminglv commented Apr 13, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

6 participants