-
Notifications
You must be signed in to change notification settings - Fork 4
Prisma
Francois edited this page Dec 17, 2025
·
1 revision
Presentation : Prisma is an object relational mapping tool for TypeScript.
Key advantages of an ORM are :
- app maintainability : decoupling between code and database
- ease of use : auto-generation from a schema of models with methods corresponding to basic SQL queries. Yet raw SQL queries are still usable.
- type safety
- maintaining migrations : versioning of database state
npm install prisma --save-dev
npm install @prisma/client @prisma/adapter-better-sqlite3 better-sqlite3- check TS config and package.json (module compatible)
-
schema.prisma: specifies path of generated client files, model and constraints for table -
prisma.config.ts: specifies paths of schema, migrations, database
import 'dotenv/config'
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3'
import { appenv } from '../config/env.js'
import { PrismaClient } from './generated/prisma/client.js' // Note the import from the generated folder
const connectionString = appenv.UM_DB_URL
const adapter = new PrismaBetterSqlite3({ url: connectionString })
export const prisma = new PrismaClient({ adapter })- modify model in
schema.prisma - run a migration with
npx prisma migrate dev --name <myname>(it will also executenpx prisma generatebegind the curtain to update client)
Tip
database can be visualized with npx prisma studio
Model attributes can have various contraints
-
@id: primary key @default(autoincrement())@unique-
?: nullable field
export async function findProfileByUsername(
username: string
): Promise<UserProfile | null> {
// .findUnique is optimized for @unique fields
return await prisma.userProfile.findUnique({
where: { username },
})
}| โ Do | โ Don't |
|---|---|
| Use a single instance | Instantiate new PrismaClient() in every file (causes connection issues). |
Use findUnique when searching by ID or Email. |
Use findFirst
|
| Update schema.prisma then run a migration. | Modify SQLite file manually |
Type function return values with generated types (e.g., Promise<UserProfile>). |
Use any for DB objects. |
Note
prisma functions are async, so we need to call them with await
Tip
models can be used to type functions return value, but also in tests
Important
current way of sending db is npx prisma db push in production stage of Dockerfile but we should use npx prisma migrate deploy in prod (no risk of data loss)
| Type | Resource | Notes |
|---|---|---|
| ๐ | Official Docs | Main reference |
| ๐ฅ | Prisma in 60 mn | Not watched |
| ๐ป | Prisma examples | Production examples - not checked |
Legend: ๐ Doc, ๐ Book, ๐ฅ Video, ๐ป GitHub, ๐ฆ Package, ๐ก Blog
- Gateway Service - API Gateway & JWT validation
- Auth Service - Authentication & 2FA/TOTP
- AI Service - AI opponent
- API Documentation - OpenAPI/Swagger
- DB Schema - Databases
- Fastify - Web framework
- Prisma - ORM
- WebSockets - Real-time communication
- Restful API - API standards
- React - UI library
- CSS - Styling
- Tailwind - CSS framework
- Accessibility - WCAG compliance
- TypeScript - Language
- Zod - Schema validation
- Nginx - Reverse proxy
- Logging and Error management - Observability
- OAuth 2.0 - Authentication flows
- Two-factor authentication - 2FA/TOTP
- Avalanche - Blockchain network
- Hardhat - Development framework
- Solidity - Smart contracts language
- Open Zeppelin - Security standards
- ESLint - Linting
- Vitest - Testing
- GitHub Actions - CI/CD
- Husky, Commit lints and git hooks - Git hooks
- ELK - Logging stack
๐ Page model