Files
relspecgo/pkg/writers/drizzle
warkanum 161ac317f0
Release / test (push) Successful in 1m45s
Release / release (push) Successful in 12m24s
Release / pkg-rpm (push) Successful in 2m3s
Release / pkg-deb (push) Successful in 2m17s
Release / pkg-aur (push) Successful in 2m47s
feat(dbml): support multiline table notes in DBML
* Add parsing for triple-quoted table notes in DBML
* Update writer to format multiline notes correctly
* Enhance tests for multiline table notes handling
2026-09-09 21:38:11 +02:00
..
2025-12-28 10:34:20 +02:00
2025-12-28 10:15:30 +02:00

Drizzle Writer

Generates TypeScript/JavaScript files with Drizzle ORM schema definitions from database schema information.

Overview

The Drizzle Writer converts RelSpec's internal database model representation into TypeScript source code with Drizzle ORM schema definitions, including tables, columns, relationships, and constraints.

Features

  • Generates Drizzle-compatible TypeScript schema
  • Supports PostgreSQL and MySQL schemas
  • Creates table definitions with proper column types
  • Generates relationship definitions
  • Handles constraints and indexes
  • Outputs formatted TypeScript code

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/drizzle"
)

func main() {
    options := &writers.WriterOptions{
        OutputPath: "schema.ts",
        Metadata: map[string]interface{}{
            "database_type": "postgresql", // or "mysql"
        },
    }

    writer := drizzle.NewWriter(options)
    err := writer.WriteDatabase(db)
    if err != nil {
        panic(err)
    }
}

CLI Examples

# Generate Drizzle schema from PostgreSQL database
relspec --input pgsql \
  --conn "postgres://localhost/mydb" \
  --output drizzle \
  --out-file schema.ts

# Convert GORM models to Drizzle
relspec --input gorm --in-file models.go --output drizzle --out-file schema.ts

# Convert JSON schema to Drizzle
relspec --input json --in-file schema.json --output drizzle --out-file db/schema.ts

Generated Code Example

import { pgTable, serial, varchar, text, timestamp, integer } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  username: varchar('username', { length: 50 }).notNull().unique(),
  email: varchar('email', { length: 100 }).notNull(),
  bio: text('bio'),
  createdAt: timestamp('created_at').notNull().defaultNow(),
});

export const posts = pgTable('posts', {
  id: serial('id').primaryKey(),
  userId: integer('user_id').notNull().references(() => users.id, { onDelete: 'cascade' }),
  title: varchar('title', { length: 200 }).notNull(),
  content: text('content'),
});

export const usersRelations = relations(users, ({ many }) => ({
  posts: many(posts),
}));

export const postsRelations = relations(posts, ({ one }) => ({
  user: one(users, {
    fields: [posts.userId],
    references: [users.id],
  }),
}));

Supported Column Types

PostgreSQL

  • serial, bigserial - Auto-increment integers
  • integer, bigint, smallint - Integer types
  • varchar, text - String types
  • boolean - Boolean
  • timestamp, date, time - Date/time types
  • json, jsonb - JSON types
  • uuid - UUID type

MySQL

  • int, bigint, smallint - Integer types
  • varchar, text - String types
  • boolean - Boolean
  • datetime, timestamp - Date/time types
  • json - JSON type

Notes

  • Table names and column names are preserved as-is
  • Relationships are generated as separate relation definitions
  • Constraint actions (CASCADE, etc.) are included in references
  • Schema names other than 'public' are supported
  • Output is formatted TypeScript code