Skip to content

Commit ffa1741

Browse files
authored
rust-free and driver adapter example/doc (#115)
* add driver example * add driver example using prisma-client * add rust-free-and-driver-adapter page
1 parent f497fe6 commit ffa1741

35 files changed

+14965
-3
lines changed

docs/src/components/Navigation.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const currentPageMatch = currentPage.endsWith('/')
3030
<li>
3131
<a
3232
href={link.link}
33-
class={`flex gap-1 border-l pl-3 ${
33+
class={`flex gap-2 items-start border-l pl-3 ${
3434
currentPageMatch === link.link
3535
? 'border-current text-violet-500 dark:text-violet-400'
3636
: 'border-transparent text-gray-700 hover:border-current hover:border-gray-500 dark:text-gray-300 dark:hover:border-gray-400'

docs/src/config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ export const navigation: Navigation[] = [
5757
title: 'Prisma Client Extensions',
5858
link: '/docs/prisma-client-extensions',
5959
},
60+
{
61+
title: 'Rust-free and Driver Adapters',
62+
link: '/docs/rust-free-and-driver-adapter',
63+
badge: 'New',
64+
},
6065
{
6166
title: 'Query Logging Extension',
6267
link: '/docs/query-logging-extension',
@@ -100,6 +105,11 @@ export const examples: Example[] = [
100105
description: 'NestJS app with Express, Prisma and nestjs-prisma.',
101106
link: 'https://github.com/notiz-dev/nestjs-prisma/tree/main/examples/basics',
102107
},
108+
{
109+
name: 'Rust-free and driver adapter',
110+
description: 'NestJS app with prisma-client (Rust-free), driver adapter and nestjs-prisma.',
111+
link: 'https://github.com/notiz-dev/nestjs-prisma/tree/main/examples/driver',
112+
},
103113
{
104114
name: 'Fastify',
105115
description: 'NestJS app with Fastify, Prisma and nestjs-prisma.',
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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.

examples/driver/.gitignore

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# compiled output
2+
/dist
3+
/node_modules
4+
/build
5+
6+
# Logs
7+
logs
8+
*.log
9+
npm-debug.log*
10+
pnpm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
lerna-debug.log*
14+
15+
# OS
16+
.DS_Store
17+
18+
# Tests
19+
/coverage
20+
/.nyc_output
21+
22+
# IDEs and editors
23+
/.idea
24+
.project
25+
.classpath
26+
.c9/
27+
*.launch
28+
.settings/
29+
*.sublime-workspace
30+
31+
# IDE - VSCode
32+
.vscode/*
33+
!.vscode/settings.json
34+
!.vscode/tasks.json
35+
!.vscode/launch.json
36+
!.vscode/extensions.json
37+
38+
# dotenv environment variable files
39+
.env
40+
.env.development.local
41+
.env.test.local
42+
.env.production.local
43+
.env.local
44+
45+
# temp directory
46+
.temp
47+
.tmp
48+
49+
# Runtime data
50+
pids
51+
*.pid
52+
*.seed
53+
*.pid.lock
54+
55+
# Diagnostic reports (https://nodejs.org/api/report.html)
56+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
57+
58+
# prisma
59+
prisma/dev.db*

examples/driver/.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

examples/driver/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<p align="center">
2+
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
3+
</p>
4+
5+
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6+
[circleci-url]: https://circleci.com/gh/nestjs/nest
7+
8+
<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
9+
<p align="center">
10+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
11+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
12+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
13+
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
14+
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
15+
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
16+
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
17+
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg" alt="Donate us"/></a>
18+
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
19+
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow" alt="Follow us on Twitter"></a>
20+
</p>
21+
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
22+
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
23+
24+
## Description
25+
26+
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
27+
28+
## Project setup
29+
30+
```bash
31+
$ npm install
32+
```
33+
34+
## Compile and run the project
35+
36+
```bash
37+
# development
38+
$ npm run start
39+
40+
# watch mode
41+
$ npm run start:dev
42+
43+
# production mode
44+
$ npm run start:prod
45+
```
46+
47+
## Run tests
48+
49+
```bash
50+
# unit tests
51+
$ npm run test
52+
53+
# e2e tests
54+
$ npm run test:e2e
55+
56+
# test coverage
57+
$ npm run test:cov
58+
```
59+
60+
## Deployment
61+
62+
When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the [deployment documentation](https://docs.nestjs.com/deployment) for more information.
63+
64+
If you are looking for a cloud-based platform to deploy your NestJS application, check out [Mau](https://mau.nestjs.com), our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:
65+
66+
```bash
67+
$ npm install -g @nestjs/mau
68+
$ mau deploy
69+
```
70+
71+
With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.
72+
73+
## Resources
74+
75+
Check out a few resources that may come in handy when working with NestJS:
76+
77+
- Visit the [NestJS Documentation](https://docs.nestjs.com) to learn more about the framework.
78+
- For questions and support, please visit our [Discord channel](https://discord.gg/G7Qnnhy).
79+
- To dive deeper and get more hands-on experience, check out our official video [courses](https://courses.nestjs.com/).
80+
- Deploy your application to AWS with the help of [NestJS Mau](https://mau.nestjs.com) in just a few clicks.
81+
- Visualize your application graph and interact with the NestJS application in real-time using [NestJS Devtools](https://devtools.nestjs.com).
82+
- Need help with your project (part-time to full-time)? Check out our official [enterprise support](https://enterprise.nestjs.com).
83+
- To stay in the loop and get updates, follow us on [X](https://x.com/nestframework) and [LinkedIn](https://linkedin.com/company/nestjs).
84+
- Looking for a job, or have a job to offer? Check out our official [Jobs board](https://jobs.nestjs.com).
85+
86+
## Support
87+
88+
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
89+
90+
## Stay in touch
91+
92+
- Author - [Kamil Myśliwiec](https://twitter.com/kammysliwiec)
93+
- Website - [https://nestjs.com](https://nestjs.com/)
94+
- Twitter - [@nestframework](https://twitter.com/nestframework)
95+
96+
## License
97+
98+
Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE).

examples/driver/eslint.config.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @ts-check
2+
import eslint from '@eslint/js';
3+
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
4+
import globals from 'globals';
5+
import tseslint from 'typescript-eslint';
6+
7+
export default tseslint.config(
8+
{
9+
ignores: ['eslint.config.mjs'],
10+
},
11+
eslint.configs.recommended,
12+
...tseslint.configs.recommendedTypeChecked,
13+
eslintPluginPrettierRecommended,
14+
{
15+
languageOptions: {
16+
globals: {
17+
...globals.node,
18+
...globals.jest,
19+
},
20+
sourceType: 'commonjs',
21+
parserOptions: {
22+
projectService: true,
23+
tsconfigRootDir: import.meta.dirname,
24+
},
25+
},
26+
},
27+
{
28+
rules: {
29+
'@typescript-eslint/no-explicit-any': 'off',
30+
'@typescript-eslint/no-floating-promises': 'warn',
31+
'@typescript-eslint/no-unsafe-argument': 'warn'
32+
},
33+
},
34+
);

examples/driver/nest-cli.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://json.schemastore.org/nest-cli",
3+
"collection": "@nestjs/schematics",
4+
"sourceRoot": "src",
5+
"compilerOptions": {
6+
"deleteOutDir": true
7+
}
8+
}

0 commit comments

Comments
 (0)