Class: Synthra::CLI::Commands::Export

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/cli/commands/export.rb

Overview

Export command - exports schemas to various formats

Examples:

synthra export User -d schemas -f typescript -o user.ts
synthra export --all -d schemas -f graphql -o schema.graphql

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Synthra::CLI::Commands::Base

Instance Method Details

#build_export_options(registry) ⇒ Object (private)



114
115
116
117
118
119
120
121
# File 'lib/synthra/cli/commands/export.rb', line 114

def build_export_options(registry)
  {
    registry: registry,
    count: options[:count],
    seed: options[:seed],
    mode: options[:mode] || :random
  }.compact
end

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/synthra/cli/commands/export.rb', line 15

def call
  schema_name = args.shift

  registry = Registry.new
  load_path(registry, options[:dir], validate: false)

  format = options[:export_format]

  output = if options[:all]
             export_all(registry, format)
           else
             unless schema_name
               error "Usage: synthra export <SchemaName> [options]"
               error "       synthra export --all [options]"
               return EXIT_PARSE_ERROR
             end
             export_single(registry, schema_name, format)
           end

  return EXIT_PARSE_ERROR unless output

  write_output(output, schema_name)
  EXIT_SUCCESS
rescue KeyError
  error "Unknown schema: #{schema_name}"
  $stderr.puts "Available schemas: #{registry.names.join(", ")}"
  EXIT_PARSE_ERROR
end

#determine_output_path(schema_name) ⇒ Object (private)



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/synthra/cli/commands/export.rb', line 135

def determine_output_path(schema_name)
  if options[:output]
    options[:output]
  elsif options[:auto_file]
    output_dir = options[:output_dir] || "."
    filename = Synthra::Export.auto_filename(
      options[:all] ? "all_schemas" : schema_name,
      options[:export_format]
    )
    File.join(output_dir, filename)
  end
end

#export_all(registry, format) ⇒ Object (private)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/synthra/cli/commands/export.rb', line 46

def export_all(registry, format)
  case format
  when "typescript", "ts"
    Synthra::Export::Typescript.export_all(registry)
  when "javascript", "js"
    Synthra::Export::Javascript.export_all(registry)
  when "python", "py", "pydantic"
    Synthra::Export::Python.export_all(registry, style: options[:python_style])
  when "dataclass"
    Synthra::Export::Python.export_all(registry, style: :dataclass)
  when "sql", "sql-ddl"
    Synthra::Export::Sql.export_all(registry, dialect: options[:sql_dialect])
  when "graphviz", "dot"
    Synthra::Export::Graphviz.new(nil, registry: registry).export
  when "graphql"
    Synthra::Export::Graphql.export_all(registry)
  else
    error "Bulk export not supported for format: #{format}"
    nil
  end
end

#export_single(registry, schema_name, format) ⇒ Object (private)



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/synthra/cli/commands/export.rb', line 68

def export_single(registry, schema_name, format)
  schema = registry.schema(schema_name)
  export_opts = build_export_options(registry)

  case format
  # Schema export formats
  when "json-schema"
    Synthra::Export::JsonSchema.new(schema, **export_opts).export
  when "typescript", "ts"
    Synthra::Export::Typescript.new(schema, **export_opts).export
  when "javascript", "js"
    Synthra::Export::Javascript.new(schema, **export_opts).export
  when "python", "py", "pydantic"
    Synthra::Export::Python.new(schema, **export_opts.merge(style: options[:python_style])).export
  when "dataclass"
    Synthra::Export::Python.new(schema, **export_opts.merge(style: :dataclass)).export
  when "sql", "sql-ddl"
    Synthra::Export::Sql.new(schema, registry: registry, dialect: options[:sql_dialect]).export
  when "graphviz", "dot"
    Synthra::Export::Graphviz.new(schema, **export_opts).export
  when "graphql"
    Synthra::Export::Graphql.new(schema, **export_opts).export

  # Data export formats
  when "json", "json-data"
    Synthra::Export::JsonData.new(schema, **export_opts.merge(pretty: options[:pretty])).export
  when "csv"
    Synthra::Export::Csv.new(schema, **export_opts).export
  when "sql-insert", "insert"
    Synthra::Export::SqlInsert.new(schema, **export_opts.merge(
      dialect: options[:sql_dialect],
      batch_insert: options[:batch_insert]
    )).export
  when "yaml", "yml"
    Synthra::Export::YamlData.new(schema, **export_opts).export
  when "xml"
    Synthra::Export::XmlData.new(schema, **export_opts.merge(
      root: options[:root],
      item: options[:item]
    )).export
  else
    error "Unknown export format: #{format}"
    nil
  end
end

#write_output(output, schema_name) ⇒ Object (private)



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/synthra/cli/commands/export.rb', line 123

def write_output(output, schema_name)
  output_path = determine_output_path(schema_name)

  if output_path
    FileUtils.mkdir_p(File.dirname(output_path)) if File.dirname(output_path) != "."
    File.write(output_path, output)
    success "Exported to #{output_path}"
  else
    puts output
  end
end