schema2type 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ce9497c7b3f40dc2f44ab9a4c7a4b6a1e4e3c865fb99fd58fbdf01e96b7b6ee
4
- data.tar.gz: 49a0de50ea29e040d2b7d8dd4914e24887b7ee0e30d83676a906bd08300b3a81
3
+ metadata.gz: fa2b5d845e214910826345f21bd320c512663c82475b2dc165ca5c49ffa19745
4
+ data.tar.gz: bd65e62df051c4c965885cecdfd0ab8273e9c2f6846d81dc1a783e22d131be69
5
5
  SHA512:
6
- metadata.gz: 8ca522a0e1d764b71f9e886245de30b747dfe7de3204a19a75229adc30043cbffbd65219ddac0148271731d62cc78491d13ea27d1af818228e7a6880dac4b3be
7
- data.tar.gz: 84267680b30d46781ec4059152d7748143f55b6c2a940ab534b5904d57275f1f28fa3c950e26adc30d1472ef0d0a197c40dc58a07a6f162acd3a7645fce9fd60
6
+ metadata.gz: 26f7181e73168703d90eb5f58401d23c3a47b85668cc8564c1931d44838b9f90578713576a2bdc0b9f90d03c7ce063512d26497d5c96eb140a00cf9c0c34c5bb
7
+ data.tar.gz: 9a42df4724c97244fa781347985c8dcb19752ba46d5e938c8edc3ab05fd04c02b56485658dfdbb2c9eaab681e8f9be9f80195b1262fb906e6aa61de18e376f9e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- schema2type (0.1.5)
4
+ schema2type (0.1.6)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -63,7 +63,27 @@ bundle exec schema2type -s db/schema.rb -o schema.d.ts -n schema
63
63
  | -s | true | - | Path of your schema.rb |
64
64
  | -o | true | - | Output file name of TypeScript |
65
65
  | -n | false | "schema" | Name of declare namespace |
66
-
66
+ | --snake | false | false | Convert property name to snake_case |
67
+
68
+ #### conversion table
69
+
70
+ |create_table block method| converted Type|
71
+ |---|---|
72
+ | string | string |
73
+ | text | string |
74
+ | json | string |
75
+ | jsonb | string |
76
+ | binary | string |
77
+ | inet | string |
78
+ | integer | number |
79
+ | bigint | number |
80
+ | float | number |
81
+ | decimal | number |
82
+ | boolean | boolean |
83
+ | date | Date |
84
+ | datetime | Date |
85
+ | timestamp | Date |
86
+ | datetime_with_timezone | Date |
67
87
  ## License
68
88
 
69
89
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/exe/schema2type CHANGED
@@ -3,6 +3,6 @@
3
3
  require 'optparse'
4
4
  require 'schema2type'
5
5
 
6
- params = ARGV.getopts('s:o:n:')
6
+ params = ARGV.getopts('s:o:n:', 'snake')
7
7
 
8
- Schema2type.execute(input_file: params["s"], out_file: params["o"], name_space: params["n"])
8
+ Schema2type.execute(input_file: params["s"], out_file: params["o"], name_space: params["n"], snake_case: params["snake"])
@@ -4,7 +4,8 @@ module Schema2type
4
4
  $convert_types = []
5
5
  $schema_version = ''
6
6
 
7
- def self.execute(input_file:, out_file:, name_space:)
7
+ def self.execute(input_file:, out_file:, name_space:, snake_case:)
8
+ $snake_case = snake_case
8
9
  eval(File.read(input_file))
9
10
 
10
11
  convert_type_text = $convert_types.map { |t| " #{t}" }.join("\n").strip
@@ -26,7 +27,7 @@ EOS
26
27
  end
27
28
 
28
29
  def self.create_table(table_name, *arg, &block)
29
- converter = SchemaConverter.new(table_name: table_name)
30
+ converter = SchemaConverter.new(table_name: table_name, snake_case: $snake_case)
30
31
  block.call(converter)
31
32
  converter.finalize
32
33
 
@@ -0,0 +1,16 @@
1
+ # This is the correspondence table of method of schema.rb and Type of TypeScript
2
+ string: string
3
+ inet: string
4
+ text: string
5
+ json: string
6
+ jsonb: string
7
+ binary: string
8
+ integer: number
9
+ bigint: number
10
+ float: number
11
+ decimal: number
12
+ boolean: boolean
13
+ date: Date
14
+ datetime: Date
15
+ timestamp: Date
16
+ datetime_with_timezone: Date
@@ -1,35 +1,21 @@
1
+ require 'yaml'
1
2
  require 'active_support/inflector'
2
3
 
3
4
  module Schema2type
4
5
  class SchemaConverter
5
6
  attr_accessor :out_text
6
- attr_reader :table_name
7
+ attr_reader :table_name, :convertion_table, :snake_case
7
8
 
8
9
  TYPE_STRING = 'string'.freeze
9
10
  TYPE_NUMBER = 'number'.freeze
10
11
  TYPE_BOOLEAN = 'boolean'.freeze
11
12
  TYPE_DATE = 'Date'.freeze
12
- COLUMN_METHODS = [
13
- { string: TYPE_STRING },
14
- { inet: TYPE_STRING },
15
- { integer: TYPE_NUMBER },
16
- { bigint: TYPE_NUMBER },
17
- { float: TYPE_NUMBER },
18
- { text: TYPE_STRING },
19
- { boolean: TYPE_BOOLEAN },
20
- { decimal: TYPE_NUMBER },
21
- { json: TYPE_STRING },
22
- { jsonb: TYPE_STRING },
23
- { binary: TYPE_STRING },
24
- { date: TYPE_DATE },
25
- { datetime: TYPE_DATE },
26
- { timestamp: TYPE_DATE },
27
- { datetime_with_timezone: TYPE_DATE }
28
- ].map(&:freeze).freeze
13
+ COLUMN_METHODS = YAML.load_file(File.expand_path(__dir__) + '/conversion_table.yml').to_a
29
14
 
30
- def initialize(table_name:)
15
+ def initialize(table_name:, snake_case: false)
31
16
  @out_text = []
32
17
  @table_name = table_name.singularize.camelize
18
+ @snake_case = snake_case
33
19
  end
34
20
 
35
21
  def finalize
@@ -39,8 +25,8 @@ module Schema2type
39
25
 
40
26
  def self.define_convert_methods(methods)
41
27
  methods.each do |m|
42
- define_method(m.keys[0]) do |name, *options|
43
- push_property_line name: name, type: m.values[0], options: options
28
+ define_method(m[0]) do |name, *options|
29
+ push_property_line name: name, type: m[1], options: options
44
30
  end
45
31
  end
46
32
  end
@@ -55,8 +41,8 @@ module Schema2type
55
41
 
56
42
  def push_property_line(name:, type:, options:)
57
43
  is_non_nullable = options[0] && options[0].key?(:null) && !options[0][:null]
58
- camelize_name = name.camelcase(:lower)
59
- property_line = is_non_nullable ? "#{camelize_name}: #{type}" : "#{camelize_name}: #{type} | null"
44
+ formatted_name = @snake_case ? name.underscore : name.camelcase(:lower)
45
+ property_line = is_non_nullable ? "#{formatted_name}: #{type};" : "#{formatted_name}: #{type} | null;"
60
46
 
61
47
  @out_text << " #{property_line}"
62
48
  end
@@ -1,3 +1,3 @@
1
1
  module Schema2type
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema2type
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - ryo
@@ -87,9 +87,9 @@ files:
87
87
  - bin/console
88
88
  - bin/setup
89
89
  - exe/schema2type
90
- - lib/config/conversion_table.yml
91
90
  - lib/schema2type.rb
92
91
  - lib/schema2type/cli.rb
92
+ - lib/schema2type/conversion_table.yml
93
93
  - lib/schema2type/schema_converter.rb
94
94
  - lib/schema2type/version.rb
95
95
  - schema2type.gemspec
@@ -1,14 +0,0 @@
1
- string: string
2
- inet: string
3
- integer: numberg
4
- bigint: numberg
5
- float: numberg
6
- text: string
7
- boolean: boolean
8
- decimal: numberg
9
- json: string
10
- jsonb: string
11
- binary: string
12
- date: TYPE_DATE
13
- datetime: TYPE_DATE
14
- timestamp: TYPE_DATE