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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5093baca17f4486d16008b5e3eb15dde18d5064b902a45c0d05defe5948f0ee
4
- data.tar.gz: 14d0f1352c9c716af3040814447c8aac81a3cdb66b04870fe698c5f9fa1c98f9
3
+ metadata.gz: 55a01d015705b45bc3b21d80c74fde717172daf0b9cc8f8856ee24bdb6ffe00d
4
+ data.tar.gz: 662b58cf50169869845249b218ad760d7f3e4d96b5e833ae916a77a86e8447dd
5
5
  SHA512:
6
- metadata.gz: b8425b8aefc7433b1360b6fb703ae9d0664513d6f6396e347f2b4c1e7d2345daf2a5047779c576cd070187c34fe87edd0da8aba6947dee644951c01e89fe2fbf
7
- data.tar.gz: 146ce2d710732da6f36cad8d4c9a9ff4d9fb95f186247c937441719951539517d8705184b8b151f42446a413b20f0b19f2dde0e79476b29d96c69aa62271aadc
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,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 :generate do
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: :environment do
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 rollback: :environment do
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
@@ -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,13 +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
- unless TsSchema.configuration.additional_models.empty?
9
- @models.concat(TsSchema.configuration.additional_models.map {|m| m.to_s.constantize })
10
- end
11
- @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
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 = TsSchema.configuration.output
37
+ path = @config.output
20
38
  FileUtils.mkdir_p(File.dirname(path))
21
39
 
22
- content = generate
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 TsSchema.configuration.include_associated
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 #{TsSchema.configuration.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] || TsSchema.configuration.default_type
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 TsSchema.configuration.case.to_sym
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 TsSchema.configuration.indent.to_sym
117
+ case @config.indent.to_sym
96
118
  when :space || :spaces
97
- "".rjust(TsSchema.configuration.spaces, " ")
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 TsSchema.configuration.indent.to_sym
126
+ case @config.indent.to_sym
105
127
  when :space || :spaces
106
- str.indent(TsSchema.configuration.spaces)
128
+ str.indent(@config.spaces)
107
129
  else
108
130
  str.indent(1, "\t")
109
131
  end
@@ -1,3 +1,3 @@
1
1
  module TsSchema
2
- VERSION = "0.1.10"
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,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.10
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-04 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
@@ -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