160 lines
3.8 KiB
Markdown
160 lines
3.8 KiB
Markdown
# YAML Reader
|
|
|
|
Reads database schema definitions from YAML files.
|
|
|
|
## Overview
|
|
|
|
The YAML Reader parses YAML files that define database schemas in RelSpec's canonical YAML format and converts them into RelSpec's internal database model representation.
|
|
|
|
## Features
|
|
|
|
- Reads RelSpec's standard YAML schema format
|
|
- Human-readable alternative to JSON 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
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"git.warky.dev/wdevs/relspecgo/pkg/readers"
|
|
"git.warky.dev/wdevs/relspecgo/pkg/readers/yaml"
|
|
)
|
|
|
|
func main() {
|
|
options := &readers.ReaderOptions{
|
|
FilePath: "/path/to/schema.yaml",
|
|
}
|
|
|
|
reader := yaml.NewReader(options)
|
|
db, err := reader.ReadDatabase()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Printf("Found %d schemas\n", len(db.Schemas))
|
|
}
|
|
```
|
|
|
|
### CLI Example
|
|
|
|
```bash
|
|
# Read YAML schema and convert to GORM models
|
|
relspec --input yaml --in-file schema.yaml --output gorm --out-file models.go
|
|
|
|
# Convert YAML to PostgreSQL DDL
|
|
relspec --input yaml --in-file database.yaml --output pgsql --out-file schema.sql
|
|
|
|
# Transform YAML to JSON
|
|
relspec --input yaml --in-file schema.yaml --output json --out-file schema.json
|
|
```
|
|
|
|
## Example YAML Schema
|
|
|
|
```yaml
|
|
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
|
|
- name: posts
|
|
schema: public
|
|
columns:
|
|
id:
|
|
name: id
|
|
type: bigint
|
|
not_null: true
|
|
is_primary_key: true
|
|
sequence: 1
|
|
user_id:
|
|
name: user_id
|
|
type: bigint
|
|
not_null: true
|
|
sequence: 2
|
|
title:
|
|
name: title
|
|
type: varchar
|
|
length: 200
|
|
not_null: true
|
|
sequence: 3
|
|
constraints:
|
|
fk_posts_user_id:
|
|
name: fk_posts_user_id
|
|
type: FOREIGN KEY
|
|
columns:
|
|
- user_id
|
|
referenced_table: users
|
|
referenced_schema: public
|
|
referenced_columns:
|
|
- id
|
|
on_delete: CASCADE
|
|
on_update: NO ACTION
|
|
```
|
|
|
|
## Schema Structure
|
|
|
|
The YAML format mirrors RelSpec's internal model structure with human-readable syntax:
|
|
|
|
- Database level: `name`, `database_type`, `schemas`
|
|
- Schema level: `name`, `tables`, `views`, `sequences`
|
|
- Table level: `name`, `schema`, `columns`, `constraints`, `indexes`, `relationships`
|
|
- Column level: `name`, `type`, `length`, `not_null`, `default`, etc.
|
|
|
|
## Notes
|
|
|
|
- YAML format is more human-readable than JSON
|
|
- Ideal for manual editing and version control
|
|
- Comments are supported in YAML
|
|
- Preserves complete schema information
|
|
- Can be used for configuration and documentation
|