ts_schema 0.1.8 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a0e244fe1ac81f5655895e0da6b4ea1cb69cbdaffdd7e512e5e1466edd1b611
4
- data.tar.gz: c5147d2c34af1fc0c3f7bdfc7ab07932b3cbb04ada1bbd905c9c7333749b7c76
3
+ metadata.gz: 55a01d015705b45bc3b21d80c74fde717172daf0b9cc8f8856ee24bdb6ffe00d
4
+ data.tar.gz: 662b58cf50169869845249b218ad760d7f3e4d96b5e833ae916a77a86e8447dd
5
5
  SHA512:
6
- metadata.gz: b7ac3e54cdaf9279babf3d1cf695c0fc57f881518c7d34c951ae1fed49c90e4a5998cefe7b734dcb22d18ee8b48303b511c22374a3bf778dee3d3f9370f574a4
7
- data.tar.gz: c38bb7bc65d6768c0e4c76dfe0d0a5e52dfac700ae63a1cb45ac7e99b9caf535a92c44f2c2b9033e1d4e06c2781541b4232cb520df60329844fa14eaf272148a
6
+ metadata.gz: 5f7ceb576b082e209567755d34c7ce25f826ec61891e3f61662f06289838b087c2d18b61f8fdcbabe4db097f59af90332ac55b861b25242a0cc20bc809ad8e9d
7
+ data.tar.gz: 785331c128754a645c9570146244fc01992329ea7864dc40b3295a587f67ce07bacfff3320a6e9a673552eff4f5489c891028715d7675be5965275a5598eb43b
data/Rakefile CHANGED
@@ -1,2 +1,3 @@
1
1
  require "bundler/setup"
2
2
  require "bundler/gem_tasks"
3
+ require "ts_schema"
@@ -1,30 +1,23 @@
1
1
  require "rake"
2
+ require "ts_schema"
2
3
 
3
4
  namespace :ts_schema do
4
- desc "Generates a schema file in the default javascripts location, or the location specified in the ts_config initializer options"
5
- task :generate do
6
- TsSchema.output_file
7
- end
5
+ desc "Generates a schema file in the default javascripts location, or the location specified in the ts_config initializer options"
6
+ task generate: :environment do
7
+ TsSchema.output_file
8
+ end
8
9
  end
9
10
 
10
11
  namespace :db do
11
- def auto_generate_and_save_file
12
- TsSchema.output_file if TsSchema.configuration.auto_generate
13
- end
14
-
15
- task migrate: :environment do
16
- auto_generate_and_save_file
17
- end
18
-
19
- task rollback: :environment do
20
- auto_generate_and_save_file
12
+ def auto_generate_and_save_file
13
+ TsSchema.output_file if TsSchema.configuration.auto_generate
21
14
  end
22
15
 
23
- task reset: :environment do
24
- auto_generate_and_save_file
16
+ task :migrate do
17
+ at_exit { auto_generate_and_save_file }
25
18
  end
26
19
 
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
@@ -37,6 +37,5 @@ module TsSchema
37
37
  end
38
38
  self
39
39
  end
40
-
41
40
  end
42
41
  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
@@ -1,4 +1,7 @@
1
+ require "rails"
2
+
1
3
  module TsSchema
2
4
  class Railtie < ::Rails::Railtie
5
+ load "tasks/ts_schema_tasks.rake"
3
6
  end
4
7
  end
@@ -2,11 +2,31 @@ require 'fileutils'
2
2
 
3
3
  module TsSchema
4
4
  class SchemaGenerator
5
- def initialize
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
- @models.concat(TsSchema.configuration.additional_models) unless TsSchema.configuration.additional_models.empty?
9
- @types = TsSchema.configuration.types.merge(TsSchema.configuration.custom_types || {})
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
10
30
  end
11
31
 
12
32
  def generate
@@ -14,10 +34,10 @@ module TsSchema
14
34
  end
15
35
 
16
36
  def output_file
17
- path = TsSchema.configuration.output
37
+ path = @config.output
18
38
  FileUtils.mkdir_p(File.dirname(path))
19
39
 
20
- content = generate
40
+ content = generate
21
41
  return if File.exist?(path) && File.read(path) == content
22
42
 
23
43
  File.open(path, 'w') do |f|
@@ -25,13 +45,11 @@ module TsSchema
25
45
  end
26
46
  end
27
47
 
28
- private
29
-
30
48
  def generate_typescript
31
49
  type_template = ""
32
50
  @models.each do |model|
33
51
  columns = map_column_types(model)
34
- columns.concat(map_associations(model)) if TsSchema.configuration.include_associated
52
+ columns.concat(map_associations(model)) if @config.include_associated
35
53
 
36
54
  type_template += <<~TYPESCRIPT
37
55
  interface #{model.model_name.param_key.camelize} {
@@ -40,7 +58,7 @@ module TsSchema
40
58
  TYPESCRIPT
41
59
  end
42
60
  type_template = <<~TPL
43
- declare namespace #{TsSchema.configuration.namespace} {
61
+ declare namespace #{@config.namespace} {
44
62
  #{indent_wrapper(type_template)}
45
63
  }
46
64
  TPL
@@ -48,7 +66,7 @@ module TsSchema
48
66
 
49
67
  def map_column_types(model)
50
68
  model.columns.map { |i|
51
- type = @types[i.type.to_s] || TsSchema.configuration.default_type
69
+ type = @types[i.type.to_s] || @config.default_type
52
70
 
53
71
  if(enum = model.defined_enums[i.name])
54
72
  type = enum.keys.map { |k| "'#{k}'" }.join(" | ")
@@ -78,8 +96,14 @@ module TsSchema
78
96
  end
79
97
  end
80
98
 
99
+ private
100
+
101
+ def nested_empty?(arr)
102
+ arr.is_a?(Array) && arr.flatten.empty?
103
+ end
104
+
81
105
  def column_name_cased(name)
82
- case TsSchema.configuration.case.to_sym
106
+ case @config.case.to_sym
83
107
  when :camel
84
108
  name.camelize(:lower)
85
109
  when :pascal
@@ -90,18 +114,18 @@ module TsSchema
90
114
  end
91
115
 
92
116
  def indent_as_str
93
- case TsSchema.configuration.indent.to_sym
117
+ case @config.indent.to_sym
94
118
  when :space || :spaces
95
- "".rjust(TsSchema.configuration.spaces, " ")
119
+ "".rjust(@config.spaces, " ")
96
120
  else
97
121
  "\t"
98
122
  end
99
123
  end
100
124
 
101
125
  def indent_wrapper(str)
102
- case TsSchema.configuration.indent.to_sym
126
+ case @config.indent.to_sym
103
127
  when :space || :spaces
104
- str.indent(TsSchema.configuration.spaces)
128
+ str.indent(@config.spaces)
105
129
  else
106
130
  str.indent(1, "\t")
107
131
  end
@@ -1,3 +1,3 @@
1
1
  module TsSchema
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.11"
3
3
  end
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
- SchemaGenerator.new.generate
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
- SchemaGenerator.new.output_file
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,35 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ts_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
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-02 00:00:00.000000000 Z
11
+ date: 2022-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '7.0'
20
17
  - - ">="
21
18
  - !ruby/object:Gem::Version
22
- version: 7.0.2.3
19
+ version: 6.0.0
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - "~>"
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: '7.0'
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:
30
38
  - - ">="
31
39
  - !ruby/object:Gem::Version
32
- version: 7.0.2.3
40
+ version: '4'
33
41
  description: ''
34
42
  email:
35
43
  - aviemet@gmail.com