prisma

package
v1.0.33 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

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
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
# 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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer implements the writers.Writer interface for Prisma schema format

func NewWriter

func NewWriter(options *writers.WriterOptions) *Writer

NewWriter creates a new Prisma writer with the given options

func (*Writer) WriteDatabase

func (w *Writer) WriteDatabase(db *models.Database) error

WriteDatabase writes a Database model to Prisma schema format

func (*Writer) WriteSchema

func (w *Writer) WriteSchema(schema *models.Schema) error

WriteSchema writes a Schema model to Prisma schema format

func (*Writer) WriteTable

func (w *Writer) WriteTable(table *models.Table) error

WriteTable writes a Table model to Prisma schema format

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL