schema2type 0.1.12 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +6 -5
- data/lib/schema2type/cli.rb +30 -22
- data/lib/schema2type/schema_converter.rb +14 -16
- data/lib/schema2type/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9c3e52c8948ce5f2642167c3fe437b480c2a8ec47e83c1cf9c25a2e555ee205b
|
|
4
|
+
data.tar.gz: 38d1d525a6227191010dd1eb280c5fa10781df2bce44ae9b86b839dc98e5174e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cba2e579992d67be775d58d2b33a9aa63161689e10a1dc2ddbd3ddbf1b7709128ffa899cb9db732220f0965a3947808d35fbf7c1256e88d426dcf5e1c145a78f
|
|
7
|
+
data.tar.gz: a5c22fe47056ac52ce8cfd4d40965950a50e575d41bcc5d0d85adbdaa9701fed96cda6609cc42d2b66a5d86c69b71d23e41817873424f908def7029f06a6cab1
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -17,7 +17,7 @@ ActiveRecord::Schema.define(version: xxxx) do
|
|
|
17
17
|
end
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
Automatically have the following TypesScript type generated
|
|
20
|
+
Automatically have the following TypesScript type generated.
|
|
21
21
|
|
|
22
22
|
```typescript
|
|
23
23
|
declare namespace schema {
|
|
@@ -53,19 +53,20 @@ Or install it yourself as:
|
|
|
53
53
|
## Usage
|
|
54
54
|
|
|
55
55
|
```
|
|
56
|
-
bundle exec schema2type -
|
|
56
|
+
bundle exec schema2type -o schema.d.ts
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
### options
|
|
60
60
|
|
|
61
61
|
|command | require | default | detail |
|
|
62
62
|
|---|---|---|---|
|
|
63
|
-
| -s | true | - | Path of your schema.rb |
|
|
64
63
|
| -o | true | - | Output file name of TypeScript |
|
|
64
|
+
| -s | false | "./db/schema.rb" | Path of your schema.rb |
|
|
65
65
|
| -n | false | "schema" | Name of declare namespace |
|
|
66
66
|
| --snake | false | false | Convert property name to snake_case |
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
## conversion table
|
|
69
|
+
the schema2type convert as per this conversion table.
|
|
69
70
|
|
|
70
71
|
|create_table block method| converted Type|
|
|
71
72
|
|---|---|
|
data/lib/schema2type/cli.rb
CHANGED
|
@@ -1,34 +1,38 @@
|
|
|
1
1
|
module Schema2type
|
|
2
2
|
|
|
3
|
+
DEFAULT_SCHEMA_PATH = "./db/schema.rb"
|
|
4
|
+
DEFAULT_NAME_SPACE = "schema"
|
|
5
|
+
|
|
3
6
|
def self.execute(input_file:, out_file:, name_space:, snake_case:)
|
|
4
|
-
@@
|
|
7
|
+
@@converted_types = []
|
|
5
8
|
@@snake_case = snake_case
|
|
6
|
-
eval(File.read(input_file))
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
eval(File.read(input_file || DEFAULT_SCHEMA_PATH))
|
|
11
|
+
|
|
12
|
+
convert_type_text = @@converted_types.map { |t| " #{t}" }.join("\n").strip
|
|
9
13
|
|
|
10
14
|
File.open(out_file, "w") do |f|
|
|
11
|
-
f.puts
|
|
12
|
-
/* eslint no-unused-vars: 0 */
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
declare namespace #{name_space
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
EOS
|
|
15
|
+
f.puts <<~EOS
|
|
16
|
+
/* eslint no-unused-vars: 0 */
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* auto-generated file
|
|
20
|
+
* schema version: #{$schema_version}
|
|
21
|
+
* This file was automatically generated by schema2type
|
|
22
|
+
*/
|
|
23
|
+
declare namespace #{name_space || DEFAULT_NAME_SPACE} {
|
|
24
|
+
#{convert_type_text}
|
|
25
|
+
}
|
|
26
|
+
EOS
|
|
23
27
|
end
|
|
24
28
|
end
|
|
25
29
|
|
|
26
|
-
def self.
|
|
30
|
+
def self.convert_schema(table_name, *)
|
|
27
31
|
converter = SchemaConverter.new(table_name: table_name, snake_case: @@snake_case)
|
|
28
|
-
block.call(converter)
|
|
29
|
-
converter.finalize
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
yield converter
|
|
34
|
+
|
|
35
|
+
@@converted_types.concat converter.result
|
|
32
36
|
end
|
|
33
37
|
|
|
34
38
|
def self.method_missing(*arg)
|
|
@@ -36,11 +40,15 @@ EOS
|
|
|
36
40
|
end
|
|
37
41
|
|
|
38
42
|
module ActiveRecord
|
|
39
|
-
|
|
40
|
-
def self.define(version
|
|
43
|
+
module Schema
|
|
44
|
+
def self.define(version)
|
|
41
45
|
$schema_version = version[:version]
|
|
42
|
-
|
|
46
|
+
yield
|
|
43
47
|
end
|
|
44
48
|
end
|
|
45
49
|
end
|
|
50
|
+
|
|
51
|
+
class << self
|
|
52
|
+
alias :create_table :convert_schema
|
|
53
|
+
end
|
|
46
54
|
end
|
|
@@ -3,8 +3,7 @@ require 'active_support/inflector'
|
|
|
3
3
|
|
|
4
4
|
module Schema2type
|
|
5
5
|
class SchemaConverter
|
|
6
|
-
|
|
7
|
-
attr_reader :table_name, :convertion_table, :snake_case
|
|
6
|
+
attr_reader :property_lines, :table_name, :convertion_table, :snake_case
|
|
8
7
|
|
|
9
8
|
TYPE_STRING = 'string'.freeze
|
|
10
9
|
TYPE_NUMBER = 'number'.freeze
|
|
@@ -12,17 +11,6 @@ module Schema2type
|
|
|
12
11
|
TYPE_DATE = 'Date'.freeze
|
|
13
12
|
COLUMN_METHODS = YAML.load_file(File.expand_path(__dir__) + '/conversion_table.yml').to_a
|
|
14
13
|
|
|
15
|
-
def initialize(table_name:, snake_case: false)
|
|
16
|
-
@out_text = []
|
|
17
|
-
@table_name = table_name.singularize.camelize
|
|
18
|
-
@snake_case = snake_case
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def finalize
|
|
22
|
-
@out_text.unshift "type #{@table_name} = {"
|
|
23
|
-
@out_text << "}\n"
|
|
24
|
-
end
|
|
25
|
-
|
|
26
14
|
def self.define_convert_methods(methods)
|
|
27
15
|
methods.each do |m|
|
|
28
16
|
define_method(m[0]) do |name, *options|
|
|
@@ -33,7 +21,17 @@ module Schema2type
|
|
|
33
21
|
|
|
34
22
|
define_convert_methods COLUMN_METHODS
|
|
35
23
|
|
|
36
|
-
def
|
|
24
|
+
def initialize(table_name:, snake_case: false)
|
|
25
|
+
@property_lines = []
|
|
26
|
+
@table_name = table_name.singularize.camelize
|
|
27
|
+
@snake_case = snake_case
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def result
|
|
31
|
+
["type #{table_name} = {", property_lines, "}\n"].flatten
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def method_missing(*)
|
|
37
35
|
# To exclude unnecessary methods
|
|
38
36
|
end
|
|
39
37
|
|
|
@@ -41,10 +39,10 @@ module Schema2type
|
|
|
41
39
|
|
|
42
40
|
def push_property_line(name:, type:, options:)
|
|
43
41
|
is_non_nullable = options[0] && options[0].key?(:null) && !options[0][:null]
|
|
44
|
-
formatted_name =
|
|
42
|
+
formatted_name = snake_case ? name.underscore : name.camelcase(:lower)
|
|
45
43
|
property_line = is_non_nullable ? "#{formatted_name}: #{type};" : "#{formatted_name}: #{type} | null;"
|
|
46
44
|
|
|
47
|
-
|
|
45
|
+
property_lines << " #{property_line}"
|
|
48
46
|
end
|
|
49
47
|
end
|
|
50
48
|
end
|
data/lib/schema2type/version.rb
CHANGED