web:api:prisma-fastify

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
web:api:prisma-fastify [2026/03/04 01:54] – créée jcheronweb:api:prisma-fastify [2026/05/04 14:17] (Version actuelle) – [6. Prisma Client] jcheron
Ligne 1: Ligne 1:
 ====== API REST avec Fastify + Prisma ====== ====== API REST avec Fastify + Prisma ======
 +====== Sommaire ======
 +  * [[web:api:prisma-fastify:p2|]]
 +  * [[web:api:prisma-fastify:p2-b|]]
 +  * [[web:api:prisma-fastify:p3|]]
 +  * [[web:api:prisma-fastify:p4|]]
 +  * [[web:api:prisma-fastify:p5|]]
 +  * [[web:api:prisma-fastify:p6|]]
  
 ===== Partie 1 ===== ===== Partie 1 =====
Ligne 21: Ligne 28:
 ==== 2. Initialisation du projet ==== ==== 2. Initialisation du projet ====
  
-<code bash>+<sxh bash;gutter:false>
 mkdir fastify-prisma-api mkdir fastify-prisma-api
 cd fastify-prisma-api cd fastify-prisma-api
 npm init -y npm init -y
-</code>+</sxh>
  
 Installation dépendances : Installation dépendances :
  
-<code bash>+<sxh bash;gutter:false>
 npm install fastify @prisma/client npm install fastify @prisma/client
 npm install -D prisma typescript ts-node-dev @types/node npm install -D prisma typescript ts-node-dev @types/node
-</code>+</sxh>
  
 Initialiser Prisma : Initialiser Prisma :
  
-<code bash>+<sxh bash;gutter:false>
 npx prisma init npx prisma init
-</code>+</sxh>
  
 ----- -----
Ligne 46: Ligne 53:
 Créer `tsconfig.json` : Créer `tsconfig.json` :
  
-<code json>+<sxh json>
 { {
   "compilerOptions": {   "compilerOptions": {
Ligne 57: Ligne 64:
   }   }
 } }
-</code>+</sxh>
  
 Ajouter dans `package.json` : Ajouter dans `package.json` :
  
-<code json>+<sxh json>
 "scripts": { "scripts": {
   "dev": "ts-node-dev --respawn src/server.ts"   "dev": "ts-node-dev --respawn src/server.ts"
 +}
 +</sxh>
 +
 +-----
 +
 +
 +==== 2. Configuration Prisma avec SQLite ====
 +
 +Modifier `prisma/schema.prisma` :
 +
 +<code prisma>
 +datasource db {
 +  provider = "sqlite"
 +  url      = env("DATABASE_URL")
 +}
 +
 +generator client {
 +  provider = "prisma-client-js"
 } }
 </code> </code>
Ligne 69: Ligne 94:
 ----- -----
  
-==== 4. Configuration Prisma ====+==== 3. Configuration du fichier .env ====
  
-Fichier `.env` :+<code> 
 +DATABASE_URL="file:./dev.db" 
 +</code> 
 + 
 +Explication : 
 + 
 +  * Prisma va créer automatiquement un fichier `dev.db` 
 +  * Le fichier sera situé à la racine du projet 
 + 
 +----- 
 + 
 +==== 4. Lancer la migration ==== 
 + 
 +<sxh bash> 
 +npx prisma migrate dev --name init 
 +</sxh> 
 + 
 +Résultat : 
 + 
 +  * Fichier dev.db créé 
 +  * Tables générées 
 +  * Client Prisma généré 
 + 
 +Aucune base externe nécessaire. 
 + 
 +----- 
 + 
 +==== 4.a Structure projet avec SQLite ====
  
 <code> <code>
-DATABASE_URL="postgresql://user:password@localhost:5432/fastifydb"+fastify-prisma-api/ 
 + ├── prisma/ 
 + ├── src/ 
 + ├── dev.db 
 + ├── .env 
 + ├── package.json
 </code> </code>
 +
 +-----
 +
 +==== 4.b Configuration Prisma avec postgres ====
 +
 +<sxh bash>
 +npm install @prisma/adapter-pg pg
 +npm install -D @types/pg
 +</sxh>
 +
 +Fichier `.env` :
 +
 +<sxh prisma;gutter:false>
 +DATABASE_URL="postgresql://user:password@localhost:5432/fastifydb"
 +</sxh>
  
 Modifier `prisma/schema.prisma` : Modifier `prisma/schema.prisma` :
  
-<code prisma>+<sxh ts>
 datasource db { datasource db {
   provider = "postgresql"   provider = "postgresql"
Ligne 95: Ligne 167:
   createdAt DateTime @default(now())   createdAt DateTime @default(now())
 } }
-</code>+</sxh>
  
 Créer la base : Créer la base :
  
-<code bash>+<sxh bash;gutter:false>
 npx prisma migrate dev --name init npx prisma migrate dev --name init
-</code>+</sxh>
  
 ----- -----
Ligne 107: Ligne 179:
 ==== 5. Structure simple du projet ==== ==== 5. Structure simple du projet ====
  
-<code>+<sxh bash;gutter:false>
 src/ src/
  ├── server.ts  ├── server.ts
  └── prisma.ts  └── prisma.ts
-</code>+</sxh>
  
 ----- -----
Ligne 119: Ligne 191:
 `src/prisma.ts` `src/prisma.ts`
  
-<code ts> +<sxh ts> 
-import { PrismaClient } from "@prisma/client"+import "dotenv/config"; 
 +import { PrismaPg } from "@prisma/adapter-pg"; 
 +import { PrismaClient } from "../generated/prisma/client";
  
-export const prisma = new PrismaClient() +const connectionString = `${process.env.DATABASE_URL}`; 
-</code>+const adapter = new PrismaPg({ connectionString }); 
 +const prisma = new PrismaClient({ adapter })
 +export { prisma }; 
 +</sxh>
  
------ 
  
 ==== 7. Serveur + Routes simples ==== ==== 7. Serveur + Routes simples ====
Ligne 131: Ligne 207:
 `src/server.ts` `src/server.ts`
  
-<code ts>+<sxh ts>
 import Fastify from "fastify" import Fastify from "fastify"
 import { prisma } from "./prisma" import { prisma } from "./prisma"
Ligne 155: Ligne 231:
   console.log("Server running on http://localhost:3000")   console.log("Server running on http://localhost:3000")
 }) })
-</code>+</sxh>
  
 ----- -----
Ligne 161: Ligne 237:
 ==== 8. Lancement ==== ==== 8. Lancement ====
  
-<code bash>+<sxh bash;gutter:false>
 npm run dev npm run dev
-</code>+</sxh>
  
 Tester : Tester :
  
-GET  http://localhost:3000/posts   +  * GET  http://localhost:3000/posts   
-POST http://localhost:3000/posts+  POST http://localhost:3000/posts 
 + 
 + 
 +<button type="primary" size="sm">[[web:api:prisma-fastify:p2|Suite >>]]</button>
  • web/api/prisma-fastify.1772585664.txt.gz
  • Dernière modification : il y a 2 mois
  • de jcheron