schema2type 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +25 -20
- data/README.md +11 -10
- data/exe/schema2type +1 -1
- data/lib/schema2type.rb +3 -3
- data/lib/schema2type/cli.rb +6 -37
- data/lib/schema2type/conversion_table.yml +4 -4
- data/lib/schema2type/covert_service.rb +45 -0
- data/lib/schema2type/schema_converter.rb +10 -12
- data/lib/schema2type/version.rb +1 -1
- data/schema2type.gemspec +1 -1
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 912e9bb9f837865415f701aec0b4844f10dd80a3064a072dfac0a47f480c0e0c
|
4
|
+
data.tar.gz: 055064e54272d3fd75c6821a438e968165e15f5d4c64d6eecc234a8b0c348ad7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b39374c2b97e67a7caba68cf89dc22882f06f1e4fe9b464cdd95ca3b8901586fe7b5a73e8c4b0ddfb1f7e41ee17074eb822de7138b1c4faf3d82bc7da8c718c6
|
7
|
+
data.tar.gz: c22cc454e307e88f441195e5ae0d55cceca5a169cb2ec40117cb79a2e1192d261274a39260039e8df1389ae2034b2ccc8cd9fb4aa8f2ce270e61f0a02c9b7fb3
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.0
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,39 +1,41 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
schema2type (0.
|
4
|
+
schema2type (0.3.0)
|
5
5
|
activesupport
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (
|
10
|
+
activesupport (6.0.3.1)
|
11
11
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
12
|
i18n (>= 0.7, < 2)
|
13
13
|
minitest (~> 5.1)
|
14
14
|
tzinfo (~> 1.1)
|
15
|
-
|
15
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
16
|
+
concurrent-ruby (1.1.6)
|
16
17
|
diff-lcs (1.3)
|
17
|
-
i18n (1.
|
18
|
+
i18n (1.8.3)
|
18
19
|
concurrent-ruby (~> 1.0)
|
19
|
-
minitest (5.
|
20
|
-
rake (
|
21
|
-
rspec (3.
|
22
|
-
rspec-core (~> 3.
|
23
|
-
rspec-expectations (~> 3.
|
24
|
-
rspec-mocks (~> 3.
|
25
|
-
rspec-core (3.
|
26
|
-
rspec-support (~> 3.
|
27
|
-
rspec-expectations (3.
|
20
|
+
minitest (5.14.1)
|
21
|
+
rake (13.0.1)
|
22
|
+
rspec (3.9.0)
|
23
|
+
rspec-core (~> 3.9.0)
|
24
|
+
rspec-expectations (~> 3.9.0)
|
25
|
+
rspec-mocks (~> 3.9.0)
|
26
|
+
rspec-core (3.9.2)
|
27
|
+
rspec-support (~> 3.9.3)
|
28
|
+
rspec-expectations (3.9.2)
|
28
29
|
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
-
rspec-support (~> 3.
|
30
|
-
rspec-mocks (3.
|
30
|
+
rspec-support (~> 3.9.0)
|
31
|
+
rspec-mocks (3.9.1)
|
31
32
|
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
-
rspec-support (~> 3.
|
33
|
-
rspec-support (3.
|
33
|
+
rspec-support (~> 3.9.0)
|
34
|
+
rspec-support (3.9.3)
|
34
35
|
thread_safe (0.3.6)
|
35
|
-
tzinfo (1.2.
|
36
|
+
tzinfo (1.2.7)
|
36
37
|
thread_safe (~> 0.1)
|
38
|
+
zeitwerk (2.3.0)
|
37
39
|
|
38
40
|
PLATFORMS
|
39
41
|
ruby
|
@@ -41,9 +43,12 @@ PLATFORMS
|
|
41
43
|
DEPENDENCIES
|
42
44
|
activesupport
|
43
45
|
bundler (~> 2.0)
|
44
|
-
rake (~>
|
46
|
+
rake (~> 13.0)
|
45
47
|
rspec (~> 3.0)
|
46
48
|
schema2type!
|
47
49
|
|
50
|
+
RUBY VERSION
|
51
|
+
ruby 2.7.0p0
|
52
|
+
|
48
53
|
BUNDLED WITH
|
49
|
-
2.
|
54
|
+
2.1.4
|
data/README.md
CHANGED
@@ -22,12 +22,13 @@ Automatically have the following TypesScript type generated.
|
|
22
22
|
```typescript
|
23
23
|
declare namespace schema {
|
24
24
|
type User = {
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
id: number;
|
26
|
+
name: string | null;
|
27
|
+
age: number | null;
|
28
|
+
sales: number | null;
|
29
|
+
paid: boolean;
|
30
|
+
createdAt: Date;
|
31
|
+
updatedAt: Date;
|
31
32
|
}
|
32
33
|
}
|
33
34
|
```
|
@@ -81,10 +82,10 @@ the schema2type convert as per this conversion table.
|
|
81
82
|
| float | number |
|
82
83
|
| decimal | number |
|
83
84
|
| boolean | boolean |
|
84
|
-
| date |
|
85
|
-
| datetime |
|
86
|
-
| timestamp |
|
87
|
-
| datetime_with_timezone |
|
85
|
+
| date | string |
|
86
|
+
| datetime | string |
|
87
|
+
| timestamp | string |
|
88
|
+
| datetime_with_timezone | string |
|
88
89
|
## License
|
89
90
|
|
90
91
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/exe/schema2type
CHANGED
@@ -5,4 +5,4 @@ require 'schema2type'
|
|
5
5
|
|
6
6
|
params = ARGV.getopts('s:o:n:', 'snake')
|
7
7
|
|
8
|
-
Schema2type.execute(input_file: params[
|
8
|
+
Schema2type.execute(input_file: params['s'], out_file: params['o'], name_space: params['n'], is_snake_case: params['snake'])
|
data/lib/schema2type.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'schema2type/version'
|
2
2
|
require 'schema2type/schema_converter'
|
3
|
-
require
|
4
|
-
|
3
|
+
require 'schema2type/covert_service'
|
4
|
+
require 'schema2type/cli'
|
data/lib/schema2type/cli.rb
CHANGED
@@ -1,15 +1,9 @@
|
|
1
1
|
module Schema2type
|
2
|
+
DEFAULT_SCHEMA_PATH = "./db/schema.rb".freeze
|
3
|
+
DEFAULT_NAME_SPACE = "schema".freeze
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
def self.execute(input_file:, out_file:, name_space:, snake_case:)
|
7
|
-
@@converted_types = []
|
8
|
-
@@snake_case = snake_case
|
9
|
-
|
10
|
-
eval(File.read(input_file || DEFAULT_SCHEMA_PATH))
|
11
|
-
|
12
|
-
convert_type_text = @@converted_types.map { |t| " #{t}" }.join("\n").strip
|
5
|
+
def self.execute(input_file:, out_file:, name_space:, is_snake_case:)
|
6
|
+
resultHash = eval(File.read(input_file || DEFAULT_SCHEMA_PATH), CovertService.new(is_snake_case).get_binding)
|
13
7
|
|
14
8
|
File.open(out_file, "w") do |f|
|
15
9
|
f.puts <<~EOS
|
@@ -17,38 +11,13 @@ module Schema2type
|
|
17
11
|
|
18
12
|
/**
|
19
13
|
* auto-generated file
|
20
|
-
* schema version: #{
|
14
|
+
* schema version: #{resultHash[:version]}
|
21
15
|
* This file was automatically generated by schema2type
|
22
16
|
*/
|
23
17
|
declare namespace #{name_space || DEFAULT_NAME_SPACE} {
|
24
|
-
#{
|
18
|
+
#{resultHash[:lines]}
|
25
19
|
}
|
26
20
|
EOS
|
27
21
|
end
|
28
22
|
end
|
29
|
-
|
30
|
-
def self.convert_schema(table_name, *)
|
31
|
-
converter = SchemaConverter.new(table_name: table_name, snake_case: @@snake_case)
|
32
|
-
|
33
|
-
yield converter
|
34
|
-
|
35
|
-
@@converted_types.concat converter.result
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.method_missing(*arg)
|
39
|
-
# To exclude unnecessary methods
|
40
|
-
end
|
41
|
-
|
42
|
-
module ActiveRecord
|
43
|
-
module Schema
|
44
|
-
def self.define(version)
|
45
|
-
$schema_version = version[:version]
|
46
|
-
yield
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
class << self
|
52
|
-
alias :create_table :convert_schema
|
53
|
-
end
|
54
23
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Schema2type
|
2
|
+
class CovertService
|
3
|
+
def initialize(is_snake_case)
|
4
|
+
@converted_types = []
|
5
|
+
@is_snake_case = is_snake_case
|
6
|
+
end
|
7
|
+
|
8
|
+
def get_binding
|
9
|
+
binding
|
10
|
+
end
|
11
|
+
|
12
|
+
# mock method for create_table in schema.rb
|
13
|
+
def convert_schema_to_type(table_name, *)
|
14
|
+
converter = SchemaConverter.new(table_name: table_name, is_snake_case: @is_snake_case)
|
15
|
+
yield converter
|
16
|
+
@converted_types.concat converter.converted_type_lines
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing(*)
|
20
|
+
# To exclude unnecessary methods
|
21
|
+
# TODO: add error handling
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.method_missing(*)
|
25
|
+
# To exclude unnecessary methods
|
26
|
+
# TODO: add error handling
|
27
|
+
end
|
28
|
+
|
29
|
+
# mock module and method for shcema.rb
|
30
|
+
module ActiveRecord
|
31
|
+
module Schema
|
32
|
+
def self.define(*arg)
|
33
|
+
converted_types = yield
|
34
|
+
converted_type_lines = converted_types.map { |t| " #{t}" }.join("\n").strip
|
35
|
+
{
|
36
|
+
lines: converted_type_lines,
|
37
|
+
version: arg[0][:version]
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
alias create_table convert_schema_to_type
|
44
|
+
end
|
45
|
+
end
|
@@ -3,43 +3,41 @@ require 'active_support/inflector'
|
|
3
3
|
|
4
4
|
module Schema2type
|
5
5
|
class SchemaConverter
|
6
|
-
attr_reader :property_lines, :table_name, :
|
6
|
+
attr_reader :property_lines, :table_name, :is_snake_case
|
7
7
|
|
8
|
-
TYPE_STRING = 'string'.freeze
|
9
|
-
TYPE_NUMBER = 'number'.freeze
|
10
|
-
TYPE_BOOLEAN = 'boolean'.freeze
|
11
|
-
TYPE_DATE = 'Date'.freeze
|
12
8
|
COLUMN_METHODS = YAML.load_file(File.expand_path(__dir__) + '/conversion_table.yml').to_a
|
9
|
+
ID_PROPERTY_LINE_TEXT = " id: number;".freeze
|
13
10
|
|
14
11
|
def self.define_convert_methods(methods)
|
15
12
|
methods.each do |m|
|
16
13
|
define_method(m[0]) do |name, *options|
|
17
|
-
|
14
|
+
convert_property_line_and_push name: name, type: m[1], options: options
|
18
15
|
end
|
19
16
|
end
|
20
17
|
end
|
21
18
|
|
22
19
|
define_convert_methods COLUMN_METHODS
|
23
20
|
|
24
|
-
def initialize(table_name:,
|
21
|
+
def initialize(table_name:, is_snake_case: false)
|
25
22
|
@property_lines = []
|
26
23
|
@table_name = table_name.singularize.camelize
|
27
|
-
@
|
24
|
+
@is_snake_case = is_snake_case
|
28
25
|
end
|
29
26
|
|
30
|
-
def
|
31
|
-
["type #{table_name} = {", property_lines, "}\n"].flatten
|
27
|
+
def converted_type_lines
|
28
|
+
["type #{table_name} = {", ID_PROPERTY_LINE_TEXT, property_lines, "}\n"].flatten
|
32
29
|
end
|
33
30
|
|
34
31
|
def method_missing(*)
|
35
32
|
# To exclude unnecessary methods
|
33
|
+
# TODO: add error handling
|
36
34
|
end
|
37
35
|
|
38
36
|
private
|
39
37
|
|
40
|
-
def
|
38
|
+
def convert_property_line_and_push(name:, type:, options:)
|
41
39
|
is_non_nullable = options[0] && options[0].key?(:null) && !options[0][:null]
|
42
|
-
formatted_name =
|
40
|
+
formatted_name = is_snake_case ? name.underscore : name.camelcase(:lower)
|
43
41
|
property_line = is_non_nullable ? "#{formatted_name}: #{type};" : "#{formatted_name}: #{type} | null;"
|
44
42
|
|
45
43
|
property_lines << " #{property_line}"
|
data/lib/schema2type/version.rb
CHANGED
data/schema2type.gemspec
CHANGED
@@ -24,6 +24,6 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.require_paths = ["lib"]
|
25
25
|
spec.add_runtime_dependency "activesupport"
|
26
26
|
spec.add_development_dependency "bundler", "~> 2.0"
|
27
|
-
spec.add_development_dependency "rake", "~>
|
27
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
28
28
|
spec.add_development_dependency "rspec", "~> 3.0"
|
29
29
|
end
|
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.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ryo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '13.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '13.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -77,6 +77,7 @@ extra_rdoc_files: []
|
|
77
77
|
files:
|
78
78
|
- ".gitignore"
|
79
79
|
- ".rspec"
|
80
|
+
- ".ruby-version"
|
80
81
|
- ".travis.yml"
|
81
82
|
- CODE_OF_CONDUCT.md
|
82
83
|
- Gemfile
|
@@ -90,6 +91,7 @@ files:
|
|
90
91
|
- lib/schema2type.rb
|
91
92
|
- lib/schema2type/cli.rb
|
92
93
|
- lib/schema2type/conversion_table.yml
|
94
|
+
- lib/schema2type/covert_service.rb
|
93
95
|
- lib/schema2type/schema_converter.rb
|
94
96
|
- lib/schema2type/version.rb
|
95
97
|
- schema2type.gemspec
|
@@ -112,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
114
|
- !ruby/object:Gem::Version
|
113
115
|
version: '0'
|
114
116
|
requirements: []
|
115
|
-
rubygems_version: 3.
|
117
|
+
rubygems_version: 3.1.2
|
116
118
|
signing_key:
|
117
119
|
specification_version: 4
|
118
120
|
summary: generate TypeScript type definitions from Rails schema.rb
|