104 lines
2.2 KiB
Markdown
104 lines
2.2 KiB
Markdown
# Prisma Reader
|
|
|
|
Reads Prisma schema files and extracts database schema information.
|
|
|
|
## Overview
|
|
|
|
The Prisma Reader parses `.prisma` schema files that define database models using Prisma's schema language and converts them into RelSpec's internal database model representation.
|
|
|
|
## Features
|
|
|
|
- Parses Prisma schema syntax
|
|
- Extracts models, fields, and relationships
|
|
- Supports Prisma attributes and directives
|
|
- Handles enums and composite types
|
|
|
|
## Usage
|
|
|
|
### Basic Example
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"git.warky.dev/wdevs/relspecgo/pkg/readers"
|
|
"git.warky.dev/wdevs/relspecgo/pkg/readers/prisma"
|
|
)
|
|
|
|
func main() {
|
|
options := &readers.ReaderOptions{
|
|
FilePath: "/path/to/schema.prisma",
|
|
}
|
|
|
|
reader := prisma.NewReader(options)
|
|
db, err := reader.ReadDatabase()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Printf("Found %d schemas\n", len(db.Schemas))
|
|
}
|
|
```
|
|
|
|
### CLI Example
|
|
|
|
```bash
|
|
# Read Prisma schema and convert to JSON
|
|
relspec --input prisma --in-file schema.prisma --output json --out-file schema.json
|
|
|
|
# Convert Prisma to GORM models
|
|
relspec --input prisma --in-file schema.prisma --output gorm --out-file models.go
|
|
```
|
|
|
|
## Example Prisma Schema
|
|
|
|
```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)
|
|
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
|
|
|
|
- `@id` - Primary key
|
|
- `@unique` - Unique constraint
|
|
- `@default` - Default value
|
|
- `@map` - Column name mapping
|
|
- `@@map` - Table name mapping
|
|
- `@relation` - Relationship definition
|
|
- `@db.*` - Database-specific type annotations
|
|
|
|
## Notes
|
|
|
|
- Extracts datasource provider information
|
|
- Supports `@@map` for custom table names
|
|
- Handles Prisma-specific types and converts them to standard SQL types
|