k_domain 0.0.1 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +39 -1
- data/Gemfile +10 -0
- data/Guardfile +30 -0
- data/Rakefile +3 -4
- data/STORIES.md +63 -0
- data/USAGE.md +19 -0
- data/k_domain.gemspec +4 -0
- data/lib/k_domain/domain_model/load.rb +29 -0
- data/lib/k_domain/domain_model/transform.rb +110 -0
- data/lib/k_domain/domain_model/transform_steps/_.rb +10 -0
- data/lib/k_domain/domain_model/transform_steps/step.rb +142 -0
- data/lib/k_domain/domain_model/transform_steps/step1_attach_db_schema.rb +21 -0
- data/lib/k_domain/domain_model/transform_steps/step2_attach_models.rb +62 -0
- data/lib/k_domain/domain_model/transform_steps/step3_attach_columns.rb +137 -0
- data/lib/k_domain/domain_model/transform_steps/step4_attach_erd_files.rb +454 -0
- data/lib/k_domain/domain_model/transform_steps/step5_attach_dictionary.rb +58 -0
- data/lib/k_domain/domain_model/transform_steps/step8_locate_rails_models.rb +44 -0
- data/lib/k_domain/raw_db_schema/load.rb +29 -0
- data/lib/k_domain/raw_db_schema/transform.rb +82 -0
- data/lib/k_domain/schemas/_.rb +15 -0
- data/lib/k_domain/schemas/database/_.rb +7 -0
- data/lib/k_domain/schemas/database/foreign_key.rb +14 -0
- data/lib/k_domain/schemas/database/index.rb +14 -0
- data/lib/k_domain/schemas/database/schema.rb +31 -0
- data/lib/k_domain/schemas/database/table.rb +32 -0
- data/lib/k_domain/schemas/dictionary.rb +19 -0
- data/lib/k_domain/schemas/domain/_.rb +65 -0
- data/lib/k_domain/schemas/domain/domain.rb +11 -0
- data/lib/k_domain/schemas/domain/erd_file.rb +80 -0
- data/lib/k_domain/schemas/domain/models/column.rb +49 -0
- data/lib/k_domain/schemas/domain/models/model.rb +111 -0
- data/lib/k_domain/schemas/domain/old/belongs_to.rb +25 -0
- data/lib/k_domain/schemas/domain/old/column_old.rb +225 -0
- data/lib/k_domain/schemas/domain/old/domain_statistics.rb +29 -0
- data/lib/k_domain/schemas/domain/old/entity.rb +338 -0
- data/lib/k_domain/schemas/domain/old/entity_statistics.rb +22 -0
- data/lib/k_domain/schemas/domain/old/foreign_key.rb +17 -0
- data/lib/k_domain/schemas/domain/old/has_and_belongs_to_many.rb +20 -0
- data/lib/k_domain/schemas/domain/old/has_many.rb +27 -0
- data/lib/k_domain/schemas/domain/old/has_one.rb +41 -0
- data/lib/k_domain/schemas/domain/old/name_options.rb +10 -0
- data/lib/k_domain/schemas/domain/old/rails_controller.rb +10 -0
- data/lib/k_domain/schemas/domain/old/rails_model.rb +92 -0
- data/lib/k_domain/schemas/domain/old/related_entity.rb +36 -0
- data/lib/k_domain/schemas/domain/old/statistics.rb +21 -0
- data/lib/k_domain/schemas/domain/old/validate.rb +25 -0
- data/lib/k_domain/schemas/domain/old/validates.rb +50 -0
- data/lib/k_domain/schemas/domain_model.rb +14 -0
- data/lib/k_domain/schemas/investigate.rb +15 -0
- data/lib/k_domain/schemas/rails_resource.rb +16 -0
- data/lib/k_domain/version.rb +1 -1
- data/lib/k_domain.rb +22 -1
- data/templates/load_schema.rb +226 -0
- metadata +91 -2
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Annotates the original schema with methods that implement existing method calls
|
4
|
+
# that are already in the schema so that we can build a hash.
|
5
|
+
#
|
6
|
+
# Writes a new annotated schema.rb file with a public method called load that
|
7
|
+
# builds the hash
|
8
|
+
|
9
|
+
module KDomain
|
10
|
+
module RawDbSchema
|
11
|
+
class Load
|
12
|
+
include KLog::Logging
|
13
|
+
|
14
|
+
attr_reader :source_file
|
15
|
+
attr_reader :data
|
16
|
+
|
17
|
+
def initialize(source_file)
|
18
|
+
@source_file = source_file
|
19
|
+
end
|
20
|
+
|
21
|
+
def call
|
22
|
+
json = File.read(source_file)
|
23
|
+
data = KUtil.data.json_parse(json, as: :hash_symbolized)
|
24
|
+
|
25
|
+
@data = KDomain::Database::Schema.new(data)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Annotates the original schema with methods that implement existing method calls
|
4
|
+
# that are already in the schema so that we can build a hash.
|
5
|
+
#
|
6
|
+
# Writes a new annotated schema.rb file with a public method called load that
|
7
|
+
# builds the hash
|
8
|
+
module KDomain
|
9
|
+
module RawDbSchema
|
10
|
+
class Transform
|
11
|
+
include KLog::Logging
|
12
|
+
|
13
|
+
attr_reader :source_file
|
14
|
+
attr_reader :template_file
|
15
|
+
attr_reader :schema_loader
|
16
|
+
|
17
|
+
def initialize(source_file)
|
18
|
+
@source_file = source_file
|
19
|
+
@template_file = KDomain::Gem.resource('templates/load_schema.rb')
|
20
|
+
end
|
21
|
+
|
22
|
+
def call
|
23
|
+
# log.kv 'source_file', source_file
|
24
|
+
# log.kv 'template_file', template_file
|
25
|
+
# log.kv 'source_file?', File.exist?(source_file)
|
26
|
+
# log.kv 'template_file?', File.exist?(template_file)
|
27
|
+
log.error "Template not found: #{template_file}" unless File.exist?(template_file)
|
28
|
+
|
29
|
+
content = File.read(source_file)
|
30
|
+
content
|
31
|
+
.gsub!(/ActiveRecord::Schema.define/, 'load')
|
32
|
+
|
33
|
+
lines = content.lines.map { |line| " #{line}" }.join
|
34
|
+
|
35
|
+
@schema_loader = File
|
36
|
+
.read(template_file)
|
37
|
+
.gsub('{{source_file}}', source_file)
|
38
|
+
.gsub('{{rails_schema}}', lines)
|
39
|
+
end
|
40
|
+
|
41
|
+
# rename to target_ruby
|
42
|
+
# This is an internal ruby structure that is evaluated
|
43
|
+
# writing is only needed for debugging purposes
|
44
|
+
def write_schema_loader(target_file)
|
45
|
+
if schema_loader.nil?
|
46
|
+
puts '.call method has not been executed'
|
47
|
+
return
|
48
|
+
end
|
49
|
+
|
50
|
+
FileUtils.mkdir_p(File.dirname(target_file))
|
51
|
+
File.write(target_file, schema_loader)
|
52
|
+
end
|
53
|
+
|
54
|
+
def write_json(json_file)
|
55
|
+
if schema_loader.nil?
|
56
|
+
puts '.call method has not been executed'
|
57
|
+
return
|
58
|
+
end
|
59
|
+
|
60
|
+
FileUtils.mkdir_p(File.dirname(json_file))
|
61
|
+
File.write(json_file, JSON.pretty_generate(schema))
|
62
|
+
end
|
63
|
+
|
64
|
+
# rubocop:disable Security/Eval
|
65
|
+
def schema
|
66
|
+
if schema_loader.nil?
|
67
|
+
puts '.call method has not been executed'
|
68
|
+
return
|
69
|
+
end
|
70
|
+
|
71
|
+
eval(schema_loader)#, __FILE__, __LINE__)
|
72
|
+
|
73
|
+
loader = LoadSchema.new
|
74
|
+
loader.load_schema
|
75
|
+
return loader.schema
|
76
|
+
rescue => ex
|
77
|
+
log.exception(ex)
|
78
|
+
end
|
79
|
+
# rubocop:enable Security/Eval
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# log.warn 'models->domain' if AppDebug.require?
|
4
|
+
|
5
|
+
module Types
|
6
|
+
include Dry.Types()
|
7
|
+
end
|
8
|
+
|
9
|
+
require_relative 'rails_resource'
|
10
|
+
require_relative 'investigate'
|
11
|
+
require_relative 'database/_'
|
12
|
+
require_relative 'dictionary'
|
13
|
+
require_relative 'domain/_'
|
14
|
+
|
15
|
+
require_relative './domain_model'
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDomain
|
4
|
+
module Database
|
5
|
+
class ForeignKey < Dry::Struct
|
6
|
+
attribute :left , Types::Strict::String
|
7
|
+
attribute :right , Types::Strict::String
|
8
|
+
attribute :name? , Types::Strict::String.optional.default(nil)
|
9
|
+
attribute :on_update? , Types::Strict::String.optional.default(nil)
|
10
|
+
attribute :on_delete? , Types::Strict::String.optional.default(nil)
|
11
|
+
attribute :column? , Types::Strict::String.optional.default(nil)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDomain
|
4
|
+
module Database
|
5
|
+
class Index < Dry::Struct
|
6
|
+
attribute :name , Types::Strict::String
|
7
|
+
attribute :fields , Types::Nominal::Any.optional.default('xxxxx1')
|
8
|
+
attribute :using , Types::Nominal::String
|
9
|
+
attribute :order? , Types::Nominal::Hash
|
10
|
+
attribute :where? , Types::Nominal::Any.optional.default(nil)
|
11
|
+
attribute :unique? , Types::Nominal::Any.optional.default(nil)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDomain
|
4
|
+
module Database
|
5
|
+
class Schema < Dry::Struct
|
6
|
+
class DbInfo < Dry::Struct
|
7
|
+
attribute :type , Types::Strict::String
|
8
|
+
attribute :version , Types::Nominal::Any.optional.default(nil)
|
9
|
+
attribute :extensions , Types::Strict::Array
|
10
|
+
end
|
11
|
+
|
12
|
+
class UniqueKey < Dry::Struct
|
13
|
+
attribute :type , Types::Strict::String
|
14
|
+
attribute :category , Types::Strict::String.optional
|
15
|
+
attribute :key , Types::Strict::String
|
16
|
+
attribute :keys , Types::Strict::Array
|
17
|
+
end
|
18
|
+
|
19
|
+
class Meta < Dry::Struct
|
20
|
+
attribute :rails , Types::Strict::Integer
|
21
|
+
attribute :db_info , KDomain::Database::Schema::DbInfo
|
22
|
+
attribute :unique_keys , Types::Strict::Array.of(KDomain::Database::Schema::UniqueKey)
|
23
|
+
end
|
24
|
+
|
25
|
+
attribute :tables , Types::Strict::Array.of(KDomain::Database::Table)
|
26
|
+
attribute :foreign_keys? , Types::Strict::Array.of(KDomain::Database::ForeignKey)
|
27
|
+
attribute :indexes? , Types::Strict::Array.of(KDomain::Database::Index)
|
28
|
+
attribute :meta , KDomain::Database::Schema::Meta
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDomain
|
4
|
+
module Database
|
5
|
+
class Table < Dry::Struct
|
6
|
+
class RailsSchema < Dry::Struct
|
7
|
+
attribute :primary_key , Types::Nominal::Any.optional.default(nil)
|
8
|
+
attribute :id , Types::Nominal::Any.optional.default(nil)
|
9
|
+
attribute :force , Types::Nominal::Any.optional.default(nil)
|
10
|
+
end
|
11
|
+
|
12
|
+
class Column < Dry::Struct
|
13
|
+
attribute :name , Types::Strict::String
|
14
|
+
attribute :type , Types::Strict::String
|
15
|
+
attribute :precision? , Types::Strict::Integer.optional.default(nil)
|
16
|
+
attribute :scale? , Types::Strict::Integer.optional.default(nil)
|
17
|
+
attribute :default? , Types::Nominal::Any.optional.default(nil)
|
18
|
+
attribute :array? , Types::Strict::Bool.optional.default(nil)
|
19
|
+
attribute :null? , Types::Strict::Bool.optional.default(nil)
|
20
|
+
attribute :limit? , Types::Strict::Integer.optional.default(nil)
|
21
|
+
end
|
22
|
+
|
23
|
+
attribute :name , Types::Strict::String
|
24
|
+
attribute :primary_key , Types::Strict::String.optional.default(nil)
|
25
|
+
attribute :primary_key_type , Types::Strict::String.optional.default(nil)
|
26
|
+
attribute :id? , Types::Nominal::Any.optional.default(nil)
|
27
|
+
attribute :columns , Types::Strict::Array.of(KDomain::Database::Table::Column)
|
28
|
+
attribute :indexes , Types::Strict::Array.of(KDomain::Database::Index) # May want to have a Table::Index, but for now this is a shared scheam
|
29
|
+
attribute :rails_schema , KDomain::Database::Table::RailsSchema
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Domain class holds a dictionary entry
|
4
|
+
module KDomain
|
5
|
+
module Schemas
|
6
|
+
class Dictionary < Dry::Struct
|
7
|
+
attribute :items , Types::Strict::Array do
|
8
|
+
attribute :name , Types::Strict::String
|
9
|
+
attribute :type , Types::Strict::String
|
10
|
+
attribute :label , Types::Strict::String
|
11
|
+
attribute :segment , Types::Strict::String
|
12
|
+
attribute :models , Types::Strict::Array
|
13
|
+
attribute :model_count , Types::Strict::Integer
|
14
|
+
attribute :types , Types::Strict::Array
|
15
|
+
attribute :type_count , Types::Strict::Integer
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# log.warn 'models->domain' if AppDebug.require?
|
4
|
+
|
5
|
+
require_relative './models/column'
|
6
|
+
require_relative './models/model'
|
7
|
+
require_relative './erd_file'
|
8
|
+
require_relative './domain'
|
9
|
+
|
10
|
+
module KDomain
|
11
|
+
module DomainModel
|
12
|
+
RUBY_TYPE = {
|
13
|
+
text: 'String',
|
14
|
+
string: 'String',
|
15
|
+
primary_key: 'Integer', # this could be GUID in future
|
16
|
+
foreign_key: 'Integer', # this could be GUID in future
|
17
|
+
integer: 'Integer',
|
18
|
+
bigint: 'Integer',
|
19
|
+
bigserial: 'Integer',
|
20
|
+
boolean: 'Boolean',
|
21
|
+
float: 'Float',
|
22
|
+
decimal: 'Decimal',
|
23
|
+
datetime: 'DateTime',
|
24
|
+
date: 'DateTime',
|
25
|
+
json: 'Hash',
|
26
|
+
jsonb: 'Hash',
|
27
|
+
hstore: 'Hash'
|
28
|
+
}.freeze
|
29
|
+
|
30
|
+
CSHARP_TYPE = {
|
31
|
+
string: 'string',
|
32
|
+
text: 'string', # NEED TO DEAL WITH THIS BETTER
|
33
|
+
integer: 'int',
|
34
|
+
bigint: 'int',
|
35
|
+
bigserial: 'long',
|
36
|
+
boolean: 'bool',
|
37
|
+
decimal: 'decimal',
|
38
|
+
float: 'double',
|
39
|
+
datetime: 'DateTime',
|
40
|
+
date: 'DateTime',
|
41
|
+
json: 'object',
|
42
|
+
jsonb: 'object',
|
43
|
+
hstore: 'object'
|
44
|
+
}.freeze
|
45
|
+
|
46
|
+
# this is used by the ruby migration files
|
47
|
+
DB_TYPE = {
|
48
|
+
boolean: 'boolean',
|
49
|
+
primary_key: 'integer',
|
50
|
+
foreign_key: 'integer',
|
51
|
+
integer: 'integer',
|
52
|
+
bigint: 'integer',
|
53
|
+
bigserial: 'bigserial',
|
54
|
+
decimal: 'decimal',
|
55
|
+
float: 'float',
|
56
|
+
datetime: 'datetime',
|
57
|
+
date: 'date',
|
58
|
+
text: 'text',
|
59
|
+
string: 'string',
|
60
|
+
json: 'json',
|
61
|
+
jsonb: 'jsonb',
|
62
|
+
hstore: 'hstore'
|
63
|
+
}.freeze
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Domain class holds a list of the entities
|
4
|
+
module KDomain
|
5
|
+
module DomainModel
|
6
|
+
class Domain < Dry::Struct
|
7
|
+
attribute :models , Types::Strict::Array.of(KDomain::DomainModel::Model)
|
8
|
+
attribute :erd_files , Types::Strict::Array.of(KDomain::DomainModel::ErdFile)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Domain class holds a list of the entities
|
4
|
+
module KDomain
|
5
|
+
module DomainModel
|
6
|
+
class ErdFile < Dry::Struct
|
7
|
+
attribute :name , Types::Strict::String
|
8
|
+
attribute :name_plural , Types::Strict::String
|
9
|
+
attribute :dsl_file , Types::Strict::String
|
10
|
+
|
11
|
+
attribute? :source , Dry::Struct.optional.default(nil) do
|
12
|
+
attribute :ruby , Types::Strict::String
|
13
|
+
attribute :public , Types::Strict::String.optional.default(nil)
|
14
|
+
attribute :private , Types::Strict::String.optional.default(nil)
|
15
|
+
|
16
|
+
attribute? :all_methods , Dry::Struct.optional.default(nil) do
|
17
|
+
attribute? :klass , Types::Strict::Array do
|
18
|
+
attribute :name , Types::Strict::String
|
19
|
+
attribute :scope , Types::Strict::String # .optional.default(nil)
|
20
|
+
attribute :class_method , Types::Strict::Bool
|
21
|
+
attribute :arguments , Types::Strict::String
|
22
|
+
end
|
23
|
+
attribute? :instance , Types::Strict::Array do
|
24
|
+
attribute :name , Types::Strict::String
|
25
|
+
attribute :scope , Types::Strict::String # .optional.default(nil)
|
26
|
+
attribute :class_method , Types::Strict::Bool
|
27
|
+
attribute :arguments , Types::Strict::String
|
28
|
+
end
|
29
|
+
attribute? :instance_public , Types::Strict::Array do
|
30
|
+
attribute :name , Types::Strict::String
|
31
|
+
attribute :scope , Types::Strict::String # .optional.default(nil)
|
32
|
+
attribute :class_method , Types::Strict::Bool
|
33
|
+
attribute :arguments , Types::Strict::String
|
34
|
+
end
|
35
|
+
attribute? :instance_private , Types::Strict::Array do
|
36
|
+
attribute :name , Types::Strict::String
|
37
|
+
attribute :scope , Types::Strict::String # .optional.default(nil)
|
38
|
+
attribute :class_method , Types::Strict::Bool
|
39
|
+
attribute :arguments , Types::Strict::String
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
attribute? :dsl , Dry::Struct.optional.default(nil) do
|
44
|
+
attribute :default_scope , Types::Strict::String.optional.default(nil)
|
45
|
+
|
46
|
+
attribute? :scopes , Types::Strict::Array do
|
47
|
+
attribute :name , Types::Strict::String
|
48
|
+
attribute :scope , Types::Strict::String # .optional.default(nil)
|
49
|
+
end
|
50
|
+
attribute? :belongs_to , Types::Strict::Array do
|
51
|
+
attribute :name , Types::Strict::String
|
52
|
+
attribute :options , Types::Strict::Hash.optional.default({}.freeze)
|
53
|
+
attribute :raw_options , Types::Strict::String
|
54
|
+
end
|
55
|
+
attribute? :has_one , Types::Strict::Array do
|
56
|
+
attribute :name , Types::Strict::String
|
57
|
+
attribute :options , Types::Strict::Hash.optional.default({}.freeze)
|
58
|
+
attribute :raw_options , Types::Strict::String
|
59
|
+
end
|
60
|
+
attribute? :has_many , Types::Strict::Array do
|
61
|
+
attribute :name , Types::Strict::String
|
62
|
+
attribute :options , Types::Strict::Hash.optional.default({}.freeze)
|
63
|
+
attribute :raw_options , Types::Strict::String
|
64
|
+
end
|
65
|
+
attribute? :has_and_belongs_to_many , Types::Strict::Array do
|
66
|
+
attribute :name , Types::Strict::String
|
67
|
+
attribute :options , Types::Strict::Hash.optional.default({}.freeze)
|
68
|
+
attribute :raw_options , Types::Strict::String
|
69
|
+
end
|
70
|
+
attribute? :validate_on , Types::Strict::Array do
|
71
|
+
attribute :line , Types::Strict::String
|
72
|
+
end
|
73
|
+
attribute? :validates_on , Types::Strict::Array do
|
74
|
+
attribute :name , Types::Strict::String
|
75
|
+
attribute :raw_options , Types::Strict::String
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDomain
|
4
|
+
module DomainModel
|
5
|
+
class Column < Dry::Struct
|
6
|
+
attribute :name , Types::Strict::String # "source_account_id"
|
7
|
+
attribute :name_plural , Types::Strict::String # "source_account_ids"
|
8
|
+
attribute :type , Types::Coercible::Symbol # "integer"
|
9
|
+
attribute :precision , Types::Strict::Integer.optional.default(nil) # null
|
10
|
+
attribute :scale , Types::Strict::Integer.optional.default(nil) # null
|
11
|
+
attribute :default , Types::Nominal::Any.optional.default(nil) # null
|
12
|
+
attribute :null , Types::Nominal::Any.optional.default(nil) # null
|
13
|
+
attribute :limit , Types::Strict::Integer.optional.default(nil) # null
|
14
|
+
attribute :array , Types::Strict::Bool.optional.default(nil) # null
|
15
|
+
|
16
|
+
# Calculated value
|
17
|
+
attribute :structure_type , Types::Coercible::Symbol #
|
18
|
+
attribute :foreign_key , Types::Strict::Bool.optional.default(nil) #
|
19
|
+
attribute :foreign_table , Types::Strict::String #
|
20
|
+
attribute :foreign_table_plural , Types::Strict::String #
|
21
|
+
|
22
|
+
# def data_column
|
23
|
+
# @columns_data ||= structure_type?(:data)
|
24
|
+
# end
|
25
|
+
|
26
|
+
# def structure_type?(*structure_types)
|
27
|
+
# structure_types.include?(column.structure_type)
|
28
|
+
# end
|
29
|
+
|
30
|
+
def db_type
|
31
|
+
return @db_type if defined? @db_type
|
32
|
+
|
33
|
+
@db_type = DB_TYPE[type] || '******'
|
34
|
+
end
|
35
|
+
|
36
|
+
def ruby_type
|
37
|
+
return @ruby_type if defined? @ruby_type
|
38
|
+
|
39
|
+
@ruby_type = RUBY_TYPE[type] || '******'
|
40
|
+
end
|
41
|
+
|
42
|
+
def csharp_type
|
43
|
+
return @csharp_type if defined? @csharp_type
|
44
|
+
|
45
|
+
@csharp_type = CSHARP_TYPE[type] || '******'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Domain class holds a list of the entities
|
4
|
+
module KDomain
|
5
|
+
module DomainModel
|
6
|
+
class Model < Dry::Struct
|
7
|
+
class Pk < Dry::Struct
|
8
|
+
attribute :name , Types::Strict::String.optional.default(nil)
|
9
|
+
attribute :type , Types::Strict::String.optional.default(nil)
|
10
|
+
attribute :exist , Types::Strict::Bool
|
11
|
+
end
|
12
|
+
|
13
|
+
class ErdLocation < Dry::Struct
|
14
|
+
attribute :file , Types::Strict::String
|
15
|
+
attribute :exist , Types::Strict::Bool
|
16
|
+
attribute :state , Types::Strict::Array
|
17
|
+
end
|
18
|
+
|
19
|
+
attribute :name , Types::Strict::String
|
20
|
+
attribute :name_plural , Types::Strict::String
|
21
|
+
attribute :table_name , Types::Strict::String
|
22
|
+
# Model type - :entity, :basic_user, :admin_user, possibly: m2m, agg_root
|
23
|
+
attribute :type , Types::Strict::Symbol.optional.default(:entity)
|
24
|
+
attribute :pk , KDomain::DomainModel::Model::Pk
|
25
|
+
attribute :erd_location , KDomain::DomainModel::Model::ErdLocation
|
26
|
+
attribute :columns , Types::Strict::Array.of(KDomain::DomainModel::Column)
|
27
|
+
|
28
|
+
def ruby?
|
29
|
+
location.exist
|
30
|
+
end
|
31
|
+
|
32
|
+
def pk?
|
33
|
+
pk.exist
|
34
|
+
end
|
35
|
+
|
36
|
+
# If filled in, the model has a main field that is useful for rendering and may be used for unique constraint, may also be called display_name
|
37
|
+
def main_key
|
38
|
+
@main_key ||= MainKey.lookup(name, columns_data)
|
39
|
+
end
|
40
|
+
|
41
|
+
def traits
|
42
|
+
@traits ||= Traits.lookup(name)
|
43
|
+
end
|
44
|
+
|
45
|
+
# def where()
|
46
|
+
# end
|
47
|
+
|
48
|
+
# def columns_where()
|
49
|
+
# end
|
50
|
+
|
51
|
+
# Column filters
|
52
|
+
|
53
|
+
def columns_data
|
54
|
+
@columns_data ||= columns_for_structure_types(:data)
|
55
|
+
end
|
56
|
+
|
57
|
+
# def columns_data_optional
|
58
|
+
# @columns_data_optional ||= columns_for_structure_types(:data).select { |c| true }
|
59
|
+
# end
|
60
|
+
|
61
|
+
# def columns_data_required
|
62
|
+
# @columns_data_required ||= columns_for_structure_types(:data).select { |c| false }
|
63
|
+
# end
|
64
|
+
|
65
|
+
def columns_primary
|
66
|
+
@columns_primary ||= columns_for_structure_types(:primary_key)
|
67
|
+
end
|
68
|
+
|
69
|
+
def columns_foreign
|
70
|
+
@columns_foreign ||= columns_for_structure_types(:foreign_key)
|
71
|
+
end
|
72
|
+
|
73
|
+
def columns_timestamp
|
74
|
+
@columns_data_timestamp ||= columns_for_structure_types(:timestamp)
|
75
|
+
end
|
76
|
+
|
77
|
+
def columns_deleted_at
|
78
|
+
@columns_data_deleted_at ||= columns_for_structure_types(:deleted_at)
|
79
|
+
end
|
80
|
+
|
81
|
+
def columns_virtual
|
82
|
+
@columns_virtual ||= columns_for_structure_types(:timestamp, :deleted_at)
|
83
|
+
end
|
84
|
+
|
85
|
+
def columns_data_foreign
|
86
|
+
@columns_data_foreign ||= columns_for_structure_types(:data, :foreign_key)
|
87
|
+
end
|
88
|
+
alias rows_fields_and_fk columns_data_foreign
|
89
|
+
|
90
|
+
def columns_data_primary
|
91
|
+
@columns_data_primary ||= columns_for_structure_types(:data, :primary_key)
|
92
|
+
end
|
93
|
+
alias rows_fields_and_pk columns_data_primary
|
94
|
+
|
95
|
+
def columns_data_virtual
|
96
|
+
@columns_data_virtual ||= columns_for_structure_types(:data, :timestamp, :deleted_at)
|
97
|
+
end
|
98
|
+
alias rows_fields_and_virtual columns_data_virtual
|
99
|
+
|
100
|
+
def columns_data_foreign_virtual
|
101
|
+
@columns_data_foreign_virtual ||= columns_for_structure_types(:data, :foreign_key, :timestamp, :deleted_at)
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
def columns_for_structure_types(*structure_types)
|
107
|
+
columns.select { |column| structure_types.include?(column.structure_type) }
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KDomain
|
4
|
+
module DomainModel
|
5
|
+
class BelongsTo
|
6
|
+
KEYS = %i[a_lambda polymorphic class_name foreign_key primary_key inverse_of with_deleted code_duplicate].freeze
|
7
|
+
|
8
|
+
attr_accessor :name
|
9
|
+
|
10
|
+
attr_accessor :model_name
|
11
|
+
attr_accessor :model_name_plural
|
12
|
+
|
13
|
+
attr_accessor :a_lambda
|
14
|
+
attr_accessor :polymorphic
|
15
|
+
attr_accessor :class_name
|
16
|
+
attr_accessor :foreign_key
|
17
|
+
attr_accessor :primary_key
|
18
|
+
attr_accessor :inverse_of
|
19
|
+
attr_accessor :with_deleted
|
20
|
+
|
21
|
+
attr_accessor :related_entity
|
22
|
+
attr_accessor :code_duplicate
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|