hecks-adapters-sql-database 0.1.16.rc → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,42 +1,42 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- module Commands
5
- class Update
6
- class LinkToReferences
7
- attr_reader :reference_ids
8
- def initialize(reference:, table:, reference_ids:, attributes:, id:)
9
- @reference = reference
10
- @reference_ids = reference_ids
11
- @table = table
12
- @attributes = attributes
13
- @column = Column.factory(@reference)
14
- @record = {}
15
- @id = id
16
- end
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ module Commands
4
+ class Update
5
+ # Link to any new references
6
+ class LinkToReferences
7
+ attr_reader :reference_ids
8
+ def initialize(reference:, table:, reference_ids:, attributes:, id:)
9
+ @reference = reference
10
+ @reference_ids = reference_ids
11
+ @table = table
12
+ @attributes = attributes
13
+ @column = Column.factory(@reference)
14
+ @record = {}
15
+ @id = id
16
+ end
17
17
 
18
- def call
19
- make_linking_records
20
- make_foreign_keys
21
- self
22
- end
18
+ def call
19
+ make_linking_records
20
+ make_foreign_keys
21
+ self
22
+ end
23
23
 
24
- private
24
+ private
25
25
 
26
- def make_linking_records
27
- return unless @reference.list?
28
- @reference_ids[@reference.name.to_sym].each do |value|
29
- @record[@column.to_foreign_key] = value
30
- @record[@table.to_foreign_key] = @id
31
- DB[JoinTable.new(@table, @reference).name.to_sym].insert(@record)
32
- end
33
- end
26
+ def make_linking_records
34
27
 
35
- def make_foreign_keys
36
- return if @reference.list?
37
- @attributes[@column.to_foreign_key] = @reference_ids[@reference.name]
28
+ return unless @reference.list?
29
+ @reference_ids[@reference.name.to_sym].each do |value|
30
+ @record[@column.to_foreign_key] = value
31
+ @record[@table.to_foreign_key] = @id
32
+ DB[JoinTable.new(@table, @reference).name.to_sym].insert(@record.merge(id: SecureRandom.uuid))
38
33
  end
39
34
  end
35
+
36
+ def make_foreign_keys
37
+ return if @reference.list?
38
+ @attributes[@column.to_foreign_key] = @reference_ids[@reference.name]
39
+ end
40
40
  end
41
41
  end
42
42
  end
@@ -1,52 +1,51 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- module Commands
5
- class Update
6
- class UpdateValues
7
- def initialize(references, attributes, table, id)
8
- @references = references
9
- @attributes = attributes
10
- @reference_ids = {}
11
- @table = table
12
- @id = id
13
- end
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ module Commands
4
+ class Update
5
+ # Update values
6
+ class UpdateValues
7
+ def initialize(references, attributes, table, id)
8
+ @references = references
9
+ @attributes = attributes
10
+ @reference_ids = {}
11
+ @table = table
12
+ @id = id
13
+ end
14
14
 
15
- def call
16
- @references.each do |reference|
17
- create_new_value(reference)
18
- delete_old_references(reference)
19
- link_to_new_values(reference)
20
- end
15
+ def call
16
+ @references.each do |reference|
17
+ create_new_value(reference)
18
+ delete_old_references(reference)
19
+ link_to_new_values(reference)
21
20
  end
21
+ end
22
22
 
23
- private
23
+ private
24
24
 
25
- def create_new_value(reference)
26
- @reference_ids = CreateNewValue.new(
27
- reference: reference,
28
- attributes: @attributes,
29
- reference_ids: @reference_ids
30
- ).call.reference_ids
31
- end
25
+ def create_new_value(reference)
26
+ @reference_ids = CreateNewValue.new(
27
+ reference: reference,
28
+ attributes: @attributes,
29
+ reference_ids: @reference_ids
30
+ ).call.reference_ids
31
+ end
32
32
 
33
- def delete_old_references(reference)
34
- @attributes = DeleteReferences.new(
35
- reference: reference,
36
- table: @table,
37
- attributes: @attributes
38
- ).call.attributes
39
- end
33
+ def delete_old_references(reference)
34
+ @attributes = DeleteReferences.new(
35
+ reference: reference,
36
+ table: @table,
37
+ attributes: @attributes
38
+ ).call.attributes
39
+ end
40
40
 
41
- def link_to_new_values(reference)
42
- LinkToReferences.new(
43
- reference: reference,
44
- table: @table,
45
- reference_ids: @reference_ids,
46
- attributes: @attributes,
47
- id: @id
48
- ).call
49
- end
41
+ def link_to_new_values(reference)
42
+ LinkToReferences.new(
43
+ reference: reference,
44
+ table: @table,
45
+ reference_ids: @reference_ids,
46
+ attributes: @attributes,
47
+ id: @id
48
+ ).call
50
49
  end
51
50
  end
52
51
  end
@@ -1,6 +1,8 @@
1
1
  require 'sequel'
2
+ require 'mysql2'
2
3
  require 'hecks-application'
3
4
 
5
+ require_relative 'cli/cli'
4
6
  require_relative 'column'
5
7
  require_relative 'join_table'
6
8
  require_relative 'schema_factory'
@@ -10,19 +12,18 @@ require_relative 'repository'
10
12
  require_relative 'commands/commands'
11
13
 
12
14
  if ENV["DATABASE_URL"]
13
- DB = Sequel.connect(ENV["DATABASE_URL"])
15
+ DB ||= Sequel.connect(ENV["DATABASE_URL"])
14
16
  end
15
17
 
16
- module Hecks
17
- module Adapters
18
- class SQLDatabase
19
- def initialize(domain:)
20
- @domain = domain
21
- end
18
+ module HecksAdapters
19
+ # The Hecks Database interface
20
+ class SQLDatabase
21
+ def initialize(domain:)
22
+ @domain = domain
23
+ end
22
24
 
23
- def [](module_name)
24
- Repository.new(module_name: module_name)
25
- end
25
+ def [](module_name)
26
+ Repository.new(module_name: module_name)
26
27
  end
27
28
  end
28
29
  end
data/lib/join_table.rb CHANGED
@@ -1,20 +1,19 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- class JoinTable
5
- def initialize(table, column)
6
- @table = table
7
- @column = column
8
- end
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ # Represents a SQL Table with information about object references
4
+ class JoinTable
5
+ def initialize(table, column)
6
+ @table = table
7
+ @column = column
8
+ end
9
9
 
10
- def name
11
- "#{@table.name}_#{@column.name}"
12
- end
10
+ def name
11
+ "#{@table.name}_#{@column.name}"
12
+ end
13
13
 
14
- def columns
15
- [@table.name, @column.name].map do |name|
16
- Column.new(name: name.singularize + '_id', type: 'Integer')
17
- end
14
+ def columns
15
+ [@table.name, @column.name].map do |name|
16
+ Column.new(name: name.singularize + '_id', type: 'String')
18
17
  end
19
18
  end
20
19
  end
data/lib/repository.rb CHANGED
@@ -1,37 +1,32 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- class Repository
5
- def initialize(module_name:)
6
- @module_name = module_name
7
- @head = DOMAIN[module_name].head
8
- end
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ # Translates calls from an application into SQL Commands
4
+ class Repository
5
+ def initialize(module_name:)
6
+ @module_name = module_name
7
+ @head = DOMAIN[module_name].head
8
+ end
9
9
 
10
- def create attributes
11
- Commands::Create.new(attributes: attributes, head: @head).call
12
- end
10
+ def create attributes
11
+ Commands::Create.new(attributes: attributes, head: @head).call
12
+ end
13
13
 
14
- def update id, attributes
15
- Commands::Update.new(id: id, attributes: attributes, head: @head).call
16
- end
14
+ def update id, attributes
15
+ Commands::Update.new(id: id, attributes: attributes, head: @head).call
16
+ end
17
17
 
18
- def read id
19
- Commands::Read.new(
20
- id: id,
21
- head: @head,
22
- entity_class: entity_class
23
- ).call
24
- end
18
+ def read id
19
+ Commands::Read.new(id: id, head: @head, entity_class: entity_class).call
20
+ end
25
21
 
26
- def delete id
27
- Commands::Delete.new(id: id, head: @head).call
28
- end
22
+ def delete id
23
+ Commands::Delete.new(id: id, head: @head).call
24
+ end
29
25
 
30
- private
26
+ private
31
27
 
32
- def entity_class
33
- DOMAIN.name.camelcase.constantize::Domain.const_get(@module_name).head
34
- end
28
+ def entity_class
29
+ DOMAIN.name.camelcase.constantize::Domain.const_get(@module_name).head
35
30
  end
36
31
  end
37
32
  end
data/lib/schema.rb CHANGED
@@ -3,22 +3,21 @@ require_relative 'table'
3
3
  require_relative 'join_table'
4
4
  require_relative 'column'
5
5
 
6
- module Hecks
7
- module Adapters
8
- class SQLDatabase
9
- class Schema
10
- attr_reader :tables
11
- def self.factory(domain_spec)
12
- SchemaFactory.new(domain_spec).build
13
- end
6
+ module HecksAdapters
7
+ class SQLDatabase
8
+ # Represents a SQL Schema
9
+ class Schema
10
+ attr_reader :tables
11
+ def self.factory(domain_spec)
12
+ SchemaFactory.new(domain_spec).build
13
+ end
14
14
 
15
- def initialize(tables)
16
- @tables = tables
17
- end
15
+ def initialize(tables)
16
+ @tables = tables
17
+ end
18
18
 
19
- def to_h
20
- tables.map { |table| [table.name.to_sym, table] }.to_h
21
- end
19
+ def to_h
20
+ tables.map { |table| [table.name.to_sym, table] }.to_h
22
21
  end
23
22
  end
24
23
  end
@@ -1,56 +1,55 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- class SchemaFactory
5
- def initialize(domain_spec)
6
- @domain_spec = domain_spec
7
- @tables = Table.factory(domain_objects)
8
- @join_tables = []
9
- end
10
-
11
- def build
12
- @tables.each do |table|
13
- swap_domain_references(table)
14
- build_join_tables(table)
15
- remove_domain_columns(table)
16
- end
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ # Build a Schema Factory
4
+ class SchemaFactory
5
+ def initialize(domain_spec)
6
+ @domain_spec = domain_spec
7
+ @tables = Table.factory(domain_objects)
8
+ @join_tables = []
9
+ end
17
10
 
18
- Schema.new(@tables + @join_tables)
11
+ def build
12
+ @tables.each do |table|
13
+ swap_domain_references(table)
14
+ build_join_tables(table)
15
+ remove_domain_columns(table)
19
16
  end
20
17
 
21
- private
18
+ Schema.new(@tables + @join_tables)
19
+ end
22
20
 
23
- def domain_objects
24
- @domain_spec.domain_modules.values.flat_map(&:objects)
25
- end
21
+ private
26
22
 
27
- def swap_domain_references(table)
28
- table.foreign_key_columns.each do |column|
29
- add_db_reference(column, table)
30
- end
23
+ def domain_objects
24
+ @domain_spec.domain_modules.values.flat_map(&:objects)
25
+ end
26
+
27
+ def swap_domain_references(table)
28
+ table.foreign_key_columns.each do |column|
29
+ add_db_reference(column, table)
31
30
  end
31
+ end
32
32
 
33
- def build_join_tables(table)
34
- table.join_table_columns.each do |column|
35
- @join_tables << JoinTable.new(table, column)
36
- end
33
+ def build_join_tables(table)
34
+ table.join_table_columns.each do |column|
35
+ @join_tables << JoinTable.new(table, column)
37
36
  end
37
+ end
38
38
 
39
- def remove_domain_columns(table)
40
- (table.foreign_key_columns + table.join_table_columns).each do |column|
41
- table.columns = table.columns.select do |table_column|
42
- table_column != column
43
- end
39
+ def remove_domain_columns(table)
40
+ (table.foreign_key_columns + table.join_table_columns).each do |column|
41
+ table.columns = table.columns.select do |table_column|
42
+ table_column != column
44
43
  end
45
44
  end
45
+ end
46
46
 
47
- def add_db_reference(column, table)
48
- table.columns << foreign_key_column(column.referenced_object)
49
- end
47
+ def add_db_reference(column, table)
48
+ table.columns << foreign_key_column(column.referenced_object)
49
+ end
50
50
 
51
- def foreign_key_column(column_name, is_list = false)
52
- Column.new(name: column_name + '_id', type: 'Integer', is_list: is_list)
53
- end
51
+ def foreign_key_column(column_name, is_list = false)
52
+ Column.new(name: column_name + '_id', type: 'String', is_list: is_list)
54
53
  end
55
54
  end
56
55
  end
data/lib/table.rb CHANGED
@@ -1,40 +1,39 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- class Table
5
- attr_accessor :columns
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ # Represents a SQL Table
4
+ class Table
5
+ attr_accessor :columns
6
6
 
7
- def self.factory(domain_objects)
8
- r = domain_objects.map do |domain_object|
9
- new(
10
- name: domain_object.name,
11
- columns: domain_object.attributes.map do |attribute|
12
- Column.factory(attribute)
13
- end
14
- )
15
- end
7
+ def self.factory(domain_objects)
8
+ r = domain_objects.map do |domain_object|
9
+ new(
10
+ name: domain_object.name,
11
+ columns: domain_object.attributes.map do |attribute|
12
+ Column.factory(attribute)
13
+ end
14
+ )
16
15
  end
16
+ end
17
17
 
18
- def initialize(name:, columns:)
19
- @name = name
20
- @columns = columns
21
- end
18
+ def initialize(name:, columns:)
19
+ @name = name
20
+ @columns = columns
21
+ end
22
22
 
23
- def to_foreign_key
24
- (name.singularize + '_id').to_sym
25
- end
23
+ def to_foreign_key
24
+ (name.singularize + '_id').to_sym
25
+ end
26
26
 
27
- def join_table_columns
28
- columns.select(&:list?)
29
- end
27
+ def join_table_columns
28
+ columns.select(&:list?)
29
+ end
30
30
 
31
- def foreign_key_columns
32
- columns.select(&:reference?)
33
- end
31
+ def foreign_key_columns
32
+ columns.select(&:reference?)
33
+ end
34
34
 
35
- def name
36
- @name.pluralize.underscore
37
- end
35
+ def name
36
+ @name.pluralize.underscore
38
37
  end
39
38
  end
40
39
  end