ts_schema 0.1.10 → 0.1.11
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/Rakefile +1 -0
- data/lib/tasks/ts_schema_tasks.rake +6 -13
- data/lib/ts_schema/configuration.rb +0 -1
- data/lib/ts_schema/conversion_table.yml +0 -2
- data/lib/ts_schema/railtie.rb +3 -0
- data/lib/ts_schema/schema_generator.rb +40 -18
- data/lib/ts_schema/version.rb +1 -1
- data/lib/ts_schema.rb +11 -2
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55a01d015705b45bc3b21d80c74fde717172daf0b9cc8f8856ee24bdb6ffe00d
|
4
|
+
data.tar.gz: 662b58cf50169869845249b218ad760d7f3e4d96b5e833ae916a77a86e8447dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f7ceb576b082e209567755d34c7ce25f826ec61891e3f61662f06289838b087c2d18b61f8fdcbabe4db097f59af90332ac55b861b25242a0cc20bc809ad8e9d
|
7
|
+
data.tar.gz: 785331c128754a645c9570146244fc01992329ea7864dc40b3295a587f67ce07bacfff3320a6e9a673552eff4f5489c891028715d7675be5965275a5598eb43b
|
data/Rakefile
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
require "rake"
|
2
|
+
require "ts_schema"
|
2
3
|
|
3
4
|
namespace :ts_schema do
|
4
5
|
desc "Generates a schema file in the default javascripts location, or the location specified in the ts_config initializer options"
|
5
|
-
task :
|
6
|
+
task generate: :environment do
|
6
7
|
TsSchema.output_file
|
7
8
|
end
|
8
9
|
end
|
@@ -12,19 +13,11 @@ namespace :db do
|
|
12
13
|
TsSchema.output_file if TsSchema.configuration.auto_generate
|
13
14
|
end
|
14
15
|
|
15
|
-
task migrate
|
16
|
-
auto_generate_and_save_file
|
16
|
+
task :migrate do
|
17
|
+
at_exit { auto_generate_and_save_file }
|
17
18
|
end
|
18
19
|
|
19
|
-
task
|
20
|
-
auto_generate_and_save_file
|
21
|
-
end
|
22
|
-
|
23
|
-
task reset: :environment do
|
24
|
-
auto_generate_and_save_file
|
25
|
-
end
|
26
|
-
|
27
|
-
task setup: :environment do
|
28
|
-
auto_generate_and_save_file
|
20
|
+
task :setup do
|
21
|
+
at_exit { auto_generate_and_save_file }
|
29
22
|
end
|
30
23
|
end
|
@@ -2,7 +2,6 @@ string: string
|
|
2
2
|
text: string
|
3
3
|
integer: number
|
4
4
|
enum: number
|
5
|
-
bigint: number
|
6
5
|
float: number
|
7
6
|
decimal: number
|
8
7
|
json: Record<string, any>
|
@@ -12,7 +11,6 @@ boolean: boolean
|
|
12
11
|
date: string
|
13
12
|
datetime: string
|
14
13
|
timestamp: string
|
15
|
-
datetime_with_timezone: string
|
16
14
|
inet: string
|
17
15
|
cidr: string
|
18
16
|
macaddr: string
|
data/lib/ts_schema/railtie.rb
CHANGED
@@ -2,13 +2,31 @@ require 'fileutils'
|
|
2
2
|
|
3
3
|
module TsSchema
|
4
4
|
class SchemaGenerator
|
5
|
-
|
5
|
+
attr_accessor :config
|
6
|
+
attr_accessor :models
|
7
|
+
|
8
|
+
def initialize(config = nil)
|
9
|
+
@config = config || TsSchema::Configuration.new
|
10
|
+
|
6
11
|
Rails.application.eager_load!
|
7
|
-
@models = ApplicationRecord.send(:subclasses)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
+
# @models = ApplicationRecord.send(:subclasses)
|
13
|
+
@models = get_subclasses(ApplicationRecord)
|
14
|
+
unless @config.additional_models.empty?
|
15
|
+
@models.concat(@config.additional_models.map do |m|
|
16
|
+
m.to_s.constantize
|
17
|
+
end)
|
18
|
+
end
|
19
|
+
@types = @config.types.stringify_keys.merge(@config.custom_types.stringify_keys || {})
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_subclasses(model)
|
23
|
+
models = model.send(:subclasses)
|
24
|
+
unless nested_empty?(models)
|
25
|
+
models.concat(models.flat_map do |m|
|
26
|
+
get_subclasses(m)
|
27
|
+
end)
|
28
|
+
end
|
29
|
+
models
|
12
30
|
end
|
13
31
|
|
14
32
|
def generate
|
@@ -16,10 +34,10 @@ module TsSchema
|
|
16
34
|
end
|
17
35
|
|
18
36
|
def output_file
|
19
|
-
path =
|
37
|
+
path = @config.output
|
20
38
|
FileUtils.mkdir_p(File.dirname(path))
|
21
39
|
|
22
|
-
|
40
|
+
content = generate
|
23
41
|
return if File.exist?(path) && File.read(path) == content
|
24
42
|
|
25
43
|
File.open(path, 'w') do |f|
|
@@ -27,13 +45,11 @@ module TsSchema
|
|
27
45
|
end
|
28
46
|
end
|
29
47
|
|
30
|
-
private
|
31
|
-
|
32
48
|
def generate_typescript
|
33
49
|
type_template = ""
|
34
50
|
@models.each do |model|
|
35
51
|
columns = map_column_types(model)
|
36
|
-
columns.concat(map_associations(model)) if
|
52
|
+
columns.concat(map_associations(model)) if @config.include_associated
|
37
53
|
|
38
54
|
type_template += <<~TYPESCRIPT
|
39
55
|
interface #{model.model_name.param_key.camelize} {
|
@@ -42,7 +58,7 @@ module TsSchema
|
|
42
58
|
TYPESCRIPT
|
43
59
|
end
|
44
60
|
type_template = <<~TPL
|
45
|
-
declare namespace #{
|
61
|
+
declare namespace #{@config.namespace} {
|
46
62
|
#{indent_wrapper(type_template)}
|
47
63
|
}
|
48
64
|
TPL
|
@@ -50,7 +66,7 @@ module TsSchema
|
|
50
66
|
|
51
67
|
def map_column_types(model)
|
52
68
|
model.columns.map { |i|
|
53
|
-
type = @types[i.type.to_s] ||
|
69
|
+
type = @types[i.type.to_s] || @config.default_type
|
54
70
|
|
55
71
|
if(enum = model.defined_enums[i.name])
|
56
72
|
type = enum.keys.map { |k| "'#{k}'" }.join(" | ")
|
@@ -80,8 +96,14 @@ module TsSchema
|
|
80
96
|
end
|
81
97
|
end
|
82
98
|
|
99
|
+
private
|
100
|
+
|
101
|
+
def nested_empty?(arr)
|
102
|
+
arr.is_a?(Array) && arr.flatten.empty?
|
103
|
+
end
|
104
|
+
|
83
105
|
def column_name_cased(name)
|
84
|
-
case
|
106
|
+
case @config.case.to_sym
|
85
107
|
when :camel
|
86
108
|
name.camelize(:lower)
|
87
109
|
when :pascal
|
@@ -92,18 +114,18 @@ module TsSchema
|
|
92
114
|
end
|
93
115
|
|
94
116
|
def indent_as_str
|
95
|
-
case
|
117
|
+
case @config.indent.to_sym
|
96
118
|
when :space || :spaces
|
97
|
-
"".rjust(
|
119
|
+
"".rjust(@config.spaces, " ")
|
98
120
|
else
|
99
121
|
"\t"
|
100
122
|
end
|
101
123
|
end
|
102
124
|
|
103
125
|
def indent_wrapper(str)
|
104
|
-
case
|
126
|
+
case @config.indent.to_sym
|
105
127
|
when :space || :spaces
|
106
|
-
str.indent(
|
128
|
+
str.indent(@config.spaces)
|
107
129
|
else
|
108
130
|
str.indent(1, "\t")
|
109
131
|
end
|
data/lib/ts_schema/version.rb
CHANGED
data/lib/ts_schema.rb
CHANGED
@@ -4,6 +4,7 @@ require "ts_schema/railtie"
|
|
4
4
|
require "ts_schema/configuration"
|
5
5
|
require "ts_schema/schema_generator"
|
6
6
|
|
7
|
+
|
7
8
|
module TsSchema
|
8
9
|
class << self
|
9
10
|
def setup(&block)
|
@@ -15,11 +16,19 @@ module TsSchema
|
|
15
16
|
end
|
16
17
|
|
17
18
|
def generate
|
18
|
-
|
19
|
+
if ActiveRecord::Base.connection.migration_context.needs_migration?
|
20
|
+
puts "Aborting: There are pending migrations"
|
21
|
+
else
|
22
|
+
SchemaGenerator.new(@configuration).generate
|
23
|
+
end
|
19
24
|
end
|
20
25
|
|
21
26
|
def output_file
|
22
|
-
|
27
|
+
if ActiveRecord::Base.connection.migration_context.needs_migration?
|
28
|
+
puts "Aborting: There are pending migrations"
|
29
|
+
else
|
30
|
+
SchemaGenerator.new(@configuration).output_file
|
31
|
+
end
|
23
32
|
end
|
24
33
|
end
|
25
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ts_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Avram Walden
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-04-
|
11
|
+
date: 2022-04-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 6.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: railties
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4'
|
27
41
|
description: ''
|
28
42
|
email:
|
29
43
|
- aviemet@gmail.com
|