Files
relspecgo/pkg/writers/drawdb
warkanum 3b88c386a1 fix(codegen): sort map iteration to make generated output deterministic
Table.Columns/Constraints/Indexes/Relationships are Go maps, and every
writer, reader, diff, inspector, and merge code path that iterated them
directly was subject to Go's randomized map order, so identical input
could produce different output (or a different in-report violation/diff
order) on every run. Most visibly this showed up as bun/gorm `unique:`
struct tags changing order across consecutive `make models` runs with no
source change.

Fixed by sorting map iteration (by Sequence then Name, or alphabetically
for string-keyed maps) everywhere the order affects generated output or
first-match tie-break logic, across the bun, gorm, sqlite, dbml, drawdb,
pgsql, prisma, graphql, typeorm, drizzle, and dctx writers; the dctx,
prisma, and typeorm readers; the shared models.GetPrimaryKey/
GetForeignKeys helpers; pkg/diff, pkg/inspector, and pkg/merge; and the
TUI column/relationship pickers in pkg/ui.
2026-08-10 20:54:40 +02:00
..
2025-12-28 10:34:20 +02:00
2025-12-16 18:10:40 +02:00
2025-12-17 22:52:24 +02:00

DrawDB Writer

Generates DrawDB-compatible JSON files from database schema information.

Overview

The DrawDB Writer converts RelSpec's internal database model representation into JSON format compatible with DrawDB, a free online database design tool.

Features

  • Generates DrawDB JSON format
  • Creates table and field definitions
  • Defines relationships
  • Includes visual layout information
  • Preserves constraints and indexes

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

func main() {
    options := &writers.WriterOptions{
        OutputPath: "diagram.json",
    }

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

CLI Examples

# Generate DrawDB diagram from PostgreSQL database
relspec --input pgsql \
  --conn "postgres://localhost/mydb" \
  --output drawdb \
  --out-file diagram.json

# Convert GORM models to DrawDB for visualization
relspec --input gorm --in-file models.go --output drawdb --out-file design.json

# Convert JSON schema to DrawDB
relspec --input json --in-file schema.json --output drawdb --out-file diagram.json

Generated JSON Example

{
  "version": "1.0",
  "database": "PostgreSQL",
  "tables": [
    {
      "id": "1",
      "name": "users",
      "x": 100,
      "y": 100,
      "fields": [
        {
          "id": "1",
          "name": "id",
          "type": "BIGINT",
          "primary": true,
          "autoIncrement": true,
          "notNull": true
        },
        {
          "id": "2",
          "name": "username",
          "type": "VARCHAR",
          "size": 50,
          "notNull": true,
          "unique": true
        },
        {
          "id": "3",
          "name": "email",
          "type": "VARCHAR",
          "size": 100,
          "notNull": true
        }
      ],
      "indexes": [
        {
          "name": "idx_users_email",
          "fields": ["email"]
        }
      ]
    },
    {
      "id": "2",
      "name": "posts",
      "x": 400,
      "y": 100,
      "fields": [
        {
          "id": "1",
          "name": "id",
          "type": "BIGINT",
          "primary": true
        },
        {
          "id": "2",
          "name": "user_id",
          "type": "BIGINT",
          "notNull": true
        },
        {
          "id": "3",
          "name": "title",
          "type": "VARCHAR",
          "size": 200,
          "notNull": true
        }
      ]
    }
  ],
  "relationships": [
    {
      "id": "1",
      "source": "2",
      "target": "1",
      "sourceField": "user_id",
      "targetField": "id",
      "type": "many-to-one",
      "onDelete": "CASCADE"
    }
  ]
}

DrawDB Features

Table Properties

  • id - Unique table identifier
  • name - Table name
  • x, y - Position in diagram
  • fields - Array of field definitions
  • indexes - Array of index definitions

Field Properties

  • id - Unique field identifier
  • name - Field name
  • type - Data type (BIGINT, VARCHAR, etc.)
  • size - Length for string types
  • primary - Primary key flag
  • notNull - NOT NULL constraint
  • unique - Unique constraint
  • autoIncrement - Auto-increment flag
  • default - Default value

Relationship Properties

  • id - Unique relationship identifier
  • source - Source table ID
  • target - Target table ID
  • sourceField - Foreign key field
  • targetField - Referenced field
  • type - Relationship type (one-to-one, one-to-many, many-to-one)
  • onDelete - Delete action
  • onUpdate - Update action

Notes

  • DrawDB is available at drawdb.vercel.app
  • Generated files can be imported for visual editing
  • Visual positions (x, y) are auto-generated
  • Ideal for creating ERD diagrams
  • Supports modern database features
  • Free and open-source tool