web:api:prisma-fastify

Différences

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

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
web:api:prisma-fastify [2026/03/04 10:45] 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 102: Ligne 109:
 ==== 4. Lancer la migration ==== ==== 4. Lancer la migration ====
  
-<code bash>+<sxh bash>
 npx prisma migrate dev --name init npx prisma migrate dev --name init
-</code>+</sxh>
  
 Résultat : Résultat :
  
-  * ✅ Fichier dev.db créé +  * Fichier dev.db créé 
-  * ✅ Tables générées +  * Tables générées 
-  * ✅ Client Prisma généré+  * Client Prisma généré
  
 Aucune base externe nécessaire. Aucune base externe nécessaire.
Ligne 130: Ligne 137:
  
 ==== 4.b Configuration Prisma avec postgres ==== ==== 4.b Configuration Prisma avec postgres ====
 +
 +<sxh bash>
 +npm install @prisma/adapter-pg pg
 +npm install -D @types/pg
 +</sxh>
  
 Fichier `.env` : Fichier `.env` :
Ligne 180: Ligne 192:
  
 <sxh 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}`; 
 +const adapter = new PrismaPg({ connectionString }); 
 +const prisma = new PrismaClient({ adapter })
 +export { prisma };
 </sxh> </sxh>
  
------ 
  
 ==== 7. Serveur + Routes simples ==== ==== 7. Serveur + Routes simples ====
Ligne 229: Ligne 245:
   * 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.1772617529.txt.gz
  • Dernière modification : il y a 2 mois
  • de jcheron