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 containername- Database namedatabase_type- Database system (postgresql, mysql, etc.)schemas[]- Array of schemas
-
Schema- Schema/namespacename- Schema nametables[]- Array of tablesviews[]- Array of viewssequences[]- Array of sequences
-
Table- Table definitionname- Table namecolumns{}- Map of columnsconstraints{}- Map of constraintsindexes{}- Map of indexesrelationships{}- 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