# Prisma Writer Generates Prisma schema files from database schema information. ## Overview The Prisma Writer converts RelSpec's internal database model representation into Prisma schema language (`.prisma` files), complete with models, fields, relationships, and attributes. ## Features - Generates Prisma schema syntax - Creates model definitions with proper field types - Adds Prisma attributes (@id, @unique, @default, etc.) - Generates relationship fields - Includes datasource and generator configurations - Maps table/column names with @map and @@map ## Usage ### Basic Example ```go package main import ( "git.warky.dev/wdevs/relspecgo/pkg/models" "git.warky.dev/wdevs/relspecgo/pkg/writers" "git.warky.dev/wdevs/relspecgo/pkg/writers/prisma" ) func main() { options := &writers.WriterOptions{ OutputPath: "schema.prisma", Metadata: map[string]interface{}{ "datasource_provider": "postgresql", }, } writer := prisma.NewWriter(options) err := writer.WriteDatabase(db) if err != nil { panic(err) } } ``` ### CLI Examples ```bash # Generate Prisma schema from PostgreSQL database relspec --input pgsql \ --conn "postgres://localhost/mydb" \ --output prisma \ --out-file schema.prisma # Convert GORM models to Prisma relspec --input gorm --in-file models.go --output prisma --out-file schema.prisma # Convert JSON to Prisma schema relspec --input json --in-file database.json --output prisma --out-file prisma/schema.prisma ``` ## Generated Code Example ```prisma datasource db { provider = "postgresql" url = env("DATABASE_URL") } generator client { provider = "prisma-client-js" } model User { id Int @id @default(autoincrement()) username String @unique @db.VarChar(50) email String @db.VarChar(100) bio String? @db.Text createdAt DateTime @default(now()) @map("created_at") posts Post[] @@map("users") } model Post { id Int @id @default(autoincrement()) userId Int @map("user_id") title String @db.VarChar(200) content String? @db.Text user User @relation(fields: [userId], references: [id], onDelete: Cascade) @@map("posts") } ``` ## Supported Prisma Attributes ### Field Attributes - `@id` - Primary key - `@unique` - Unique constraint - `@default()` - Default value - `@map()` - Column name mapping - `@db.*` - Database-specific types - `@relation()` - Relationship definition ### Model Attributes - `@@map()` - Table name mapping - `@@unique()` - Composite unique constraints - `@@index()` - Index definitions - `@@id()` - Composite primary keys ## Type Mapping | SQL Type | Prisma Type | Database Type | |----------|-------------|---------------| | bigint | Int | @db.BigInt | | integer | Int | - | | varchar(n) | String | @db.VarChar(n) | | text | String | @db.Text | | boolean | Boolean | - | | timestamp | DateTime | @db.Timestamp | | uuid | String | @db.Uuid | | json | Json | - | ## Notes - Model names are PascalCase (e.g., `User`, `Post`) - Field names are camelCase with `@map` for snake_case columns - Table names use `@@map` when different from model name - Nullable fields are marked with `?` - Relationship fields are automatically generated - Datasource provider defaults to `postgresql`