From 8575cf747632a3f0e3d7c5c861b75e7ea97571df Mon Sep 17 00:00:00 2001 From: Esteban Luchsinger Date: Wed, 12 Nov 2025 08:50:45 +0100 Subject: [PATCH 1/3] Migrate to use the new rust-free client --- content/recipes/prisma.md | 55 +++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/content/recipes/prisma.md b/content/recipes/prisma.md index c794f81f9e..1f8850baaf 100644 --- a/content/recipes/prisma.md +++ b/content/recipes/prisma.md @@ -75,7 +75,7 @@ Specify your output `path` for the generated Prisma client either by passing `-- ```groovy generator client { - provider = "prisma-client-js" + provider = "prisma-client" output = "../generated/prisma" } ``` @@ -96,13 +96,14 @@ Your database connection is configured in the `datasource` block in your `schema ```groovy datasource db { - provider = "sqlite" - url = env("DATABASE_URL") + provider = "sqlite" + url = env("DATABASE_URL") } generator client { - provider = "prisma-client-js" - output = "../generated/prisma" + provider = "prisma-client" + output = "../generated/prisma" + engineType = "client" } ``` @@ -128,13 +129,14 @@ If you're using PostgreSQL, you have to adjust the `schema.prisma` and `.env` fi ```groovy datasource db { - provider = "postgresql" - url = env("DATABASE_URL") + provider = "postgresql" + url = env("DATABASE_URL") } generator client { - provider = "prisma-client-js" - output = "../generated/prisma" + provider = "prisma-client" + output = "../generated/prisma" + engineType = "client" } ``` @@ -160,13 +162,14 @@ If you're using MySQL, you have to adjust the `schema.prisma` and `.env` files a ```groovy datasource db { - provider = "mysql" - url = env("DATABASE_URL") + provider = "mysql" + url = env("DATABASE_URL") } generator client { - provider = "prisma-client-js" - output = "../generated/prisma" + provider = "prisma-client" + output = "../generated/prisma" + engineType = "client" } ``` @@ -186,13 +189,14 @@ If you're using Microsoft SQL Server or Azure SQL Server, you have to adjust the ```groovy datasource db { - provider = "sqlserver" - url = env("DATABASE_URL") + provider = "sqlserver" + url = env("DATABASE_URL") } generator client { - provider = "prisma-client-js" - output = "../generated/prisma" + provider = "prisma-client" + output = "../generated/prisma" + engineType = "client" } ``` @@ -300,14 +304,27 @@ When setting up your NestJS application, you'll want to abstract away the Prisma Inside the `src` directory, create a new file called `prisma.service.ts` and add the following code to it: ```typescript -import { Injectable, OnModuleInit } from '@nestjs/common'; +import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common'; import { PrismaClient } from 'generated/prisma'; +import { PrismaPg } from "@prisma/adapter-pg"; // Postgres Adapter + @Injectable() -export class PrismaService extends PrismaClient implements OnModuleInit { +export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy { + constructor(options: { adapter: PrismaPg}) + { + super({ + adapter: options.adapter, + }); + } + async onModuleInit() { await this.$connect(); } + + async onModuleDestroy() { + await this.$disconnect(); + } } ``` From cbe61ec6e705ba51904d65ab69142f71310f8852 Mon Sep 17 00:00:00 2001 From: Esteban Luchsinger Date: Wed, 12 Nov 2025 08:56:57 +0100 Subject: [PATCH 2/3] Add section about the driver adapter --- content/recipes/prisma.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/content/recipes/prisma.md b/content/recipes/prisma.md index 1f8850baaf..28bc0fc424 100644 --- a/content/recipes/prisma.md +++ b/content/recipes/prisma.md @@ -77,6 +77,7 @@ Specify your output `path` for the generated Prisma client either by passing `-- generator client { provider = "prisma-client" output = "../generated/prisma" + engineType = "client" } ``` @@ -295,6 +296,24 @@ Note that during installation, Prisma automatically invokes the `prisma generate > info **Note** The `prisma generate` command reads your Prisma schema and updates the generated Prisma Client library inside `node_modules/@prisma/client`. +#### Install the corresponding database Adapter + +Prisma requires a database driver adapter to connect to your database. The driver adapter required depends on the database in use. + +For PostgreSQL: + +```bash +$ npm install @prisma/adapter-pg +``` + +For MSSQL: + +```bash +$ npm install @prisma/adapter-mssql +``` + +> info **Note** The list of database driver adapters can be found in the [Prisma documentation](https://www.prisma.io/docs/orm/overview/databases/database-drivers#database-driver-adapters). + #### Use Prisma Client in your NestJS services You're now able to send database queries with Prisma Client. If you want to learn more about building queries with Prisma Client, check out the [API documentation](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/crud). From 2e791fb7fdb2deb177a3c03fab843c1592785139 Mon Sep 17 00:00:00 2001 From: Esteban Luchsinger Date: Wed, 12 Nov 2025 09:07:08 +0100 Subject: [PATCH 3/3] Add factory function example for injecting PrismaService with database adapter --- content/recipes/prisma.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/content/recipes/prisma.md b/content/recipes/prisma.md index 28bc0fc424..11e9b046ae 100644 --- a/content/recipes/prisma.md +++ b/content/recipes/prisma.md @@ -349,6 +349,34 @@ export class PrismaService extends PrismaClient implements OnModuleInit, OnModul > info **Note** The `onModuleInit` is optional — if you leave it out, Prisma will connect lazily on its first call to the database. +In order to inject the database adapter into the `PrismaService`, use a factory in the corresponding module. In this example, the `PrismaService` is registered in the AppModule. + +```typescript + +function prismaServiceFactory() { + // Read the database URL directly from environment or use ConfigService + const databaseUrl = process.env.DATABASE_URL; + if (!databaseUrl) { + throw new Error("DATABASE_URL is not set"); + } + + // Initialize the database driver adapter + const pg = new PrismaPg({ connectionString: databaseUrl }); + + // Inject the adapter + return new PrismaService({ adapter: pg }); +} + +@Module({ + providers: [ + { + provide: PrismaService, + useFactory: prismaServiceFactory + }] +}) +export class AppModule {} +``` + Next, you can write services that you can use to make database calls for the `User` and `Post` models from your Prisma schema. Still inside the `src` directory, create a new file called `user.service.ts` and add the following code to it: