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/03/04 11:26] (Version actuelle) – [6. Prisma Client] jcheron
Ligne 21: Ligne 21:
 ==== 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 46:
 Créer `tsconfig.json` : Créer `tsconfig.json` :
  
-<code json>+<sxh json>
 { {
   "compilerOptions": {   "compilerOptions": {
Ligne 57: Ligne 57:
   }   }
 } }
-</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 87:
 ----- -----
  
-==== 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 ====
 +
 +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 155:
   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 167:
 ==== 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 179:
 `src/prisma.ts` `src/prisma.ts`
  
-<code ts> +<sxh ts> 
-import { PrismaClient } from "@prisma/client"+import {PrismaClient} from "@prisma/client/extension";
  
 export const prisma = new PrismaClient() export const prisma = new PrismaClient()
-</code>+</sxh>
  
 ----- -----
Ligne 131: Ligne 191:
 `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 215:
   console.log("Server running on http://localhost:3000")   console.log("Server running on http://localhost:3000")
 }) })
-</code>+</sxh>
  
 ----- -----
Ligne 161: Ligne 221:
 ==== 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
  • web/api/prisma-fastify.1772585664.txt.gz
  • Dernière modification : il y a 32 heures
  • de jcheron