json

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

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

Reader implements the readers.Reader interface for JSON format

func NewReader

func NewReader(options *readers.ReaderOptions) *Reader

NewReader creates a new JSON reader with the given options

func (*Reader) ReadDatabase

func (r *Reader) ReadDatabase() (*models.Database, error)

ReadDatabase reads and parses JSON input, returning a Database model

func (*Reader) ReadSchema

func (r *Reader) ReadSchema() (*models.Schema, error)

ReadSchema reads and parses JSON input, returning a Schema model

func (*Reader) ReadTable

func (r *Reader) ReadTable() (*models.Table, error)

ReadTable reads and parses JSON input, returning a Table model

Jump to

Keyboard shortcuts

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