Files
relspecgo/pkg/writers/drizzle
warkanum 96281c9f03
Release / test (push) Failing after 2m20s
Release / release (push) Skipped
Release / pkg-aur (push) Skipped
Release / pkg-deb (push) Skipped
Release / pkg-rpm (push) Skipped
chore(ci): add govulncheck, staticcheck, go vet, gofumpt gates
* Add lint/format checks to the Gitea release workflow and Makefile
  (targets: vet, fmt, fmt-check, staticcheck, govulncheck, check)
* Switch .golangci.json formatter from gofmt to gofumpt (extra.group-params)
* Bump golang.org/x/text 0.37.0 -> 0.39.0 for GO-2026-5970; re-vendor
* Fix staticcheck S1011 in pkg/diff; drop unused pgsql writer helpers
* Fix gocritic unnamedResult (pkg/diff) and rangeValCopy (pkg/pgsql)
* Apply gofumpt + goimports formatting across the tree
2026-09-03 21:17:03 +02:00
..
2025-12-28 10:34:20 +02:00
2025-12-28 10:15:30 +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