Prisma is a next-generation ORM that provides a comprehensive database toolkit including type-safe query builder, declarative migrations, and GUI database management.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Schema validation, formatting, and development utilities for maintaining clean, valid, and consistent Prisma schema files with comprehensive error checking.
Format Prisma schema files with consistent styling, proper indentation, and standardized structure.
/**
* Format Prisma schema files with consistent styling
* Applies standard formatting rules and validates syntax
*/
prisma format [options]
Options:
--schema <path> Schema file path (default: ./prisma/schema.prisma)
--check Check formatting without modifying files
--help/-h Show format command helpUsage Examples:
# Format default schema file
prisma format
# Format custom schema location
prisma format --schema ./custom/schema.prisma
# Check formatting without making changes
prisma format --check
# Format multiple schemas in CI/CD
find . -name "*.prisma" -exec prisma format --schema {} \;Formatting Rules Applied:
Before/After Example:
// Before formatting
model User{
id Int @id @default(autoincrement())
email String@unique
name String?
posts Post[]
}
// After formatting
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}Validate Prisma schema syntax, configuration, and database compatibility with comprehensive error reporting.
/**
* Validate Prisma schema syntax and configuration
* Checks schema validity, database compatibility, and configuration
*/
prisma validate [options]
Options:
--schema <path> Schema file path (default: ./prisma/schema.prisma)
--config <path> Configuration file path
--help/-h Show validate command helpUsage Examples:
# Validate default schema
prisma validate
# Validate custom schema location
prisma validate --schema ./custom/schema.prisma
# Validate with custom configuration
prisma validate --config ./prisma/config.json
# Validate in CI/CD pipeline
prisma validate && echo "Schema is valid"Validation Checks:
Check schema formatting without making modifications, useful for CI/CD and code review processes.
/**
* Check schema formatting without modifying files
* Returns exit code indicating whether formatting is needed
*/
prisma format --check [options]
Exit Codes:
0: Schema is properly formatted
1: Schema needs formatting or has syntax errorsCI/CD Integration:
# Check formatting in CI pipeline
if prisma format --check; then
echo "Schema formatting is valid"
else
echo "Schema needs formatting" >&2
exit 1
fiUsage in Pre-commit Hooks:
# .git/hooks/pre-commit
#!/bin/bash
prisma format --check || {
echo "Please run 'prisma format' before committing"
exit 1
}# Development cycle with formatting
# 1. Modify schema
vim prisma/schema.prisma
# 2. Format schema
prisma format
# 3. Validate schema
prisma validate
# 4. Generate client
prisma generate# Before committing changes
prisma format # Format schema
prisma validate # Validate schema
git add prisma/schema.prisma # Stage formatted schema
git commit -m "Update schema" # Commit changes# Format multiple schema files
prisma format --schema ./apps/api/prisma/schema.prisma
prisma format --schema ./apps/admin/prisma/schema.prisma
# Validate all schemas
find . -name "schema.prisma" -exec prisma validate --schema {} \;// Well-organized schema structure
generator client {
provider = "prisma-client-js"
output = "../generated/client"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
// Enums first
enum UserRole {
ADMIN
USER
MODERATOR
}
// Base models
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
role UserRole @default(USER)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
posts Post[]
profile Profile?
@@map("users")
}
// Related models
model Profile {
id Int @id @default(autoincrement())
bio String?
avatar String?
userId Int @unique
user User @relation(fields: [userId], references: [id])
@@map("profiles")
}// Valid schema patterns
model Product {
id Int @id @default(autoincrement())
name String @db.VarChar(255)
price Decimal @db.Decimal(10, 2)
inStock Boolean @default(true)
categoryId Int?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Proper relation definition
category Category? @relation(fields: [categoryId], references: [id])
orderItems OrderItem[]
// Proper indexes
@@index([categoryId])
@@index([createdAt])
@@map("products")
}// Valid generator configuration
generator client {
provider = "prisma-client-js"
output = "./generated/client"
previewFeatures = ["jsonProtocol", "metrics"]
binaryTargets = ["native", "linux-musl"]
}
// Valid datasource configuration
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
relationMode = "foreignKeys"
}Syntax Errors:
$ prisma validate
Error: Schema parsing failed:
--> schema.prisma:15:3
|
15 | posts Post[]
| ^^^^^ Type "Post" referenced but not definedType Errors:
Error: Relation field "posts" must specify both `fields` and `references`
or be a list and have a back-relation field with both `fields` and `references`.Database Compatibility:
Error: The preview feature "extendedWhereUnique" is not supported
with the current database provider "sqlite".Formatting Check Output:
$ prisma format --check
The following files are not formatted:
- prisma/schema.prisma
Run 'prisma format' to fix formatting issues.Auto-fixing Common Issues:
# Fix formatting automatically
prisma format
# Validate after formatting
prisma validate// VS Code settings.json
{
"prisma.format.enable": true,
"editor.formatOnSave": true,
"[prisma]": {
"editor.defaultFormatter": "Prisma.prisma"
}
}# .husky/pre-commit
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
# Format and validate schema
prisma format
prisma validate
# Add formatted schema to commit
git add prisma/schema.prisma# GitHub Actions workflow
name: Schema Validation
on: [push, pull_request]
jobs:
validate-schema:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18
- name: Install dependencies
run: npm ci
- name: Check schema formatting
run: prisma format --check
- name: Validate schema
run: prisma validate# Dockerfile with schema validation
FROM node:18-alpine
COPY . .
RUN npm install
# Validate schema during build
RUN npx prisma validate
# Format schema and build
RUN npx prisma format
RUN npx prisma generate// VS Code extension configuration
{
"prisma.format.enable": true,
"editor.codeActionsOnSave": {
"source.format.prisma": true
}
}# Script for batch schema management
#!/bin/bash
for schema in $(find . -name "*.prisma"); do
echo "Processing $schema"
prisma format --schema "$schema"
prisma validate --schema "$schema"
done# Quality gate script
#!/bin/bash
set -e
echo "Checking schema formatting..."
prisma format --check || {
echo "Schema formatting check failed"
exit 1
}
echo "Validating schema..."
prisma validate || {
echo "Schema validation failed"
exit 1
}
echo "All schema checks passed!"prisma formatInstall with Tessl CLI
npx tessl i tessl/npm-prisma