Files
relspecgo/pkg/readers/json
Hein 666eab7cec
Some checks failed
CI / Test (1.24) (push) Failing after -24m41s
CI / Test (1.25) (push) Failing after -24m25s
CI / Lint (push) Failing after -25m49s
CI / Build (push) Successful in -26m3s
Updated Readme files
2025-12-28 10:34:20 +02:00
..
2025-12-16 21:43:45 +02:00
2025-12-16 21:43:45 +02:00
2025-12-28 10:34:20 +02:00

JSON Reader

Reads database schema definitions from JSON files.

Overview

The JSON Reader parses JSON files that define database schemas in RelSpec's canonical JSON format and converts them into RelSpec's internal database model representation.

Features

  • Reads RelSpec's standard JSON schema format
  • Supports complete schema representation including:
    • Databases and schemas
    • Tables, columns, and data types
    • Constraints (PK, FK, unique, check)
    • Indexes
    • Relationships
    • Views and sequences

Usage

Basic Example

package main

import (
    "fmt"
    "git.warky.dev/wdevs/relspecgo/pkg/readers"
    "git.warky.dev/wdevs/relspecgo/pkg/readers/json"
)

func main() {
    options := &readers.ReaderOptions{
        FilePath: "/path/to/schema.json",
    }

    reader := json.NewReader(options)
    db, err := reader.ReadDatabase()
    if err != nil {
        panic(err)
    }

    fmt.Printf("Found %d schemas\n", len(db.Schemas))
}

CLI Example

# Read JSON schema and convert to GORM models
relspec --input json --in-file schema.json --output gorm --out-file models.go

# Convert JSON to PostgreSQL DDL
relspec --input json --in-file database.json --output pgsql --out-file schema.sql

# Transform JSON to YAML
relspec --input json --in-file schema.json --output yaml --out-file schema.yaml

Example JSON Schema

{
  "name": "myapp",
  "database_type": "postgresql",
  "schemas": [
    {
      "name": "public",
      "tables": [
        {
          "name": "users",
          "schema": "public",
          "columns": {
            "id": {
              "name": "id",
              "type": "bigint",
              "not_null": true,
              "is_primary_key": true,
              "auto_increment": true,
              "sequence": 1
            },
            "username": {
              "name": "username",
              "type": "varchar",
              "length": 50,
              "not_null": true,
              "sequence": 2
            },
            "email": {
              "name": "email",
              "type": "varchar",
              "length": 100,
              "not_null": true,
              "sequence": 3
            }
          },
          "constraints": {
            "pk_users": {
              "name": "pk_users",
              "type": "PRIMARY KEY",
              "columns": ["id"]
            },
            "uq_users_username": {
              "name": "uq_users_username",
              "type": "UNIQUE",
              "columns": ["username"]
            }
          },
          "indexes": {
            "idx_users_email": {
              "name": "idx_users_email",
              "columns": ["email"],
              "unique": false,
              "type": "btree"
            }
          }
        }
      ]
    }
  ]
}

Schema Structure

The JSON format follows RelSpec's internal model structure:

  • Database - Top-level container

    • name - Database name
    • database_type - Database system (postgresql, mysql, etc.)
    • schemas[] - Array of schemas
  • Schema - Schema/namespace

    • name - Schema name
    • tables[] - Array of tables
    • views[] - Array of views
    • sequences[] - Array of sequences
  • Table - Table definition

    • name - Table name
    • columns{} - Map of columns
    • constraints{} - Map of constraints
    • indexes{} - Map of indexes
    • relationships{} - Map of relationships

Notes

  • This is RelSpec's native interchange format
  • Preserves complete schema information
  • Ideal for version control and schema documentation
  • Can be used as an intermediate format for transformations