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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +21 -1
- data/exe/schema2type +2 -2
- data/lib/schema2type/cli.rb +3 -2
- data/lib/schema2type/conversion_table.yml +16 -0
- data/lib/schema2type/schema_converter.rb +9 -23
- data/lib/schema2type/version.rb +1 -1
- metadata +2 -2
- data/lib/config/conversion_table.yml +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa2b5d845e214910826345f21bd320c512663c82475b2dc165ca5c49ffa19745
|
4
|
+
data.tar.gz: bd65e62df051c4c965885cecdfd0ab8273e9c2f6846d81dc1a783e22d131be69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26f7181e73168703d90eb5f58401d23c3a47b85668cc8564c1931d44838b9f90578713576a2bdc0b9f90d03c7ce063512d26497d5c96eb140a00cf9c0c34c5bb
|
7
|
+
data.tar.gz: 9a42df4724c97244fa781347985c8dcb19752ba46d5e938c8edc3ab05fd04c02b56485658dfdbb2c9eaab681e8f9be9f80195b1262fb906e6aa61de18e376f9e
|
data/Gemfile.lock
CHANGED
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"])
|
data/lib/schema2type/cli.rb
CHANGED
@@ -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
|
43
|
-
push_property_line name: name, type: m
|
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
|
-
|
59
|
-
property_line = is_non_nullable ? "#{
|
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
|
data/lib/schema2type/version.rb
CHANGED
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.
|
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
|