Module: Synthra::Export
- Defined in:
- lib/synthra/export.rb,
lib/synthra/export/csv.rb,
lib/synthra/export/sql.rb,
lib/synthra/export/base.rb,
lib/synthra/export/python.rb,
lib/synthra/export/graphql.rb,
lib/synthra/export/openapi.rb,
lib/synthra/export/graphviz.rb,
lib/synthra/export/protobuf.rb,
lib/synthra/export/xml_data.rb,
lib/synthra/export/json_data.rb,
lib/synthra/export/terraform.rb,
lib/synthra/export/yaml_data.rb,
lib/synthra/export/javascript.rb,
lib/synthra/export/sql_insert.rb,
lib/synthra/export/typescript.rb,
lib/synthra/export/json_schema.rb,
lib/synthra/export/type_mapping.rb,
lib/synthra/export/graphql_federation.rb
Overview
Export module for converting schemas to various formats
Supports two categories of exports:
Schema Exports (structure/types)
- JSON Schema (draft 2020-12)
- TypeScript interfaces
- JavaScript with JSDoc types
- SQL CREATE TABLE statements
- GraphViz DOT diagrams
Data Exports (generated records)
- JSON data
- CSV data
- SQL INSERT statements
- YAML data
- XML data
Defined Under Namespace
Modules: TypeMapping Classes: Base, Csv, GraphQLFederation, Graphql, Graphviz, Javascript, JsonData, JsonSchema, OpenAPI, Protobuf, Python, Sql, SqlInsert, Terraform, Typescript, XmlData, YamlData
Constant Summary collapse
- SCHEMA_FORMATS =
=========================================================================
Schema Export Formats (types/structure)
{ "json-schema" => JsonSchema, "typescript" => Typescript, "ts" => Typescript, "javascript" => Javascript, "js" => Javascript, "python" => Python, "py" => Python, "pydantic" => Python, "dataclass" => Python, "sql" => Sql, "sql-ddl" => Sql, "postgresql" => Sql, "mysql" => Sql, "sqlite" => Sql, "graphviz" => Graphviz, "dot" => Graphviz, "graphql" => Graphql, "gql" => Graphql }.freeze
- FILE_EXTENSIONS =
File extensions for auto-generated filenames
{ "json-schema" => ".schema.json", "typescript" => ".ts", "ts" => ".ts", "javascript" => ".js", "js" => ".js", "python" => ".py", "py" => ".py", "pydantic" => ".py", "dataclass" => ".py", "sql" => ".sql", "sql-ddl" => ".sql", "postgresql" => ".sql", "mysql" => ".sql", "sqlite" => ".sql", "graphviz" => ".dot", "dot" => ".dot", "graphql" => ".graphql", "gql" => ".graphql", "json" => ".json", "json-data" => ".json", "csv" => ".csv", "sql-insert" => ".sql", "insert" => ".sql", "yaml" => ".yaml", "yml" => ".yaml", "xml" => ".xml" }.freeze
- DATA_FORMATS =
=========================================================================
Data Export Formats (generated records)
{ "json" => JsonData, "json-data" => JsonData, "csv" => Csv, "sql-insert" => SqlInsert, "insert" => SqlInsert, "yaml" => YamlData, "yml" => YamlData, "xml" => XmlData }.freeze
- FORMATS =
All available formats
SCHEMA_FORMATS.merge(DATA_FORMATS).freeze
Class Method Summary collapse
-
.auto_filename(schema_name, format, **options) ⇒ String
Generate automatic filename.
-
.available_formats ⇒ Array<String>
List available formats.
-
.csv(schema, **options) ⇒ String
Export generated data as CSV.
-
.data_format?(format) ⇒ Boolean
Check if format is for data export.
-
.data_formats ⇒ Array<String>
List data export formats.
-
.export(format, schema, registry: nil, **options) ⇒ String
Export schema to specified format.
-
.export_all(registry, format, **options) ⇒ String
Export all schemas from a registry to a format.
-
.export_to_file(format, schema, output_dir: ".", **options) ⇒ String
Export and save to auto-generated file.
-
.file_extension(format) ⇒ String
Get file extension for a format.
-
.graphql(schema, **options) ⇒ String
Export schema as GraphQL SDL.
-
.graphviz(schema = nil, **options) ⇒ String
Export schemas as GraphViz DOT.
-
.javascript(schema, **options) ⇒ String
Export schema as JavaScript with JSDoc.
-
.json_data(schema, **options) ⇒ String
Export generated data as JSON.
-
.json_schema(schema, **options) ⇒ String
Export schema as JSON Schema.
-
.python(schema, **options) ⇒ String
Export schema as Python (Pydantic/dataclass/TypedDict).
-
.schema_format?(format) ⇒ Boolean
Check if format is for schema export.
-
.schema_formats ⇒ Array<String>
List schema export formats.
-
.sql(schema, **options) ⇒ String
Export schema as SQL CREATE TABLE.
-
.sql_insert(schema, **options) ⇒ String
Export generated data as SQL INSERT statements.
- .to_snake_case(str) ⇒ Object private
-
.typescript(schema, **options) ⇒ String
Export schema as TypeScript interface.
-
.xml_data(schema, **options) ⇒ String
Export generated data as XML.
-
.yaml_data(schema, **options) ⇒ String
Export generated data as YAML.
Class Method Details
.auto_filename(schema_name, format, **options) ⇒ String
Generate automatic filename
368 369 370 371 372 373 374 375 |
# File 'lib/synthra/export.rb', line 368 def auto_filename(schema_name, format, **) prefix = [:prefix] || "" suffix = [:suffix] || "" ext = file_extension(format) base = to_snake_case(schema_name) "#{prefix}#{base}#{suffix}#{ext}" end |
.available_formats ⇒ Array<String>
List available formats
314 315 316 |
# File 'lib/synthra/export.rb', line 314 def available_formats FORMATS.keys.uniq.sort end |
.csv(schema, **options) ⇒ String
Export generated data as CSV
238 239 240 |
# File 'lib/synthra/export.rb', line 238 def csv(schema, **) Csv.new(schema, **).export end |
.data_format?(format) ⇒ Boolean
Check if format is for data export
348 349 350 |
# File 'lib/synthra/export.rb', line 348 def data_format?(format) DATA_FORMATS.key?(format.to_s.downcase) end |
.data_formats ⇒ Array<String>
List data export formats
330 331 332 |
# File 'lib/synthra/export.rb', line 330 def data_formats DATA_FORMATS.keys.uniq.sort end |
.export(format, schema, registry: nil, **options) ⇒ String
Export schema to specified format
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/synthra/export.rb', line 126 def export(format, schema, registry: nil, **) format_key = format.to_s.downcase exporter_class = FORMATS[format_key] unless exporter_class available = available_formats.join(", ") raise ArgumentError, "Unknown format: #{format}. Available: #{available}" end # Set dialect for SQL formats if %w[postgresql mysql sqlite].include?(format_key) [:dialect] = format_key.to_sym end exporter = exporter_class.new(schema, registry: registry, **) exporter.export end |
.export_all(registry, format, **options) ⇒ String
Export all schemas from a registry to a format
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/synthra/export.rb', line 283 def export_all(registry, format, **) format_key = format.to_s.downcase case format_key when "typescript", "ts" Typescript.export_all(registry, **) when "javascript", "js" Javascript.export_all(registry, **) when "python", "py", "pydantic", "dataclass" [:style] = :dataclass if format_key == "dataclass" Python.export_all(registry, **) when "sql", "postgresql", "mysql", "sqlite", "sql-ddl" [:dialect] = format_key.to_sym if %w[postgresql mysql sqlite].include?(format_key) Sql.export_all(registry, **) when "graphviz", "dot" Graphviz.new(nil, registry: registry, **).export when "graphql", "gql" Graphql.export_all(registry) else raise ArgumentError, "Bulk export not supported for format: #{format}" end end |
.export_to_file(format, schema, output_dir: ".", **options) ⇒ String
Export and save to auto-generated file
385 386 387 388 389 390 391 392 393 |
# File 'lib/synthra/export.rb', line 385 def export_to_file(format, schema, output_dir: ".", **) filename = auto_filename(schema.name, format, **) path = File.join(output_dir, filename) content = export(format, schema, **) File.write(path, content) path end |
.file_extension(format) ⇒ String
Get file extension for a format
357 358 359 |
# File 'lib/synthra/export.rb', line 357 def file_extension(format) FILE_EXTENSIONS[format.to_s.downcase] || ".txt" end |
.graphql(schema, **options) ⇒ String
Export schema as GraphQL SDL
214 215 216 |
# File 'lib/synthra/export.rb', line 214 def graphql(schema, **) Graphql.new(schema, **).export end |
.graphviz(schema = nil, **options) ⇒ String
Export schemas as GraphViz DOT
204 205 206 |
# File 'lib/synthra/export.rb', line 204 def graphviz(schema = nil, **) Graphviz.new(schema, **).export end |
.javascript(schema, **options) ⇒ String
Export schema as JavaScript with JSDoc
174 175 176 |
# File 'lib/synthra/export.rb', line 174 def javascript(schema, **) Javascript.new(schema, **).export end |
.json_data(schema, **options) ⇒ String
Export generated data as JSON
228 229 230 |
# File 'lib/synthra/export.rb', line 228 def json_data(schema, **) JsonData.new(schema, **).export end |
.json_schema(schema, **options) ⇒ String
Export schema as JSON Schema
154 155 156 |
# File 'lib/synthra/export.rb', line 154 def json_schema(schema, **) JsonSchema.new(schema, **).export end |
.python(schema, **options) ⇒ String
Export schema as Python (Pydantic/dataclass/TypedDict)
184 185 186 |
# File 'lib/synthra/export.rb', line 184 def python(schema, **) Python.new(schema, **).export end |
.schema_format?(format) ⇒ Boolean
Check if format is for schema export
339 340 341 |
# File 'lib/synthra/export.rb', line 339 def schema_format?(format) SCHEMA_FORMATS.key?(format.to_s.downcase) end |
.schema_formats ⇒ Array<String>
List schema export formats
322 323 324 |
# File 'lib/synthra/export.rb', line 322 def schema_formats SCHEMA_FORMATS.keys.uniq.sort end |
.sql(schema, **options) ⇒ String
Export schema as SQL CREATE TABLE
194 195 196 |
# File 'lib/synthra/export.rb', line 194 def sql(schema, **) Sql.new(schema, **).export end |
.sql_insert(schema, **options) ⇒ String
Export generated data as SQL INSERT statements
248 249 250 |
# File 'lib/synthra/export.rb', line 248 def sql_insert(schema, **) SqlInsert.new(schema, **).export end |
.to_snake_case(str) ⇒ Object (private)
397 398 399 400 401 |
# File 'lib/synthra/export.rb', line 397 def to_snake_case(str) str.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .downcase end |
.typescript(schema, **options) ⇒ String
Export schema as TypeScript interface
164 165 166 |
# File 'lib/synthra/export.rb', line 164 def typescript(schema, **) Typescript.new(schema, **).export end |