|
| 1 | +--- |
| 2 | +title: Rust-free and driver adapter |
| 3 | +--- |
| 4 | + |
| 5 | +> Rust-free Prisma Client is Generally Available with [v6.16.0](https://github.com/prisma/prisma/releases/tag/6.16.0). |
| 6 | +
|
| 7 | +Learn how to use the new `prisma-client` provider with a custom output path and adapter driver. For more information see the [No Rust engine](https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/no-rust-engine) guide on Prisma docs. |
| 8 | + |
| 9 | +Make sure you have `prisma` and `@prisma/client` v6.16.0 or later installed. |
| 10 | + |
| 11 | +## Configure Rust-free Prisma Client |
| 12 | + |
| 13 | +Configure your Prisma schema to use the new `prisma-client` provider and set a output directory. You can include the output directory in git, because it doesn't contain the rust query engine binary. |
| 14 | + |
| 15 | +```prisma |
| 16 | +// prisma/schema.prisma |
| 17 | +generator client { |
| 18 | + provider = "prisma-client" |
| 19 | + output = "../src/generated/prisma" |
| 20 | + engineType = "client" |
| 21 | + runtime = "nodejs" |
| 22 | + moduleFormat = "cjs" |
| 23 | +} |
| 24 | +
|
| 25 | +datasource db { |
| 26 | + provider = "sqlite" |
| 27 | + url = "file:dev.db" |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +Read more about additional [fields](https://www.prisma.io/docs/orm/reference/prisma-schema-reference#fields-for-prisma-client-provider) for the `prisma-client` provider. |
| 32 | + |
| 33 | +Add your models to the `schema.prisma`, run `npx prisma generate` and run the prisma migration `npx prisma migrate dev`. |
| 34 | + |
| 35 | +### Prisma config (optional) |
| 36 | + |
| 37 | +Add at the root level the `prisma.config.ts` to [configure](https://www.prisma.io/docs/orm/reference/prisma-config-reference) the Prisma CLI for commands like `migrate`, `seed`. |
| 38 | + |
| 39 | +```ts |
| 40 | +// prisma.config.ts |
| 41 | +import path from 'node:path'; |
| 42 | +import { defineConfig } from 'prisma/config'; |
| 43 | + |
| 44 | +export default defineConfig({ |
| 45 | + schema: path.join('prisma', 'schema.prisma'), |
| 46 | + migrations: { |
| 47 | + path: './prisma/migrations', |
| 48 | + seed: 'tsx ./prisma/seed.ts', |
| 49 | + }, |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +### Install driver adapter |
| 54 | + |
| 55 | +You need to [install a driver adapter](https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/no-rust-engine#3-install-the-driver-adapter) depending on your database. |
| 56 | + |
| 57 | +For the example, use install the driver adatper for SQLite. |
| 58 | + |
| 59 | +```bash |
| 60 | +npm install @prisma/adapter-better-sqlite3 |
| 61 | +``` |
| 62 | + |
| 63 | +### Prisma Client instance |
| 64 | + |
| 65 | +Now create a `PrismaClient` instance from your custom output and configure the driver adapter. |
| 66 | + |
| 67 | +```ts |
| 68 | +// src/prisma.extension.ts |
| 69 | +import { PrismaBetterSQLite3 } from '@prisma/adapter-better-sqlite3'; |
| 70 | +import { PrismaClient } from './generated/prisma/client'; |
| 71 | + |
| 72 | +const adapter = new PrismaBetterSQLite3({ url: 'file:./prisma/dev.db' }); |
| 73 | +export const prisma = new PrismaClient({ adapter }); |
| 74 | + |
| 75 | +export type PrismaClientType = typeof prisma; |
| 76 | +``` |
| 77 | + |
| 78 | +You can also use `process.env.DATABASE_URL` for setting the url in the adapter. |
| 79 | + |
| 80 | +### Seed database |
| 81 | + |
| 82 | +Add `seed.ts` in your prisma directory and reuse the prisma client instance for seeding your development environment. |
| 83 | + |
| 84 | +```ts |
| 85 | +// prisma/seed.ts |
| 86 | +// reuse the prisma client instance |
| 87 | +import { prisma } from '../src/prisma.extension'; |
| 88 | + |
| 89 | +async function main() { |
| 90 | + console.log('Seeding database...'); |
| 91 | + console.time('Seeding complete 🌱'); |
| 92 | + |
| 93 | + // TODO seed development data |
| 94 | + |
| 95 | + console.timeEnd('Seeding complete 🌱'); |
| 96 | +} |
| 97 | + |
| 98 | +main().catch((e) => console.log(e)); |
| 99 | +``` |
| 100 | + |
| 101 | +## Use Rust-free Prisma Client |
| 102 | + |
| 103 | +Register your `prisma` instance with the `CustomPrismaModule`. |
| 104 | + |
| 105 | +```ts |
| 106 | +import { Module } from '@nestjs/common'; |
| 107 | +import { AppController } from './app.controller'; |
| 108 | +import { AppService } from './app.service'; |
| 109 | +import { CustomPrismaModule } from 'nestjs-prisma/dist/custom'; |
| 110 | +import { prisma } from './prisma.extension'; |
| 111 | + |
| 112 | +@Module({ |
| 113 | + imports: [ |
| 114 | + CustomPrismaModule.forRootAsync({ |
| 115 | + name: 'PrismaService', |
| 116 | + useFactory: () => { |
| 117 | + return prisma; |
| 118 | + }, |
| 119 | + }), |
| 120 | + ], |
| 121 | + controllers: [AppController], |
| 122 | + providers: [AppService], |
| 123 | +}) |
| 124 | +export class AppModule {} |
| 125 | +``` |
| 126 | + |
| 127 | +Query your database by injecting `CustomPrismaService` with your `PrismaClientType`. You have full access to the Prisma Client with `this.prismaService.client`. |
| 128 | + |
| 129 | +```ts |
| 130 | +import { Inject, Injectable } from '@nestjs/common'; |
| 131 | +import { CustomPrismaService } from 'nestjs-prisma/dist/custom'; |
| 132 | +import { PrismaClientType } from './prisma.extension'; |
| 133 | + |
| 134 | +@Injectable() |
| 135 | +export class AppService { |
| 136 | + constructor( |
| 137 | + // ✅ use `ExtendedPrismaClient` from extension for correct type-safety |
| 138 | + @Inject('PrismaService') |
| 139 | + private prismaService: CustomPrismaService<PrismaClientType>, |
| 140 | + ) {} |
| 141 | + |
| 142 | + users() { |
| 143 | + return this.prismaService.client.user.findMany(); |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +## Example |
| 149 | + |
| 150 | +Checkout the [Rust-free and driver adapter](https://github.com/notiz-dev/nestjs-prisma/blob/main/examples/extensions/src/query-logging.extension.ts) example on GitHub. |
0 commit comments