Documentation
¶
Overview ¶
Package writers provides interfaces and implementations for writing database schemas to various output formats and destinations.
Overview ¶
The writers package defines a common Writer interface that all format-specific writers implement. This allows RelSpec to export database schemas to multiple formats including:
- SQL schema files (PostgreSQL, SQLite)
- Schema definition files (DBML, DCTX, DrawDB, GraphQL)
- ORM model files (GORM, Bun, Drizzle, Prisma, TypeORM)
- Data interchange formats (JSON, YAML)
Architecture ¶
Each writer implementation is located in its own subpackage (e.g., pkg/writers/dbml, pkg/writers/pgsql) and implements the Writer interface, supporting three levels of granularity:
- WriteDatabase() - Write complete database with all schemas
- WriteSchema() - Write single schema with all tables
- WriteTable() - Write single table with all columns and metadata
Usage ¶
Writers are instantiated with WriterOptions containing destination-specific configuration:
// Write to file
writer := dbml.NewWriter(&writers.WriterOptions{
OutputPath: "schema.dbml",
})
err := writer.WriteDatabase(db)
// Write ORM models with package name
writer := gorm.NewWriter(&writers.WriterOptions{
OutputPath: "./models",
PackageName: "models",
})
err := writer.WriteDatabase(db)
// Write with schema flattening for SQLite
writer := sqlite.NewWriter(&writers.WriterOptions{
OutputPath: "schema.sql",
FlattenSchema: true,
})
err := writer.WriteDatabase(db)
Schema Flattening ¶
The FlattenSchema option controls how schema-qualified table names are handled:
- false (default): Uses dot notation (schema.table)
- true: Joins with underscore (schema_table), useful for SQLite
Supported Formats ¶
- dbml: Database Markup Language files
- dctx: DCTX schema files
- drawdb: DrawDB JSON format
- graphql: GraphQL schema definition language
- json: JSON database schema
- yaml: YAML database schema
- gorm: Go GORM model structs
- bun: Go Bun model structs
- drizzle: TypeScript Drizzle ORM schemas
- prisma: Prisma schema language
- typeorm: TypeScript TypeORM entities
- pgsql: PostgreSQL SQL schema
- sqlite: SQLite SQL schema with automatic flattening
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func QualifiedTableName ¶ added in v1.0.30
QualifiedTableName returns a schema-qualified table name. When flatten is true, schema and table are joined with underscore (e.g., "schema_table"). When flatten is false, they are dot-separated (e.g., "schema.table"). If schema is empty, just the table name is returned regardless of flatten.
func SanitizeFilename ¶ added in v1.0.7
SanitizeFilename removes quotes, comments, and invalid characters from identifiers to make them safe for use in filenames. This handles: - Double and single quotes: "table_name" or 'table_name' -> table_name - DBML comments: table [note: 'description'] -> table - Invalid filename characters: replaced with underscores
func SanitizeStructTagValue ¶ added in v1.0.8
SanitizeStructTagValue sanitizes a value to be safely used inside Go struct tags. Go struct tags are delimited by backticks, so any backtick in the value would break the syntax. This function: - Removes DBML/DCTX comments in brackets - Removes all quotes (double, single, and backticks) - Returns a clean identifier safe for use in struct tags and field names
Types ¶
type Writer ¶
type Writer interface {
// WriteDatabase takes a Database model and writes it to the desired format
WriteDatabase(db *models.Database) error
// WriteSchema takes a Schema model and writes it to the desired format
WriteSchema(schema *models.Schema) error
// WriteTable takes a Table model and writes it to the desired format
WriteTable(table *models.Table) error
}
Writer defines the interface for writing database specifications to various output formats at different granularity levels
type WriterOptions ¶
type WriterOptions struct {
// OutputPath is the path where the output should be written
OutputPath string
// PackageName is the Go package name (for code generation)
PackageName string
// FlattenSchema disables schema.table dot notation and instead joins
// schema and table with an underscore (e.g., "public_users").
// Useful for databases like SQLite that do not support schemas.
FlattenSchema bool
// Additional options can be added here as needed
Metadata map[string]interface{}
}
WriterOptions contains common options for writers