diff --git a/content/800-guides/080-turborepo.mdx b/content/800-guides/080-turborepo.mdx index b5f0078c93..6580182f7b 100644 --- a/content/800-guides/080-turborepo.mdx +++ b/content/800-guides/080-turborepo.mdx @@ -185,6 +185,7 @@ Inside the `database` directory, initialize prisma by running: This will create several files inside `packages/database`: - A `prisma` directory with a `schema.prisma` file. +- A `prisma.config.ts` file for configuring Prisma - A Prisma Postgres database. - A `.env` file containing the `DATABASE_URL` at the project root. - An `output` directory for the generated Prisma Client as `generated/prisma`. @@ -193,7 +194,7 @@ In the `packages/database/prisma/schema.prisma` file, add the following models: ```prisma file=packages/database/prisma/schema.prisma generator client { - provider = "prisma-client-js" + provider = "prisma-client" output = "../generated/prisma" } diff --git a/content/800-guides/090-nextjs.mdx b/content/800-guides/090-nextjs.mdx index d474cacbbe..445d252b3e 100644 --- a/content/800-guides/090-nextjs.mdx +++ b/content/800-guides/090-nextjs.mdx @@ -70,13 +70,13 @@ To get started with Prisma, you'll need to install a few dependencies: ```terminal npm install prisma tsx --save-dev -npm install @prisma/extension-accelerate @prisma/client +npm install @prisma/extension-accelerate @prisma/client dotenv ``` ```terminal npm install prisma tsx --save-dev -npm install @prisma/client +npm install @prisma/client dotenv ``` @@ -93,6 +93,7 @@ You'll need to answer a few questions while setting up your Prisma Postgres data This will create: - A `prisma` directory with a `schema.prisma` file. +- A `prisma.config.ts` file for configuring Prisma - A Prisma Postgres database. - A `.env` file containing the `DATABASE_URL` at the project root. - An `output` directory for the generated Prisma Client as `app/generated/prisma`. @@ -103,7 +104,7 @@ In the `prisma/schema.prisma` file, add the following models: ```prisma file=prisma/schema.prisma generator client { - provider = "prisma-client-js" + provider = "prisma-client" output = "../app/generated/prisma" } @@ -133,14 +134,36 @@ model Post { This creates two models: `User` and `Post`, with a one-to-many relationship between them. -### 2.3. Configure the Prisma Client generator +### 2.3 Add `dotenv` to `prisma.config.ts` + +To get access to the variables in the `.env` file, they can either be loaded by your runtime, or by using `dotenv`. +Include an import for `dotenv` at the top of the `prisma.config.ts` + +```ts +//add-start +import 'dotenv/config' +//add-end +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations' + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); +``` + +### 2.4. Configure the Prisma Client generator Now, run the following command to create the database tables and generate the Prisma Client: ```terminal npx prisma migrate dev --name init ``` -### 2.4. Seed the database +### 2.5. Seed the database Add some seed data to populate the database with sample users and posts. @@ -193,45 +216,24 @@ export async function main() { main(); ``` -Now, tell Prisma how to run this script by updating your `package.json`: - -```json file=package.json -{ - "name": "nextjs-prisma", - "version": "0.1.0", - "private": true, - "scripts": { - "dev": "next dev --turbopack", - "build": "next build", - "start": "next start", - "lint": "next lint" +Now, tell Prisma how to run this script by updating your `prisma.config.ts`: + +```ts file=prisma.config.ts +import 'dotenv/config' +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + //add-start + seed: `tsx prisma/seed.ts`, + //add-end }, - // add-start - "prisma": { - "seed": "tsx prisma/seed.ts" + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), }, - // add-end - "dependencies": { - "@prisma/client": "^6.7.0", - "@prisma/extension-accelerate": "^1.3.0", - "next": "15.3.1", - "react": "^19.0.0", - "react-dom": "^19.0.0" - }, - "devDependencies": { - "@eslint/eslintrc": "^3", - "@tailwindcss/postcss": "^4", - "@types/node": "^20", - "@types/react": "^19", - "@types/react-dom": "^19", - "eslint": "^9", - "eslint-config-next": "15.3.1", - "prisma": "^6.7.0", - "tailwindcss": "^4", - "tsx": "^4.19.4", - "typescript": "^5" - } -} +}); ``` @@ -266,7 +268,7 @@ And open Prisma Studio to inspect your data: npx prisma studio ``` -### 2.5 Set up Prisma Client +### 2.6 Set up Prisma Client Now that you have a database with some initial data, you can set up Prisma Client and connect it to your database. diff --git a/content/800-guides/160-tanstack-start.mdx b/content/800-guides/160-tanstack-start.mdx index f65cd8dda4..971908e4d4 100644 --- a/content/800-guides/160-tanstack-start.mdx +++ b/content/800-guides/160-tanstack-start.mdx @@ -310,13 +310,13 @@ To get started with Prisma, you'll need to install a few dependencies: ```terminal npm install prisma tsx --save-dev -npm install @prisma/extension-accelerate @prisma/client +npm install @prisma/extension-accelerate @prisma/client dotenv ``` ```terminal npm install prisma tsx --save-dev -npm install @prisma/client +npm install @prisma/client dotenv ``` @@ -334,6 +334,7 @@ You'll need to answer a few questions while setting up your Prisma Postgres data This will create: - A `prisma` directory with a `schema.prisma` file. +- A `prisma.config.ts` file for configuring Prisma - A Prisma Postgres database. - A `.env` file containing the `DATABASE_URL` at the project root. - An `output` directory for the generated Prisma Client as `app/generated/prisma`. @@ -344,7 +345,6 @@ In `schema.prisma`, create a model for our posts and change the generator to use ```prisma file=prisma/schema.prisma generator client { - //edit-next-line provider = "prisma-client" output = "../app/generated/prisma" } @@ -375,27 +375,46 @@ model Post { This creates two models: `User` and `Post`, with a one-to-many relationship between them. -### 2.3. Configure the Prisma Client generator +### 2.3 Add `dotenv` to `prisma.config.ts` + +To get access to the variables in the `.env` file, they can either be loaded by your runtime, or by using `dotenv`. +Include an import for `dotenv` at the top of the `prisma.config.ts` + +```ts +//add-start +import 'dotenv/config' +//add-end +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); +``` + +### 2.4. Configure the Prisma Client generator Now, run the following command to create the database tables and generate the Prisma Client: ```terminal npx prisma migrate dev --name init ``` -### 2.4. Seed the database +### 2.5. Seed the database Let's add some seed data to populate the database with sample users and posts. Create a new file called `seed.ts` in the `prisma/` directory: ```typescript file=prisma/seed.ts -//add-next-line -import { PrismaClient, Prisma } from "../src/generated/prisma/client.js"; +import { PrismaClient, Prisma } from "../app/generated/prisma/client.js"; -//add-next-line const prisma = new PrismaClient(); -//add-start const userData: Prisma.UserCreateInput[] = [ { name: "Alice", @@ -428,28 +447,32 @@ const userData: Prisma.UserCreateInput[] = [ }, }, ]; -//add-end - -//add-start export async function main() { for (const u of userData) { await prisma.user.create({ data: u }); } } -//add-end - -//add-next-line main(); ``` -Now, tell Prisma how to run this script by updating your `package.json`: +Now, tell Prisma how to run this script by updating your `prisma.config.ts`: -```json file=package.json +```ts file=prisma.config.ts +import 'dotenv/config' +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', //add-start -"prisma": { - "seed": "tsx prisma/seed.ts" -} + seed: `tsx prisma/seed.ts`, //add-end + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); ``` Run the seed script: diff --git a/content/800-guides/170-react-router-7.mdx b/content/800-guides/170-react-router-7.mdx index c2e2bfedda..6cd1474854 100644 --- a/content/800-guides/170-react-router-7.mdx +++ b/content/800-guides/170-react-router-7.mdx @@ -50,13 +50,13 @@ To get started with Prisma, you'll need to install a few dependencies: ```terminal npm install prisma tsx --save-dev -npm install @prisma/extension-accelerate @prisma/client +npm install @prisma/extension-accelerate @prisma/client dotenv ``` ```terminal npm install prisma tsx --save-dev -npm install @prisma/client +npm install @prisma/client dotenv ``` @@ -73,6 +73,7 @@ You'll need to answer a few questions while setting up your Prisma Postgres data This will create: - A `prisma` directory with a `schema.prisma` file. +- A `prisma.config.ts` file for configuring Prisma - A Prisma Postgres database. - A `.env` file containing the `DATABASE_URL` at the project root. - An `output` directory for the generated Prisma Client as `app/generated/prisma`. @@ -83,7 +84,6 @@ In the `prisma/schema.prisma` file, add the following models and change the gene ```prisma file=prisma/schema.prisma generator client { - //edit-next-line provider = "prisma-client" output = "../app/generated/prisma" } @@ -114,14 +114,36 @@ model Post { This creates two models: `User` and `Post`, with a one-to-many relationship between them. -### 2.3. Configure the Prisma Client generator +### 2.3 Add `dotenv` to `prisma.config.ts` + +To get access to the variables in the `.env` file, they can either be loaded by your runtime, or by using `dotenv`. +Include an import for `dotenv` at the top of the `prisma.config.ts` + +```ts +//add-start +import 'dotenv/config' +//add-end +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); +``` + +### 2.4. Configure the Prisma Client generator Now, run the following command to create the database tables and generate the Prisma Client: ```terminal npx prisma migrate dev --name init ``` -### 2.4. Seed the database +### 2.5. Seed the database Add some seed data to populate the database with sample users and posts. @@ -174,47 +196,24 @@ export async function main() { main(); ``` -Now, tell Prisma how to run this script by updating your `package.json`: - -```json file=package.json -{ - "name": "react-router-7-prisma", - "private": true, - "type": "module", - "scripts": { - "build": "react-router build", - "dev": "react-router dev", - "start": "react-router-serve ./build/server/index.js", - "typecheck": "react-router typegen && tsc" - }, - //add-start - "prisma": { - "seed": "tsx prisma/seed.ts" +Now, tell Prisma how to run this script by updating your `prisma.config.ts`: + +```ts file=prisma.config.ts +import 'dotenv/config' +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', +//add-start + seed: `tsx prisma/seed.ts`, +//add-end }, - //add-end - "dependencies": { - "@react-router/node": "^7.3.0", - "@react-router/serve": "^7.3.0", - "isbot": "^5.1.17", - "react": "^19.0.0", - "react-dom": "^19.0.0", - "react-router": "^7.3.0" + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), }, - "devDependencies": { - "@react-router/dev": "^7.3.0", - "@tailwindcss/vite": "^4.0.0", - "@types/node": "^20", - "@types/react": "^19.0.1", - "@types/react-dom": "^19.0.1", - "prisma": "^6.5.0", - "react-router-devtools": "^1.1.0", - "tailwindcss": "^4.0.0", - "tsx": "^4.19.3", - "typescript": "^5.7.2", - "vite": "^5.4.11", - "vite-tsconfig-paths": "^5.1.4" - } -} +}); ``` Run the seed script: diff --git a/content/800-guides/180-solid-start.mdx b/content/800-guides/180-solid-start.mdx index bb077ffe0c..705eaf4a58 100644 --- a/content/800-guides/180-solid-start.mdx +++ b/content/800-guides/180-solid-start.mdx @@ -70,13 +70,13 @@ To get started with Prisma, you'll need to install a few dependencies: ```terminal npm install prisma tsx --save-dev -npm install @prisma/extension-accelerate @prisma/client +npm install @prisma/extension-accelerate @prisma/client dotenv ``` ```terminal npm install prisma tsx --save-dev -npm install @prisma/client +npm install @prisma/client dotenv ``` @@ -93,6 +93,7 @@ You'll need to answer a few questions while setting up your Prisma Postgres data This will create: - A `prisma` directory with a `schema.prisma` file. +- A `prisma.config.ts` file for configuring Prisma - A Prisma Postgres database. - A `.env` file containing the `DATABASE_URL` at the project root. - An `output` directory for the generated Prisma Client as `src/generated/prisma`. @@ -103,7 +104,6 @@ In the `prisma/schema.prisma` file, add the following models and change the gene ```prisma file=prisma/schema.prisma generator client { - //edit-next-line provider = "prisma-client" output = "../src/generated/prisma" } @@ -134,14 +134,37 @@ model Post { This creates two models: `User` and `Post`, with a one-to-many relationship between them. -### 2.3. Configure the Prisma Client generator +### 2.3 Add `dotenv` to `prisma.config.ts` + +To get access to the variables in the `.env` file, they can either be loaded by your runtime, or by using `dotenv`. +Include an import for `dotenv` at the top of the `prisma.config.ts` + +```ts +//add-start +import 'dotenv/config' +//add-end +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); +``` + + +### 2.4. Configure the Prisma Client generator Now, run the following command to create the database tables and generate the Prisma Client: ```terminal npx prisma migrate dev --name init ``` -### 2.4. Seed the database +### 2.5. Seed the database Let's add some seed data to populate the database with sample users and posts. @@ -194,38 +217,24 @@ export async function main() { main(); ``` -Now, tell Prisma how to run this script by updating your `package.json`: +Now, tell Prisma how to run this script by updating your `prisma.config.ts`: -```json file=package.json -{ - "name": "prisma-solid", - "type": "module", - "scripts": { - "dev": "vinxi dev", - "build": "vinxi build", - "start": "vinxi start" - }, - //add-start - "prisma": { - "seed": "tsx prisma/seed.ts" - } - //add-end - "dependencies": { - "@prisma/client": "^6.5.0", - "@prisma/extension-accelerate": "^1.3.0", - "@solidjs/start": "^1.1.0", - "solid-js": "^1.9.5", - "vinxi": "^0.5.3" +```ts file=prisma.config.ts +import 'dotenv/config' +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', +//add-start + seed: `tsx prisma/seed.ts`, +//add-end }, - "engines": { - "node": ">=22" + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), }, - "devDependencies": { - "@types/node": "^22.13.11", - "prisma": "^6.5.0", - "tsx": "^4.19.3" - } -} +}); ``` Run the seed script: diff --git a/content/800-guides/190-sveltekit.mdx b/content/800-guides/190-sveltekit.mdx index f729ac53a9..9031029e71 100644 --- a/content/800-guides/190-sveltekit.mdx +++ b/content/800-guides/190-sveltekit.mdx @@ -64,13 +64,13 @@ To get started with Prisma, you'll need to install a few dependencies: ```terminal npm install prisma tsx --save-dev -npm install @prisma/extension-accelerate @prisma/client +npm install @prisma/extension-accelerate @prisma/client dotenv ``` ```terminal npm install prisma tsx --save-dev -npm install @prisma/client +npm install @prisma/client dotenv ``` @@ -87,6 +87,7 @@ You'll need to answer a few questions while setting up your Prisma Postgres data This will create: - A `prisma` directory with a `schema.prisma` file. +- A `prisma.config.ts` file for configuring Prisma - A Prisma Postgres database. - A `.env` file containing the `DATABASE_URL` at the project root. - An `output` directory for the generated Prisma Client as `src/generated/prisma`. @@ -97,7 +98,6 @@ In the `prisma/schema.prisma` file, add the following models and change the gene ```prisma file=prisma/schema.prisma generator client { - //edit-next-line provider = "prisma-client" output = "../src/generated/prisma" } @@ -128,14 +128,36 @@ model Post { This creates two models: `User` and `Post`, with a one-to-many relationship between them. -### 2.3. Configure the Prisma Client generator +### 2.3 Add `dotenv` to `prisma.config.ts` + +To get access to the variables in the `.env` file, they can either be loaded by your runtime, or by using `dotenv`. +Include an import for `dotenv` at the top of the `prisma.config.ts` + +```ts +//add-start +import 'dotenv/config' +//add-end +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); +``` + +### 2.4. Configure the Prisma Client generator Now, run the following command to create the database tables and generate the Prisma Client: ```terminal npx prisma migrate dev --name init ``` -### 2.4. Seed the database +### 2.5. Seed the database Add some seed data to populate the database with sample users and posts. @@ -188,29 +210,24 @@ export async function main() { main(); ``` -Now, tell Prisma how to run this script by updating your `package.json`: - -```json file=package.json -{ - "name": "sveltekit-prisma", - "private": true, - "version": "0.0.1", - "type": "module", - "scripts": { - // ... - }, - //add-start - "prisma": { - "seed": "tsx prisma/seed.ts" - } - //add-end - "devDependencies": { - // ... - }, - "dependencies": { - // ... - } -} +Now, tell Prisma how to run this script by updating your `prisma.config.ts`: + +```ts file=prisma.config.ts +import 'dotenv/config' +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', +//add-start + seed: `tsx prisma/seed.ts`, +//add-end + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); ``` Run the seed script: diff --git a/content/800-guides/220-astro.mdx b/content/800-guides/220-astro.mdx index 47ee8e888c..ccd930ce0b 100644 --- a/content/800-guides/220-astro.mdx +++ b/content/800-guides/220-astro.mdx @@ -53,13 +53,13 @@ To get started with Prisma, you'll need to install a few dependencies: ```terminal npm install prisma tsx --save-dev -npm install @prisma/extension-accelerate @prisma/client +npm install @prisma/extension-accelerate @prisma/client dotenv ``` ```terminal npm install prisma tsx --save-dev -npm install @prisma/client +npm install @prisma/client dotenv ``` @@ -75,6 +75,7 @@ You'll need to answer a few questions while setting up your Prisma Postgres data This will create: - A `prisma/` directory with a `schema.prisma` file +- A `prisma.config.ts` file for configuring Prisma - A `.env` file with a `DATABASE_URL` already set ### 2.2. Define your Prisma Schema @@ -84,7 +85,6 @@ In the `prisma/schema.prisma` file, add the following models and change the gene ```prisma file=prisma/schema.prisma generator client { provider = "prisma-client" - engineType = "client" output = "../prisma/generated" } @@ -114,14 +114,37 @@ model Post { This creates two models: `User` and `Post`, with a one-to-many relationship between them. -### 2.3. Configure the Prisma Client generator + +### 2.3 Add `dotenv` to `prisma.config.ts` + +To get access to the variables in the `.env` file, they can either be loaded by your runtime, or by using `dotenv`. +Include an import for `dotenv` at the top of the `prisma.config.ts` + +```ts +//add-start +import 'dotenv/config' +//add-end +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); +``` + +### 2.4. Configure the Prisma Client generator Now, run the following command to create the database tables and generate the Prisma Client: ```terminal npx prisma migrate dev --name init ``` -### 2.4. Seed the database +### 2.5. Seed the database Let's add some seed data to populate the database with sample users and posts. @@ -189,34 +212,22 @@ main() }); ``` -Now, tell Prisma how to run this script by updating your `package.json`: - -```json file=package.json -{ - "name": "astro-prisma", - "type": "module", - "version": "0.0.1", - "scripts": { - "dev": "astro dev", - "build": "astro build", - "preview": "astro preview", - "astro": "astro" - }, - //add-start - "prisma": { - "seed": "tsx prisma/seed.ts" +Now, tell Prisma how to run this script by updating your `prisma.config.ts`: + +```ts file=prisma.config.ts +import 'dotenv/config' +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + seed: `tsx prisma/seed.ts`, }, - //add-end - "dependencies": { - "@prisma/client": "^6.17.1", - "@prisma/extension-accelerate": "^2.0.2", - "astro": "^5.14.4" + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), }, - "devDependencies": { - "prisma": "^6.17.1", - "tsx": "^4.20.6" - } -} +}); ``` Run the seed script: diff --git a/content/800-guides/320-permit-io-access-control.mdx b/content/800-guides/320-permit-io-access-control.mdx index fb482a2850..39e3e7f688 100644 --- a/content/800-guides/320-permit-io-access-control.mdx +++ b/content/800-guides/320-permit-io-access-control.mdx @@ -57,7 +57,11 @@ Then, initialize your Prisma setup: npx prisma init ``` -This creates a `prisma/` directory with a default `schema.prisma` file and a `.env` file at the root. +This creates: + +- A `prisma/` directory with a default `schema.prisma` file +- A `prisma.config.ts` file for configuring Prisma +- A `.env` file at the root. ### 1.3 Set up your TypeScript config @@ -165,7 +169,7 @@ Open `prisma/schema.prisma` and replace the contents with the following: ```prisma file=prisma/schema.prisma generator client { - provider = "prisma-client-js" + provider = "prisma-client" } datasource db { @@ -192,7 +196,30 @@ model Task { } ``` -### 3.2 Run your first migration +### 3.2 Add `dotenv` to `prisma.config.ts` + +To get access to the variables in the `.env` file, they can either be loaded by your runtime, or by using `dotenv`. +Include an import for `dotenv` at the top of the `prisma.config.ts` + +```ts +//add-start +import 'dotenv/config' +//add-end +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); +``` + + +### 3.3 Run your first migration To create the database schema: @@ -225,7 +252,7 @@ To test your data filtering logic, you'll create two projects, each with its own Create a new file at `scripts/seed.ts` and add the following: ```ts -import { PrismaClient } from '@prisma/client'; +import { PrismaClient } from '../src/generated/prisma/client'; const prisma = new PrismaClient(); @@ -310,7 +337,6 @@ npx tsx scripts/seed.ts If successful, you'll see: ✅ Seeded 2 projects and 4 tasks with distinct ownership -``` At this point, if you run a query like `prisma.task.findMany()`, it will return all tasks. In the next steps, you'll connect Permit.io to filter these results automatically based on the user's access rights. @@ -428,7 +454,7 @@ Create a new file: `src/middleware/auth.middleware.ts` ```ts import { Request, Response, NextFunction } from 'express'; -import { PrismaClient } from '@prisma/client'; +import { PrismaClient } from '../generated/prisma/client.js'; import createPermitClientExtension from '@permitio/permit-prisma'; import { clientExtensionConfig } from '../config/permit-config'; @@ -663,4 +689,4 @@ Explore [Permit Elements](https://docs.permit.io/embeddable-uis/overview/) to ma - [Permit.io ReBAC Policy Setup Guide](https://docs.permit.io/overview/create-a-rebac-policy) - [Read the full guide: Data Filtering with Prisma and ReBAC](https://docs.permit.io/how-to/enforce-permissions/data-filtering) -- [ReBAC vs RBAC - Learn when to use which](https://www.permit.io/blog/rbac-vs-rebac) +- [ReBAC vs RBAC - Learn when to use which](https://www.permit.io/blog/rbac-vs-rebac) \ No newline at end of file diff --git a/content/800-guides/340-ai-sdk-nextjs.mdx b/content/800-guides/340-ai-sdk-nextjs.mdx index 2654ed960f..d59d18004d 100644 --- a/content/800-guides/340-ai-sdk-nextjs.mdx +++ b/content/800-guides/340-ai-sdk-nextjs.mdx @@ -56,13 +56,13 @@ To get started with Prisma, you'll need to install a few dependencies: ```terminal npm install prisma tsx --save-dev -npm install @prisma/extension-accelerate @prisma/client +npm install @prisma/extension-accelerate @prisma/client dotenv ``` ```terminal npm install prisma tsx --save-dev -npm install @prisma/client +npm install @prisma/client dotenv ``` @@ -79,6 +79,7 @@ You'll need to answer a few questions while setting up your Prisma Postgres data This will create: - A `prisma` directory with a `schema.prisma` file. +- A `prisma.config.ts` file for configuring Prisma - A Prisma Postgres database. - A `.env` file containing the `DATABASE_URL` at the project root. - The `output` field specifies where the generated Prisma Client will be stored. @@ -124,7 +125,29 @@ enum MessageRole { This creates three models: `Session`, `Message`, and `MessageRole`. -### 2.3. Configure the Prisma Client generator +### 2.3 Add `dotenv` to `prisma.config.ts` + +To get access to the variables in the `.env` file, they can either be loaded by your runtime, or by using `dotenv`. +Include an import for `dotenv` at the top of the `prisma.config.ts` + +```ts +//add-start +import 'dotenv/config' +//add-end +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); +``` + +### 2.4. Configure the Prisma Client generator Now, run the following command to create the database tables and generate the Prisma Client: diff --git a/content/800-guides/350-authjs-nextjs.mdx b/content/800-guides/350-authjs-nextjs.mdx index 1e1051d8af..2bf14e647b 100644 --- a/content/800-guides/350-authjs-nextjs.mdx +++ b/content/800-guides/350-authjs-nextjs.mdx @@ -57,13 +57,13 @@ To get started with Prisma, you'll need to install a few dependencies: ```terminal npm install prisma tsx --save-dev -npm install @prisma/extension-accelerate @prisma/client +npm install @prisma/extension-accelerate @prisma/client dotenv ``` ```terminal npm install prisma tsx --save-dev -npm install @prisma/client +npm install @prisma/client dotenv ``` @@ -81,6 +81,7 @@ You'll need to answer a few questions while setting up your Prisma Postgres data This will create: - A `prisma` directory with a `schema.prisma` file. +- A `prisma.config.ts` file for configuring Prisma - A Prisma Postgres database. - A `.env` file containing the `DATABASE_URL` at the project root. - A schema configuration that specifies where the Prisma Client will be generated (`../app/generated/prisma`). @@ -91,7 +92,6 @@ In the `prisma/schema.prisma` file, swap the provider to `prisma-client` and add ```prisma file=prisma/schema.prisma generator client { - //edit-next-line provider = "prisma-client" output = "../app/generated/prisma" //add-next-line @@ -171,7 +171,30 @@ This creates the following models: - **`VerificationToken`**: Stores temporary tokens for email verification, password reset, and other security operations with expiration times. -### 2.3. Configure the Prisma Client generator + +### 2.3 Add `dotenv` to `prisma.config.ts` + +To get access to the variables in the `.env` file, they can either be loaded by your runtime, or by using `dotenv`. +Include an import for `dotenv` at the top of the `prisma.config.ts` + +```ts +//add-start +import 'dotenv/config' +//add-end +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); +``` + +### 2.4. Configure the Prisma Client generator Now, run the following command to create the database tables and generate the Prisma Client: @@ -179,7 +202,7 @@ Now, run the following command to create the database tables and generate the Pr npx prisma migrate dev --name init ``` -### 2.4 Create a Prisma Client +### 2.5 Create a Prisma Client Create a new folder in the root called `lib` and create a new file called `prisma.ts` in it. This file will contain the Prisma Client: diff --git a/content/800-guides/360-embed-studio-nextjs.mdx b/content/800-guides/360-embed-studio-nextjs.mdx index 60d436ef9a..2b117d1664 100644 --- a/content/800-guides/360-embed-studio-nextjs.mdx +++ b/content/800-guides/360-embed-studio-nextjs.mdx @@ -80,7 +80,7 @@ Install the required Prisma packages: ```terminal npm install prisma tsx --save-dev -npm install @prisma/extension-accelerate @prisma/client +npm install @prisma/extension-accelerate @prisma/client dotenv ``` ### 2.2. Initialize Prisma with Prisma Postgres @@ -100,6 +100,7 @@ You'll need to answer a few questions while setting up your Prisma Postgres data The `prisma init --db` command creates: - A `prisma/` directory with your `schema.prisma` file +- A `prisma.config.ts` file for configuring Prisma - A new Prisma Postgres database - A `.env` file with your `DATABASE_URL` - An output directory at `app/generated/prisma` for the Prisma Client @@ -110,7 +111,7 @@ Open `prisma/schema.prisma` and replace the content with: ```prisma file=prisma/schema.prisma generator client { - provider = "prisma-client-js" + provider = "prisma-client" output = "../app/generated/prisma" } @@ -141,7 +142,30 @@ model Post { //add-end ``` -### 2.4. Apply the schema to your database +### 2.4 Add `dotenv` to `prisma.config.ts` + +To get access to the variables in the `.env` file, they can either be loaded by your runtime, or by using `dotenv`. +Include an import for `dotenv` at the top of the `prisma.config.ts` + +```ts +//add-start +import 'dotenv/config' +//add-end +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); +``` + + +### 2.5. Apply the schema to your database Generate the Prisma Client and apply the schema: @@ -151,7 +175,7 @@ npx prisma migrate dev --name init This creates the tables in your Prisma Postgres database and generates the Prisma Client. -### 2.5. Seed your database (optional) +### 2.6. Seed your database (optional) Create a seed file to add some sample data. Create a `seed.ts` file in the `prisma` folder and add the following code: @@ -208,23 +232,22 @@ main() }) ``` -Add a seed script to your `package.json`: +Add a seed script to your `prisma.config.ts`: -```json file=package.json -{ - "name": "nextjs-studio-embed", - "version": "0.1.0", - "private": true, - // add-start - "prisma": { - "seed": "tsx prisma/seed.ts" +```ts file=prisma.config.ts +import 'dotenv/config'; +import { defineConfig, env } from 'prisma/config'; +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + seed: `tsx prisma/seed.ts`, }, - // add-end - "scripts": { - // ... existing scripts - } - // ... rest of package.json -} + engine: 'classic', + datasource: { + url: env('DIRECT_URL'), + }, +}); ``` Run the seed script: @@ -303,6 +326,7 @@ Next, set up a backend endpoint that Prisma Studio can communicate with. This en To do this, create a new folder called `api` inside the `app` directory. Inside it, add a `studio` folder with a `route.ts` file. This file will handle all requests sent to `/api/studio` and act as the bridge between the Studio component in your frontend and the database in your backend: ```typescript file=app/api/studio/route.ts +import "dotenv/config" import { createPrismaPostgresHttpClient } from "@prisma/studio-core/data/ppg"; import { serializeError } from "@prisma/studio-core/data/bff"; @@ -471,4 +495,4 @@ At this point you have Prisma Studio running inside your Next.js application and By adding authentication, environment-specific settings, and styling, you move from a working demo to a secure and polished production setup. For more patterns and examples, see the [Prisma Studio Core demo repository](https://github.com/prisma/studio-core-demo), which includes an alternative implementation using Hono.js and React. If you prefer a guided walkthrough, watch the YouTube video: [**Use Prisma Studio in Your Own Applications -**](https://www.youtube.com/watch?v=Up5vG2YHPvc). +**](https://www.youtube.com/watch?v=Up5vG2YHPvc). \ No newline at end of file diff --git a/content/800-guides/370-bun.mdx b/content/800-guides/370-bun.mdx index dc44653d67..fd90ceac90 100644 --- a/content/800-guides/370-bun.mdx +++ b/content/800-guides/370-bun.mdx @@ -1,7 +1,7 @@ --- title: 'How to use Prisma in Bun' metaTitle: 'How to use Prisma ORM and Prisma Postgres with Bun' -description: 'Learn how to use Prisma ORM in a Bun application with driver adapters and Prisma Postgres' +description: 'Learn how to use Prisma ORM in a Bun application with Prisma Postgres' sidebar_label: 'Bun' image: '/img/guides/prisma-bun-cover-image.png' completion_time: '10 min' @@ -10,7 +10,7 @@ community_section: true ## Introduction -[Bun](https://bun.sh) is a fast JavaScript runtime that includes a bundler, test runner, and package manager. In this guide, you will set up a Bun project with Prisma ORM and a Prisma Postgres database. You will configure Prisma driver adapters, create a simple HTTP server, and build a Bun executable for deployment. +[Bun](https://bun.sh) is a fast JavaScript runtime that includes a bundler, test runner, and package manager. In this guide, you will set up a Bun project with Prisma ORM and a Prisma Postgres database. You will create a simple HTTP server and build a Bun executable for deployment. ## Prerequisites @@ -43,7 +43,7 @@ Install the required Prisma packages and other dependencies: ```terminal bun add -d prisma -bun add @prisma/client @prisma/adapter-pg dotenv +bun add @prisma/client ``` ### 2.2. Initialize Prisma ORM with Prisma Postgres @@ -64,15 +64,12 @@ This command creates: - A `prisma/` directory with your `schema.prisma` file - A new Prisma Postgres database +- A `prisma.config.ts` file - A `.env` file with your `DATABASE_URL` -### 2.3. Configure environment variables for driver adapters +### 2.3. Configure environment variables for direct connection -We are going to use the [`node-postgres` driver adapter](/orm/overview/databases/postgresql#using-the-node-postgres-driver) to perform queries to our database. - -When using the `node-postgres` driver adapter with Prisma Postgres, you need to add a `DIRECT_URL` environment variable. This provides a direct connection to your PostgreSQL database. - -To get your [direct connection string](/postgres/database/direct-connections#how-to-connect-to-prisma-postgres-via-direct-tcp): +We're going to use a direct connection string for connecting to Prisma Postgres. To get your [direct connection string](/postgres/database/direct-connections#how-to-connect-to-prisma-postgres-via-direct-tcp): 1. Navigate to your recently created Prisma Postgres project dashboard (e.g. "My Bun Project") 2. Click the **API Keys** tab in the project's sidebar @@ -80,32 +77,25 @@ To get your [direct connection string](/postgres/database/direct-connections#how 4. Provide a name for the API key and click **Create** 5. Copy the connection string starting with `postgres://` -Update your `.env` file to include both URLs: +Update your `.env` file to replace the `DATABASE_URL` with the new connection string: ```env file=.env +//delete-start DATABASE_URL="your_database_url_here" +//delete-end //add-start -DIRECT_URL="your_direct_connection_string_here" +DATABASE_URL="your_direct_connection_string_here" //add-end ``` ### 2.4. Update your Prisma schema -Open `prisma/schema.prisma` and update it to use driver adapters with Bun runtime: +Open `prisma/schema.prisma` and update it to include your data model: ```prisma file=prisma/schema.prisma generator client { - //delete-start - provider = "prisma-client-js" - //delete-end - //add-start provider = "prisma-client" - //add-end output = "../generated/prisma" - //add-start - previewFeatures = ["driverAdapters", "queryCompiler"] - runtime = "bun" - //add-end } datasource db { @@ -126,18 +116,12 @@ model User { ### 3.1. Create a database utility file -Create a `db.ts` file in your project root to configure `PrismaClient` with the `node-postgres` adapter: +Create a `db.ts` file in your project root to configure `PrismaClient`: ```typescript file=db.ts -import "dotenv/config"; import { PrismaClient } from "./generated/prisma/client"; -import { PrismaPg } from "@prisma/adapter-pg"; - -const connectionString = `${process.env.DIRECT_URL}`; - -const adapter = new PrismaPg({ connectionString }); -export const prisma = new PrismaClient({ adapter }); +export const prisma = new PrismaClient(); ``` ### 3.2. Create a seed script @@ -180,25 +164,25 @@ main() }); ``` -### 3.3. Create Prisma Config file to run the seed script - -Create a [`prisma.config.ts` file](/orm/reference/prisma-config-reference#migrationsseed) to configure Prisma's seed command: - -```terminal -touch prisma.config.ts -``` +### 3.3. Add the seed script to Prisma Config -Then add the following content to the file: +Add the following content to the file: ```typescript file=prisma.config.ts -import 'dotenv/config' -import { defineConfig } from 'prisma/config' - +import { defineConfig, env } from 'prisma/config'; export default defineConfig({ - migrations: { - seed: `bun run prisma/seed.ts`, + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + // add-start + seed: `bun run prisma/seed.ts` + //add-end }, -}) + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, +}); ``` ## 4. Generate Prisma client and run migrations @@ -206,7 +190,7 @@ export default defineConfig({ Generate the Prisma client and apply your schema to the database: ```terminal -bun prisma migrate dev --name init +bunx --bun prisma migrate dev --name init ``` This command: @@ -214,16 +198,10 @@ This command: - Creates the database tables based on your schema - Generates the Prisma client in the `generated/prisma` directory -Because you are using the `node-postgres` driver adapter, you will need to generate the `PrismaClient` again. The client automatically produced by `migrate dev` is optimized for Prisma Postgres, but the adapter requires a client built specifically for the driver: - -```terminal -bun prisma generate -``` - Run the seed script to populate your database: ```terminal -bun prisma db seed +bunx --bun prisma db seed ``` ## 5. Creating your Bun server @@ -323,8 +301,6 @@ Now that you have a Bun application connected to a Prisma Postgres database, you ### More info - [Bun Documentation](https://bun.sh/docs) -- [Prisma Driver Adapters](/orm/overview/databases/database-drivers) - [Prisma Config File](/orm/reference/prisma-config-reference) - [Prisma Client without the Rust engine](/orm/prisma-client/setup-and-configuration/no-rust-engine) - [Prisma Postgres](/postgres) - diff --git a/content/800-guides/390-hono.mdx b/content/800-guides/390-hono.mdx index ea45dc8e2b..40df95a5b9 100644 --- a/content/800-guides/390-hono.mdx +++ b/content/800-guides/390-hono.mdx @@ -65,6 +65,7 @@ You'll need to answer a few questions while setting up your Prisma Postgres data This will create: - A `prisma/` directory with a `schema.prisma` file +- A `prisma.config.ts` with your Prisma configuration - A `.env` file with a `DATABASE_URL` already set ### 2.2. Define your Prisma Schema @@ -73,9 +74,7 @@ In the `prisma/schema.prisma` file, add the following models and change the gene ```prisma file=prisma/schema.prisma generator client { - //edit-next-line provider = "prisma-client" - engineType = "client" output = "../src/generated/prisma" } @@ -105,6 +104,25 @@ model Post { This creates two models: `User` and `Post`, with a one-to-many relationship between them. +In `prisma.config.ts`, import `dotenv` at the top of the file + +```ts +import { defineConfig, env } from "prisma/config"; +// add-start +import "dotenv/config"; +// add-end +export default defineConfig({ + schema: "prisma/schema.prisma", + migrations: { + path: "prisma/migrations", + }, + engine: "classic", + datasource: { + url: env("DATABASE_URL"), + }, +}); +``` + ### 2.3. Configure the Prisma Client generator Now, run the following command to create the database tables and generate the Prisma Client: @@ -172,36 +190,24 @@ main() }); ``` -Now, tell Prisma how to run this script by updating your `package.json`: +Now, tell Prisma how to run this script by updating your `prisma.config.ts`: -```json file=package.json -{ - "name": "my-app", - "type": "module", - "scripts": { - "dev": "tsx watch src/index.ts", - "build": "tsc", - "start": "node dist/index.js" - }, - //add-start - "prisma": { - "seed": "tsx prisma/seed.ts" +```ts file=prisma.config.ts +import { defineConfig, env } from "prisma/config"; +import "dotenv/config"; +export default defineConfig({ + schema: "prisma/schema.prisma", + migrations: { + path: "prisma/migrations", +//add-start + seed: "tsx prisma/seed.ts" +//add-end }, - //add-end - "dependencies": { - "@hono/node-server": "^1.19.5", - "@prisma/client": "^6.16.3", - "@prisma/extension-accelerate": "^2.0.2", - "dotenv": "^17.2.3", - "hono": "^4.9.9" + engine: "classic", + datasource: { + url: env("DATABASE_URL"), }, - "devDependencies": { - "@types/node": "^20.11.17", - "prisma": "^6.16.3", - "tsx": "^4.20.6", - "typescript": "^5.8.3" - } -} +}); ``` Run the seed script: @@ -229,6 +235,7 @@ Inside of `/src`, create a `lib` directory and a `prisma.ts` file inside it. Thi import type { Context, Next } from 'hono'; import { PrismaClient } from '../generated/prisma/client.js'; import { withAccelerate } from '@prisma/extension-accelerate'; +import "dotenv/config"; function withPrisma(c: Context, next: Next) { if (!c.get('prisma')) { @@ -254,6 +261,7 @@ export default withPrisma; ```tsx file=src/lib/prisma.ts import type { Context, Next } from 'hono'; import { PrismaClient } from '../generated/prisma/client.js'; +import "dotenv/config"; const databaseUrl = process.env.DATABASE_URL; if (!databaseUrl) { @@ -281,21 +289,8 @@ If you choose not to use one, in long-lived environments (for example, a Node.js ### 3.2 Environment Variables & Types -By default, Hono does not load any environment variables from a `.env`. `dotenv` handles this and will be read that file and expose them via `process.env`. - -Edit the `src/index.ts` to import `dotenv` and call the `config` method on it. - -```ts file=src/index.ts -import { Hono } from 'hono'; -import { serve } from '@hono/node-server'; - -// Read .env and set variables to process.env -import * as dotenv from 'dotenv'; -dotenv.config(); -``` - -Next, Hono needs additional types to to know that the `withPrisma` middleware will set a `prisma` -key on the Hono Context +By default, Hono does not load any environment variables from a `.env`. `dotenv` handles this and will be read that file and expose them via `process.env`. Hono can get additional types to know that the `withPrisma` middleware will set a `prisma` +key on the Hono context ```ts file=src/index.ts import { Hono } from "hono"; @@ -303,9 +298,6 @@ import { serve } from "@hono/node-server"; // add-next-line import type { PrismaClient } from "./generated/prisma/client.js"; -import * as dotenv from "dotenv"; -dotenv.config(); - // add-start type ContextWithPrisma = { Variables: { @@ -332,8 +324,7 @@ serve( ); ``` -If using Cloudflare Workers, the environment variables will automatically be set to Hono's contenxt, -so `dotenv` can be skipped. +If using Cloudflare Workers, the environment variables will automatically be set to Hono's contenxt, so `dotenv` can be skipped. ### 3.3. Create A GET Route @@ -389,4 +380,4 @@ Now that you have a working Hono app connected to a Prisma Postgres database, yo ### More Info - [Prisma Documentation](/orm/overview/introduction) -- [Hono Documentation](https://hono.dev/docs/) +- [Hono Documentation](https://hono.dev/docs/) \ No newline at end of file