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

Examples:

Export schema as TypeScript

Synthra::Export.typescript(schema, registry: registry)

Export generated data as JSON

Synthra::Export.json_data(schema, registry: registry, count: 100)

Export generated data as SQL INSERTs

Synthra::Export.sql_insert(schema, count: 50, dialect: :postgresql)

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

Class Method Details

.auto_filename(schema_name, format, **options) ⇒ String

Generate automatic filename

Parameters:

  • schema_name (String)

    schema name

  • format (String)

    format name

  • options (Hash)

    additional options (prefix, suffix)

Returns:

  • (String)

    generated filename



368
369
370
371
372
373
374
375
# File 'lib/synthra/export.rb', line 368

def auto_filename(schema_name, format, **options)
  prefix = options[:prefix] || ""
  suffix = options[:suffix] || ""
  ext = file_extension(format)
  
  base = to_snake_case(schema_name)
  "#{prefix}#{base}#{suffix}#{ext}"
end

.available_formatsArray<String>

List available formats

Returns:

  • (Array<String>)

    available format names



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

Parameters:

  • schema (Schema)

    the schema to generate from

  • options (Hash)

    export options (count, seed, mode)

Returns:

  • (String)

    CSV string



238
239
240
# File 'lib/synthra/export.rb', line 238

def csv(schema, **options)
  Csv.new(schema, **options).export
end

.data_format?(format) ⇒ Boolean

Check if format is for data export

Parameters:

  • format (String)

    format name

Returns:

  • (Boolean)


348
349
350
# File 'lib/synthra/export.rb', line 348

def data_format?(format)
  DATA_FORMATS.key?(format.to_s.downcase)
end

.data_formatsArray<String>

List data export formats

Returns:

  • (Array<String>)

    data format names



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

Parameters:

  • format (String, Symbol)

    the export format

  • schema (Schema)

    the schema to export

  • registry (Registry) (defaults to: nil)

    the registry for resolving references

  • options (Hash)

    format-specific options

Returns:

  • (String)

    the exported content



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, **options)
  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)
    options[:dialect] = format_key.to_sym
  end

  exporter = exporter_class.new(schema, registry: registry, **options)
  exporter.export
end

.export_all(registry, format, **options) ⇒ String

Export all schemas from a registry to a format

Parameters:

  • registry (Registry)

    the registry

  • format (String, Symbol)

    the export format

  • options (Hash)

    format-specific options

Returns:

  • (String)

    the exported content



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, **options)
  format_key = format.to_s.downcase

  case format_key
  when "typescript", "ts"
    Typescript.export_all(registry, **options)
  when "javascript", "js"
    Javascript.export_all(registry, **options)
  when "python", "py", "pydantic", "dataclass"
    options[:style] = :dataclass if format_key == "dataclass"
    Python.export_all(registry, **options)
  when "sql", "postgresql", "mysql", "sqlite", "sql-ddl"
    options[:dialect] = format_key.to_sym if %w[postgresql mysql sqlite].include?(format_key)
    Sql.export_all(registry, **options)
  when "graphviz", "dot"
    Graphviz.new(nil, registry: registry, **options).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

Parameters:

  • format (String)

    format name

  • schema (Schema)

    schema to export

  • output_dir (String) (defaults to: ".")

    output directory

  • options (Hash)

    export options

Returns:

  • (String)

    path to 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: ".", **options)
  filename = auto_filename(schema.name, format, **options)
  path = File.join(output_dir, filename)
  
  content = export(format, schema, **options)
  File.write(path, content)
  
  path
end

.file_extension(format) ⇒ String

Get file extension for a format

Parameters:

  • format (String)

    format name

Returns:

  • (String)

    file extension with dot



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

Parameters:

  • schema (Schema)

    the schema to export

  • options (Hash)

    export options (registry)

Returns:

  • (String)

    GraphQL SDL string



214
215
216
# File 'lib/synthra/export.rb', line 214

def graphql(schema, **options)
  Graphql.new(schema, **options).export
end

.graphviz(schema = nil, **options) ⇒ String

Export schemas as GraphViz DOT

Parameters:

  • schema (Schema, nil) (defaults to: nil)

    single schema or nil for registry

  • options (Hash)

    export options (registry, direction)

Returns:

  • (String)

    DOT string



204
205
206
# File 'lib/synthra/export.rb', line 204

def graphviz(schema = nil, **options)
  Graphviz.new(schema, **options).export
end

.javascript(schema, **options) ⇒ String

Export schema as JavaScript with JSDoc

Parameters:

  • schema (Schema)

    the schema to export

  • options (Hash)

    export options

Returns:

  • (String)

    JavaScript with JSDoc types



174
175
176
# File 'lib/synthra/export.rb', line 174

def javascript(schema, **options)
  Javascript.new(schema, **options).export
end

.json_data(schema, **options) ⇒ String

Export generated data as JSON

Parameters:

  • schema (Schema)

    the schema to generate from

  • options (Hash)

    export options (count, seed, mode, pretty, envelope)

Returns:

  • (String)

    JSON string



228
229
230
# File 'lib/synthra/export.rb', line 228

def json_data(schema, **options)
  JsonData.new(schema, **options).export
end

.json_schema(schema, **options) ⇒ String

Export schema as JSON Schema

Parameters:

  • schema (Schema)

    the schema to export

  • options (Hash)

    export options

Returns:

  • (String)

    JSON Schema string



154
155
156
# File 'lib/synthra/export.rb', line 154

def json_schema(schema, **options)
  JsonSchema.new(schema, **options).export
end

.python(schema, **options) ⇒ String

Export schema as Python (Pydantic/dataclass/TypedDict)

Parameters:

  • schema (Schema)

    the schema to export

  • options (Hash)

    export options (style: :pydantic/:dataclass/:typed_dict)

Returns:

  • (String)

    Python code



184
185
186
# File 'lib/synthra/export.rb', line 184

def python(schema, **options)
  Python.new(schema, **options).export
end

.schema_format?(format) ⇒ Boolean

Check if format is for schema export

Parameters:

  • format (String)

    format name

Returns:

  • (Boolean)


339
340
341
# File 'lib/synthra/export.rb', line 339

def schema_format?(format)
  SCHEMA_FORMATS.key?(format.to_s.downcase)
end

.schema_formatsArray<String>

List schema export formats

Returns:

  • (Array<String>)

    schema format names



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

Parameters:

  • schema (Schema)

    the schema to export

  • options (Hash)

    export options (dialect: :postgresql/:mysql/:sqlite)

Returns:

  • (String)

    SQL statement string



194
195
196
# File 'lib/synthra/export.rb', line 194

def sql(schema, **options)
  Sql.new(schema, **options).export
end

.sql_insert(schema, **options) ⇒ String

Export generated data as SQL INSERT statements

Parameters:

  • schema (Schema)

    the schema to generate from

  • options (Hash)

    export options (count, seed, mode, dialect, batch_insert)

Returns:

  • (String)

    SQL INSERT statements



248
249
250
# File 'lib/synthra/export.rb', line 248

def sql_insert(schema, **options)
  SqlInsert.new(schema, **options).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

Parameters:

  • schema (Schema)

    the schema to export

  • options (Hash)

    export options

Returns:

  • (String)

    TypeScript interface string



164
165
166
# File 'lib/synthra/export.rb', line 164

def typescript(schema, **options)
  Typescript.new(schema, **options).export
end

.xml_data(schema, **options) ⇒ String

Export generated data as XML

Parameters:

  • schema (Schema)

    the schema to generate from

  • options (Hash)

    export options (count, seed, mode, root, item, indent)

Returns:

  • (String)

    XML string



268
269
270
# File 'lib/synthra/export.rb', line 268

def xml_data(schema, **options)
  XmlData.new(schema, **options).export
end

.yaml_data(schema, **options) ⇒ String

Export generated data as YAML

Parameters:

  • schema (Schema)

    the schema to generate from

  • options (Hash)

    export options (count, seed, mode, root_key)

Returns:

  • (String)

    YAML string



258
259
260
# File 'lib/synthra/export.rb', line 258

def yaml_data(schema, **options)
  YamlData.new(schema, **options).export
end