schema2type 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/schema2type/cli.rb +2 -6
- data/lib/schema2type/schema_converter.rb +34 -67
- data/lib/schema2type/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c1f92fd874fa9544443fbb63b86d500777d18ed87b4bff1791cc10cb7e505fc
|
4
|
+
data.tar.gz: d896d9566f061ed9012927f62ac0dbd858a942ac74f7fdfec1d22aa62856aaaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 211ab47195029c4e1bdb334c59fa41c42fcff44399af28ebb38d264d30773792c6e3c8a22cb9ca7882ee7349232bf24aa705a1d1388270f9629e05f701754ab2
|
7
|
+
data.tar.gz: df37e175322b12a30b51215e68b00319213b8f055a53aa8105e9de0bcdfec9b526eddf68d51d1b17d2b2bfa4bab7c97e6d9b082688cfb14c1aa6256ac5222c47
|
data/lib/schema2type/cli.rb
CHANGED
@@ -1,18 +1,16 @@
|
|
1
1
|
require 'date'
|
2
2
|
|
3
3
|
module Schema2type
|
4
|
-
|
5
4
|
$convert_types = []
|
6
5
|
$schema_version = ''
|
7
6
|
|
8
7
|
def self.execute(input_file:, out_file:, name_space:)
|
9
|
-
|
10
8
|
eval(File.read(input_file))
|
11
9
|
|
12
10
|
convert_type_text = $convert_types.map { |t| " #{t}" }.join("\n").strip
|
13
11
|
|
14
12
|
File.open(out_file, "w") do |f|
|
15
|
-
|
13
|
+
f.puts <<-EOS
|
16
14
|
/* eslint no-unused-vars: 0 */
|
17
15
|
|
18
16
|
/**
|
@@ -23,12 +21,10 @@ module Schema2type
|
|
23
21
|
declare namespace #{name_space ||= "schema"} {
|
24
22
|
#{convert_type_text}
|
25
23
|
}
|
26
|
-
|
24
|
+
EOS
|
27
25
|
end
|
28
26
|
end
|
29
27
|
|
30
|
-
private
|
31
|
-
|
32
28
|
def self.create_table(table_name, *arg, &block)
|
33
29
|
converter = SchemaConverter.new(table_name: table_name)
|
34
30
|
block.call(converter)
|
@@ -1,84 +1,51 @@
|
|
1
|
-
require
|
1
|
+
require 'active_support/inflector'
|
2
2
|
|
3
3
|
module Schema2type
|
4
4
|
class SchemaConverter
|
5
5
|
attr_accessor :out_text
|
6
6
|
attr_reader :table_name
|
7
7
|
|
8
|
-
TYPE_STRING =
|
9
|
-
TYPE_NUMBER =
|
10
|
-
TYPE_BOOLEAN =
|
11
|
-
TYPE_DATE =
|
8
|
+
TYPE_STRING = 'string'.freeze
|
9
|
+
TYPE_NUMBER = 'number'.freeze
|
10
|
+
TYPE_BOOLEAN = 'string'.freeze
|
11
|
+
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
|
12
29
|
|
13
30
|
def initialize(table_name:)
|
14
31
|
@out_text = []
|
15
|
-
@table_name = table_name
|
32
|
+
@table_name = table_name.singularize.camelize
|
16
33
|
end
|
17
34
|
|
18
35
|
def finalize
|
19
|
-
@out_text.unshift "type #{@table_name
|
36
|
+
@out_text.unshift "type #{@table_name} = {"
|
20
37
|
@out_text << "}\n"
|
21
38
|
end
|
22
39
|
|
23
|
-
def
|
24
|
-
|
40
|
+
def self.define_convert_methods(methods)
|
41
|
+
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
|
44
|
+
end
|
45
|
+
end
|
25
46
|
end
|
26
47
|
|
27
|
-
|
28
|
-
push_property_line name: name, type: TYPE_STRING, options: options
|
29
|
-
end
|
30
|
-
|
31
|
-
def inet(name, *options)
|
32
|
-
push_property_line name: name, type: TYPE_STRING, options: options
|
33
|
-
end
|
34
|
-
|
35
|
-
def integer(name, *options)
|
36
|
-
push_property_line name: name, type: TYPE_NUMBER, options: options
|
37
|
-
end
|
38
|
-
|
39
|
-
def bigint(name, *options)
|
40
|
-
push_property_line name: name, type: TYPE_NUMBER, options: options
|
41
|
-
end
|
42
|
-
|
43
|
-
def float(name, *options)
|
44
|
-
push_property_line name: name, type: TYPE_NUMBER, options: options
|
45
|
-
end
|
46
|
-
|
47
|
-
def text(name, *options)
|
48
|
-
push_property_line name: name, type: TYPE_STRING, options: options
|
49
|
-
end
|
50
|
-
|
51
|
-
def boolean(name, *options)
|
52
|
-
push_property_line name: name, type: TYPE_BOOLEAN, options: options
|
53
|
-
end
|
54
|
-
|
55
|
-
def decimal(name, *options)
|
56
|
-
push_property_line name: name, type: TYPE_NUMBER, options: options
|
57
|
-
end
|
58
|
-
|
59
|
-
def json(name, *options)
|
60
|
-
push_property_line name: name, type: TYPE_STRING, options: options
|
61
|
-
end
|
62
|
-
|
63
|
-
def jsonb(name, *options)
|
64
|
-
push_property_line name: name, type: TYPE_STRING, options: options
|
65
|
-
end
|
66
|
-
|
67
|
-
def binary(name, *options)
|
68
|
-
push_property_line name: name, type: TYPE_STRING, options: options
|
69
|
-
end
|
70
|
-
|
71
|
-
def datetime(name, *options)
|
72
|
-
push_property_line name: name, type: TYPE_DATE, options: options
|
73
|
-
end
|
74
|
-
|
75
|
-
def timestamp(name, *options)
|
76
|
-
push_property_line name: name, type: TYPE_DATE, options: options
|
77
|
-
end
|
78
|
-
|
79
|
-
def datetime_with_timezone(name, *options)
|
80
|
-
push_property_line name: name, type: TYPE_DATE, options: options
|
81
|
-
end
|
48
|
+
define_convert_methods COLUMN_METHODS
|
82
49
|
|
83
50
|
def method_missing(*arg)
|
84
51
|
# To exclude unnecessary methods
|
@@ -87,9 +54,9 @@ module Schema2type
|
|
87
54
|
private
|
88
55
|
|
89
56
|
def push_property_line(name:, type:, options:)
|
90
|
-
is_non_nullable = options[0] && options[0].
|
91
|
-
|
92
|
-
property_line = is_non_nullable ? "#{
|
57
|
+
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"
|
93
60
|
|
94
61
|
@out_text << " #{property_line}"
|
95
62
|
end
|
data/lib/schema2type/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ryo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-04-
|
11
|
+
date: 2019-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|