ree_lib 1.0.42 → 1.0.43

Sign up to get free protection for your applications and to get access to all the features.
Files changed (26) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +2 -2
  3. data/lib/ree_lib/packages/ree_actions/Package.schema.json +3 -0
  4. data/lib/ree_lib/packages/ree_actions/package/ree_actions/dsl.rb +12 -0
  5. data/lib/ree_lib/packages/ree_actions/package/ree_actions.rb +1 -0
  6. data/lib/ree_lib/packages/ree_actions/spec/ree_actions/dsl_spec.rb +110 -0
  7. data/lib/ree_lib/packages/ree_dao/Package.schema.json +31 -0
  8. data/lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb +56 -0
  9. data/lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb +39 -39
  10. data/lib/ree_lib/packages/ree_dao/package/ree_dao/functions/build_pg_connection.rb +1 -1
  11. data/lib/ree_lib/packages/ree_dao/package/ree_dao/functions/drop_cache.rb +15 -0
  12. data/lib/ree_lib/packages/ree_dao/package/ree_dao/functions/extract_changes.rb +26 -0
  13. data/lib/ree_lib/packages/ree_dao/package/ree_dao/functions/init_cache.rb +15 -0
  14. data/lib/ree_lib/packages/ree_dao/package/ree_dao.rb +9 -0
  15. data/lib/ree_lib/packages/ree_dao/schemas/ree_dao/beans/dao_cache.schema.json +22 -0
  16. data/lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_pg_connection.schema.json +1 -1
  17. data/lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/drop_cache.schema.json +31 -0
  18. data/lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/extract_changes.schema.json +45 -0
  19. data/lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/init_cache.schema.json +31 -0
  20. data/lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/build_pg_connection_spec.rb +150 -0
  21. data/lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/build_sqlite_connection_spec.rb +6 -5
  22. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper.rb +4 -0
  23. data/lib/ree_lib/packages/ree_roda/spec/ree_roda/app_spec.rb +4 -1
  24. data/lib/ree_lib/packages/ree_roda/spec/ree_roda/services/build_routing_tree_spec.rb +4 -1
  25. data/lib/ree_lib/version.rb +1 -1
  26. metadata +11 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '029a676848df8b25ed6e8319749a9a563f779e65886e866c57e1a8834bf5614b'
4
- data.tar.gz: 6bd0ff97e2bb2d6b10eb121c29e988fe63af07cd8759212c261a61abeefb89f9
3
+ metadata.gz: 1322fe618658e425a1614927faf1ed43252e3b49bc9c953d3ba10de162693217
4
+ data.tar.gz: 8843b92fb6f84cbd83b1966108ea2a541ae798067b6b1a379c1cb7a1baeebbfa
5
5
  SHA512:
6
- metadata.gz: 2f009bab3a5897fafc59e3443577b19b10435b0fa74d8840d148d2b4151fae4c527d230dcb8218cde86d401ef955084a34c418124e4ae35ddb91845d2d44ff5e
7
- data.tar.gz: ee04a53688b9c282b314440fc4da21943c30dcfbb5a73c209bfdca4605cabef1a6f74f8805f9a0fa76581f192dbb58ce82a50dbe7e6110277984ab6b95e93680
6
+ metadata.gz: 41693ec34aff5834b7aba142dd2fb9a9a10146ce178091a8404d7c143bc6a1bce54b106fdbacac896ccc19df3ef5ef87d41a5a88cf2ad27646362839151a04cc
7
+ data.tar.gz: eb691d0b59814c5fab063627c6bea39ab8dcfec2f48643e17249a6175d2738c5867ba342776b7967626008757244848ad4abd95dd290f57de119a772b3167d81
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ree_lib (1.0.42)
4
+ ree_lib (1.0.43)
5
5
  binding_of_caller (~> 1.0.0)
6
6
  i18n (~> 1.12.0)
7
7
  loofah (~> 2.18.0)
@@ -49,7 +49,7 @@ GEM
49
49
  rack (>= 1.3)
50
50
  rainbow (3.1.1)
51
51
  rake (13.0.6)
52
- ree (1.0.27)
52
+ ree (1.0.28)
53
53
  commander (~> 4.6.0)
54
54
  rexml (3.2.5)
55
55
  roda (3.58.0)
@@ -7,6 +7,9 @@
7
7
  "ree_actions"
8
8
  ],
9
9
  "depends_on": [
10
+ {
11
+ "name": "ree_dao"
12
+ },
10
13
  {
11
14
  "name": "ree_mapper"
12
15
  }
@@ -3,11 +3,19 @@ module ReeActions
3
3
  def self.included(base)
4
4
  base.extend(ClassMethods)
5
5
  base.include(ReeMapper::DSL)
6
+ link_dao_cache(base)
6
7
  end
7
8
 
8
9
  def self.extended(base)
9
10
  base.extend(ClassMethods)
10
11
  base.include(ReeMapper::DSL)
12
+ link_dao_cache(base)
13
+ end
14
+
15
+ private_class_method def self.link_dao_cache(base)
16
+ base.include(Ree::LinkDSL)
17
+ base.link :drop_cache, as: :__ree_dao_drop_cache, from: :ree_dao
18
+ base.link :init_cache, as: :__ree_dao_init_cache, from: :ree_dao
11
19
  end
12
20
 
13
21
  module ClassMethods
@@ -40,6 +48,8 @@ module ReeActions
40
48
  alias_method(:__original_call, :call)
41
49
 
42
50
  define_method :call do |user_access, attrs|
51
+ __ree_dao_init_cache
52
+
43
53
  if self.class.const_defined?(:ActionCaster)
44
54
  caster = self.class.const_get(:ActionCaster)
45
55
 
@@ -51,6 +61,8 @@ module ReeActions
51
61
  else
52
62
  __original_call(user_access, attrs)
53
63
  end
64
+ ensure
65
+ __ree_dao_drop_cache
54
66
  end
55
67
 
56
68
  nil
@@ -5,6 +5,7 @@ module ReeActions
5
5
 
6
6
  package do
7
7
  depends_on :ree_mapper
8
+ depends_on :ree_dao
8
9
  end
9
10
  end
10
11
 
@@ -1,6 +1,29 @@
1
1
  package_require("ree_actions/dsl")
2
2
 
3
3
  RSpec.describe ReeActions::DSL, type: [:autoclean] do
4
+ link :build_sqlite_connection, from: :ree_dao
5
+
6
+ before(:all) do
7
+ connection = build_sqlite_connection({database: 'sqlite_db'})
8
+
9
+ if connection.table_exists?(:users)
10
+ connection.drop_table(:users)
11
+ end
12
+
13
+ if connection.table_exists?(:products)
14
+ connection.drop_table(:products)
15
+ end
16
+
17
+ connection.create_table :users do
18
+ primary_key :id
19
+
20
+ column :name, 'varchar(256)'
21
+ column :age, :integer
22
+ end
23
+
24
+ connection.disconnect
25
+ end
26
+
4
27
  before do
5
28
  Ree.enable_irb_mode
6
29
  end
@@ -15,6 +38,7 @@ RSpec.describe ReeActions::DSL, type: [:autoclean] do
15
38
 
16
39
  package do
17
40
  depends_on :ree_mapper
41
+ depends_on :ree_dao
18
42
  end
19
43
 
20
44
  class TestAction
@@ -42,6 +66,8 @@ RSpec.describe ReeActions::DSL, type: [:autoclean] do
42
66
  include Ree::PackageDSL
43
67
 
44
68
  package do
69
+ depends_on :ree_mapper
70
+ depends_on :ree_dao
45
71
  end
46
72
 
47
73
  class TestAction2
@@ -59,4 +85,88 @@ RSpec.describe ReeActions::DSL, type: [:autoclean] do
59
85
  result = ReeActionsTest::TestAction2.new.call('user_access', {user_id: 1})
60
86
  expect(result).to eq(1)
61
87
  }
88
+
89
+ it {
90
+ module ReeActionsTest
91
+ include Ree::PackageDSL
92
+
93
+ package do
94
+ depends_on :ree_mapper
95
+ depends_on :ree_dao
96
+ end
97
+
98
+ class Db
99
+ include Ree::BeanDSL
100
+
101
+ bean :db do
102
+ singleton
103
+ factory :build
104
+
105
+ link :build_sqlite_connection, from: :ree_dao
106
+ end
107
+
108
+ def build
109
+ build_sqlite_connection({database: 'sqlite_db'})
110
+ end
111
+ end
112
+
113
+ class User
114
+ include ReeDto::EntityDSL
115
+
116
+ properties(
117
+ id: Nilor[Integer],
118
+ name: String,
119
+ age: Integer,
120
+ )
121
+
122
+ attr_accessor :name
123
+ end
124
+
125
+ class UsersDao
126
+ include ReeDao::DSL
127
+
128
+ dao :users_dao do
129
+ link :db
130
+ end
131
+
132
+ table :users
133
+
134
+ schema ReeActionsTest::User do
135
+ integer :id, null: true
136
+ string :name
137
+ integer :age
138
+ end
139
+ end
140
+
141
+ class TestAction3
142
+ include ReeActions::DSL
143
+
144
+ action :test_action3 do
145
+ link :users_dao
146
+ end
147
+
148
+ contract Any, Hash => Integer
149
+ def call(user_access, attrs)
150
+ user = ReeActionsTest::User.new(name: 'John', age: 30)
151
+ users_dao.put(user)
152
+
153
+ thr = Thread.new {
154
+ user2 = ReeActionsTest::User.new(name: 'Alex', age: 33)
155
+ users_dao.put(user2)
156
+ }
157
+
158
+ thr.join
159
+
160
+ $thread_group_cache = ReeDao::DaoCache.new.instance_variable_get(:@thread_groups)
161
+ .dig(Thread.current.group.object_id, :users)
162
+
163
+ attrs[:user_id]
164
+ end
165
+ end
166
+ end
167
+
168
+ ReeActionsTest::TestAction3.new.call('user_access', {user_id: 1})
169
+ expect($thread_group_cache.keys.count).to_not eq(0)
170
+ expect($thread_group_cache.keys.count).to eq(2)
171
+ }
62
172
  end
@@ -19,6 +19,9 @@
19
19
  {
20
20
  "name": "ree_mapper"
21
21
  },
22
+ {
23
+ "name": "ree_object"
24
+ },
22
25
  {
23
26
  "name": "ree_string"
24
27
  }
@@ -62,6 +65,34 @@
62
65
  "object"
63
66
  ]
64
67
  },
68
+ {
69
+ "name": "dao_cache",
70
+ "schema": "packages/ree_dao/schemas/ree_dao/beans/dao_cache.schema.json",
71
+ "tags": [
72
+ "object"
73
+ ]
74
+ },
75
+ {
76
+ "name": "drop_cache",
77
+ "schema": "packages/ree_dao/schemas/ree_dao/functions/drop_cache.schema.json",
78
+ "tags": [
79
+ "fn"
80
+ ]
81
+ },
82
+ {
83
+ "name": "extract_changes",
84
+ "schema": "packages/ree_dao/schemas/ree_dao/functions/extract_changes.schema.json",
85
+ "tags": [
86
+ "fn"
87
+ ]
88
+ },
89
+ {
90
+ "name": "init_cache",
91
+ "schema": "packages/ree_dao/schemas/ree_dao/functions/init_cache.schema.json",
92
+ "tags": [
93
+ "fn"
94
+ ]
95
+ },
65
96
  {
66
97
  "name": "one_to_many",
67
98
  "schema": "packages/ree_dao/schemas/ree_dao/functions/one_to_many.schema.json",
@@ -0,0 +1,56 @@
1
+ class ReeDao::DaoCache
2
+ include Ree::BeanDSL
3
+
4
+ bean :dao_cache do
5
+ link :deep_dup, from: :ree_object
6
+
7
+ singleton
8
+ after_init :setup
9
+ end
10
+
11
+ def setup
12
+ @thread_groups = {}
13
+ end
14
+
15
+ def add_thread_group_cache(thread_group)
16
+ @thread_groups[thread_group.object_id] ||= {}
17
+ end
18
+
19
+ def drop_thread_group_cache(thread_group)
20
+ @thread_groups.delete(thread_group.object_id)
21
+ end
22
+
23
+ def get(table_name, primary_key)
24
+ add_thread_group_cache(current_thread_group)
25
+ add_table_name(table_name)
26
+
27
+ @thread_groups[current_thread_group.object_id][table_name][primary_key]
28
+ end
29
+
30
+ def set(table_name, primary_key, data)
31
+ add_thread_group_cache(current_thread_group)
32
+ add_table_name(table_name)
33
+ add_primary_key(table_name, primary_key)
34
+
35
+ @thread_groups[current_thread_group.object_id][table_name][primary_key] = deep_dup(data)
36
+ end
37
+
38
+ def drop_table_cache(table_name)
39
+ add_thread_group_cache(current_thread_group)
40
+ @thread_groups[current_thread_group.object_id].delete(table_name)
41
+ end
42
+
43
+ private
44
+
45
+ def current_thread_group
46
+ Thread.current.group
47
+ end
48
+
49
+ def add_table_name(thread_group = Thread.current.group, table_name)
50
+ @thread_groups[thread_group.object_id][table_name] ||= {}
51
+ end
52
+
53
+ def add_primary_key(thread_group = Thread.current.group, table_name, primary_key)
54
+ @thread_groups[thread_group.object_id][table_name][primary_key] ||= {}
55
+ end
56
+ end
@@ -19,24 +19,25 @@ module ReeDao
19
19
  end
20
20
 
21
21
  module InstanceMethods
22
- PERSISTENCE_STATE_VARIABLE = :@persistence_state
23
22
  IMPORT_BATCH_SIZE = 1000
24
23
 
24
+ package_require("ree_dao/functions/extract_changes")
25
+
25
26
  # override methods
26
- def find(id, mode = :write, mapper: nil)
27
- where(primary_key => id).first(mode, mapper: mapper)
27
+ def find(id, mapper: nil)
28
+ where(primary_key => id).first(mapper: mapper)
28
29
  end
29
30
 
30
- def first(mode = :write, mapper: nil)
31
- with_mapper(mode, mapper).__original_first
31
+ def first(mapper: nil)
32
+ with_mapper(mapper).__original_first
32
33
  end
33
34
 
34
- def last(mode = :write, mapper: nil)
35
- with_mapper(mode, mapper).__original_last
35
+ def last(mapper: nil)
36
+ with_mapper(mapper).__original_last
36
37
  end
37
38
 
38
- def all(mode = :write, mapper: nil)
39
- with_mapper(mode, mapper).__original_all
39
+ def all(mapper: nil)
40
+ with_mapper(mapper).__original_all
40
41
  end
41
42
 
42
43
  def delete_all
@@ -49,7 +50,7 @@ module ReeDao
49
50
  key = insert(raw)
50
51
 
51
52
  set_entity_primary_key(entity, raw, key)
52
- set_persistence_state(entity, raw)
53
+ set_entity_cache(entity, raw)
53
54
 
54
55
  entity
55
56
  end
@@ -73,7 +74,7 @@ module ReeDao
73
74
  key = insert_conflict(conflict_opts).insert(raw)
74
75
 
75
76
  set_entity_primary_key(entity, raw, key)
76
- set_persistence_state(entity, raw)
77
+ set_entity_cache(entity, raw)
77
78
 
78
79
  where(primary_key => key).first
79
80
  end
@@ -104,7 +105,7 @@ module ReeDao
104
105
  raw_data = raw[entity]
105
106
 
106
107
  set_entity_primary_key(entity, raw_data, id)
107
- set_persistence_state(entity, raw_data)
108
+ set_entity_cache(entity, raw_data)
108
109
  end
109
110
 
110
111
  nil
@@ -115,10 +116,10 @@ module ReeDao
115
116
  return __original_update(hash_or_entity) if hash_or_entity.is_a?(Hash)
116
117
 
117
118
  raw = opts[:schema_mapper].db_dump(hash_or_entity)
118
- raw = extract_changes(hash_or_entity, raw)
119
+ raw = ReeDao::ExtractChanges.new.call(table_name, extract_primary_key(hash_or_entity), raw)
119
120
 
120
121
  unless raw.empty?
121
- update_persistence_state(hash_or_entity, raw)
122
+ update_entity_cache(hash_or_entity, raw)
122
123
  key_condition = prepare_key_condition_from_entity(hash_or_entity)
123
124
  where(key_condition).__original_update(raw)
124
125
  end
@@ -148,10 +149,18 @@ module ReeDao
148
149
 
149
150
  private
150
151
 
152
+ def __ree_dao_cache
153
+ ReeDao::DaoCache.new
154
+ end
155
+
151
156
  def primary_key
152
157
  opts[:primary_key] || :id
153
158
  end
154
159
 
160
+ def table_name
161
+ first_source_table
162
+ end
163
+
155
164
  def remove_null_primary_key(raw)
156
165
  return if primary_key.is_a?(Array)
157
166
 
@@ -166,9 +175,9 @@ module ReeDao
166
175
  end
167
176
  end
168
177
 
169
- def with_mapper(mode, mapper)
178
+ def with_mapper(mapper)
170
179
  clone(
171
- mode: mode, schema_mapper: mapper || opts[:schema_mapper],
180
+ schema_mapper: mapper || opts[:schema_mapper],
172
181
  ).with_row_proc(
173
182
  Proc.new { |hash|
174
183
  m = mapper || opts[:schema_mapper]
@@ -176,9 +185,7 @@ module ReeDao
176
185
  if m
177
186
  entity = m.db_load(hash)
178
187
 
179
- if mode == :write
180
- self.set_persistence_state(entity, hash)
181
- end
188
+ self.set_entity_cache(entity, hash)
182
189
 
183
190
  entity
184
191
  else
@@ -188,34 +195,27 @@ module ReeDao
188
195
  )
189
196
  end
190
197
 
191
- def extract_changes(entity, hash)
192
- return hash unless entity.instance_variable_defined?(PERSISTENCE_STATE_VARIABLE)
193
- changes = {}
194
-
195
- persistence_state = entity.instance_variable_get(PERSISTENCE_STATE_VARIABLE)
196
-
197
- hash.each do |column, value|
198
- previous_column_value = persistence_state[column]
199
-
200
- if persistence_state.has_key?(column) && previous_column_value != value
201
- changes[column] = value
202
- end
198
+ def extract_primary_key(entity)
199
+ if primary_key.is_a?(Array)
200
+ primary_key.map do |key|
201
+ entity.send(key)
202
+ end.join("_")
203
+ else
204
+ entity.send(primary_key)
203
205
  end
204
-
205
- changes
206
206
  end
207
207
 
208
- def set_persistence_state(entity, raw)
208
+ def set_entity_cache(entity, raw)
209
209
  if !entity.is_a?(Integer) && !entity.is_a?(Symbol)
210
- entity.instance_variable_set(PERSISTENCE_STATE_VARIABLE, raw)
210
+ __ree_dao_cache.set(table_name, extract_primary_key(entity), raw)
211
211
  end
212
212
  end
213
213
 
214
- def update_persistence_state(entity, raw)
215
- persistence_state = entity.instance_variable_get(PERSISTENCE_STATE_VARIABLE)
214
+ def update_entity_cache(entity, raw)
215
+ cache = __ree_dao_cache.get(table_name, extract_primary_key(entity))
216
216
 
217
- if persistence_state
218
- persistence_state.merge!(raw)
217
+ if cache
218
+ __ree_dao_cache.set(table_name, extract_primary_key(entity), cache.merge(raw))
219
219
  end
220
220
  end
221
221
 
@@ -16,7 +16,7 @@ class ReeDao::BuildPgConnection
16
16
  user?: String,
17
17
  password?: String,
18
18
  host?: String,
19
- port?: String,
19
+ port?: Or[String, Integer],
20
20
  convert_infinite_timestamps?: Or[:string, :nil, :float],
21
21
  connect_timeout?: Integer,
22
22
  driver_options?: Hash,
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ReeDao::DropCache
4
+ include Ree::FnDSL
5
+
6
+ fn :drop_cache do
7
+ link :dao_cache
8
+ end
9
+
10
+ contract(None => nil)
11
+ def call
12
+ dao_cache.drop_thread_group_cache(Thread.current.group)
13
+ nil
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ReeDao::ExtractChanges
4
+ include Ree::FnDSL
5
+
6
+ fn :extract_changes do
7
+ link :dao_cache, as: :__ree_dao_cache
8
+ end
9
+
10
+ contract(Symbol, Any, Hash => Hash)
11
+ def call(table_name, primary_key, hash)
12
+ cached = __ree_dao_cache.get(table_name, primary_key)
13
+ return hash unless cached
14
+ changes = {}
15
+
16
+ hash.each do |column, value|
17
+ previous_column_value = cached[column]
18
+
19
+ if cached.has_key?(column) && previous_column_value != value
20
+ changes[column] = value
21
+ end
22
+ end
23
+
24
+ changes
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ReeDao::InitCache
4
+ include Ree::FnDSL
5
+
6
+ fn :init_cache do
7
+ link :dao_cache
8
+ end
9
+
10
+ contract(None => nil)
11
+ def call
12
+ dao_cache.add_thread_group_cache(Thread.current.group)
13
+ nil
14
+ end
15
+ end
@@ -11,7 +11,16 @@ module ReeDao
11
11
  depends_on :ree_enum
12
12
  depends_on :ree_mapper
13
13
  depends_on :ree_string
14
+ depends_on :ree_object
14
15
  end
15
16
 
16
17
  require_relative "./ree_dao/dsl"
18
+
19
+ def self.init_cache(thread)
20
+ ReeDao::Cache.init_cache(thread)
21
+ end
22
+
23
+ def self.drop_cache(thread)
24
+ ReeDao::Cache.delete_cache(thread)
25
+ end
17
26
  end
@@ -0,0 +1,22 @@
1
+ {
2
+ "schema_type": "object",
3
+ "schema_version": "1.1",
4
+ "name": "dao_cache",
5
+ "path": "packages/ree_dao/package/ree_dao/beans/dao_cache.rb",
6
+ "mount_as": "object",
7
+ "class": "ReeDao::DaoCache",
8
+ "factory": null,
9
+ "methods": [
10
+
11
+ ],
12
+ "links": [
13
+ {
14
+ "target": "deep_dup",
15
+ "package_name": "ree_object",
16
+ "as": "deep_dup",
17
+ "imports": [
18
+
19
+ ]
20
+ }
21
+ ]
22
+ }
@@ -17,7 +17,7 @@
17
17
  {
18
18
  "arg": "conn_opts",
19
19
  "arg_type": "req",
20
- "type": "{:conn_str? => String, :adapter => String, :database? => String, :encoding? => String, :user? => String, :password? => String, :host? => String, :port? => String, :convert_infinite_timestamps? => Or[string, nil, float], :connect_timeout? => Integer, :driver_options? => Hash, :notice_receiver? => Proc, :sslmode? => Or[disable, allow, prefer, require, verify-ca, verify-full...], :sslrootcert? => String, :search_path? => String, :use_iso_date_format? => Bool, :max_connections? => Integer}"
20
+ "type": "{:conn_str? => String, :adapter => String, :database? => String, :encoding? => String, :user? => String, :password? => String, :host? => String, :port? => Or[String, Integer], :convert_infinite_timestamps? => Or[string, nil, float], :connect_timeout? => Integer, :driver_options? => Hash, :notice_receiver? => Proc, :sslmode? => Or[disable, allow, prefer, require, verify-ca, verify-full...], :sslrootcert? => String, :search_path? => String, :use_iso_date_format? => Bool, :max_connections? => Integer}"
21
21
  },
22
22
  {
23
23
  "arg": "opts",
@@ -0,0 +1,31 @@
1
+ {
2
+ "schema_type": "object",
3
+ "schema_version": "1.1",
4
+ "name": "drop_cache",
5
+ "path": "packages/ree_dao/package/ree_dao/functions/drop_cache.rb",
6
+ "mount_as": "fn",
7
+ "class": "ReeDao::DropCache",
8
+ "factory": null,
9
+ "methods": [
10
+ {
11
+ "doc": "",
12
+ "throws": [
13
+
14
+ ],
15
+ "return": "Any",
16
+ "args": [
17
+
18
+ ]
19
+ }
20
+ ],
21
+ "links": [
22
+ {
23
+ "target": "dao_cache",
24
+ "package_name": "ree_dao",
25
+ "as": "dao_cache",
26
+ "imports": [
27
+
28
+ ]
29
+ }
30
+ ]
31
+ }
@@ -0,0 +1,45 @@
1
+ {
2
+ "schema_type": "object",
3
+ "schema_version": "1.1",
4
+ "name": "extract_changes",
5
+ "path": "packages/ree_dao/package/ree_dao/functions/extract_changes.rb",
6
+ "mount_as": "fn",
7
+ "class": "ReeDao::ExtractChanges",
8
+ "factory": null,
9
+ "methods": [
10
+ {
11
+ "doc": "",
12
+ "throws": [
13
+
14
+ ],
15
+ "return": "Hash",
16
+ "args": [
17
+ {
18
+ "arg": "table_name",
19
+ "arg_type": "req",
20
+ "type": "Symbol"
21
+ },
22
+ {
23
+ "arg": "primary_key",
24
+ "arg_type": "req",
25
+ "type": "Any"
26
+ },
27
+ {
28
+ "arg": "hash",
29
+ "arg_type": "req",
30
+ "type": "Hash"
31
+ }
32
+ ]
33
+ }
34
+ ],
35
+ "links": [
36
+ {
37
+ "target": "dao_cache",
38
+ "package_name": "ree_dao",
39
+ "as": "__ree_dao_cache",
40
+ "imports": [
41
+
42
+ ]
43
+ }
44
+ ]
45
+ }
@@ -0,0 +1,31 @@
1
+ {
2
+ "schema_type": "object",
3
+ "schema_version": "1.1",
4
+ "name": "init_cache",
5
+ "path": "packages/ree_dao/package/ree_dao/functions/init_cache.rb",
6
+ "mount_as": "fn",
7
+ "class": "ReeDao::InitCache",
8
+ "factory": null,
9
+ "methods": [
10
+ {
11
+ "doc": "",
12
+ "throws": [
13
+
14
+ ],
15
+ "return": "Any",
16
+ "args": [
17
+
18
+ ]
19
+ }
20
+ ],
21
+ "links": [
22
+ {
23
+ "target": "dao_cache",
24
+ "package_name": "ree_dao",
25
+ "as": "dao_cache",
26
+ "imports": [
27
+
28
+ ]
29
+ }
30
+ ]
31
+ }
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ package_require("ree_dao/wrappers/pg_jsonb")
4
+ package_require("ree_dao/wrappers/pg_array")
5
+
6
+ RSpec.describe :build_pg_connection do
7
+ link :build_pg_connection, from: :ree_dao
8
+
9
+ DB_CONFIG = {
10
+ host: "localhost",
11
+ user: "postgres",
12
+ database: "postgres",
13
+ password: "password",
14
+ adapter: "postgres"
15
+ }.freeze
16
+
17
+ after do
18
+ Ree.disable_irb_mode
19
+ end
20
+
21
+ before :all do
22
+ connection = build_pg_connection(
23
+ DB_CONFIG,
24
+ **{extensions: [:pg_array, :pg_json]}
25
+ )
26
+
27
+ if connection.table_exists?(:products)
28
+ connection.drop_table(:products)
29
+ end
30
+
31
+ connection.create_table :products do
32
+ primary_key :id
33
+
34
+ column :title, 'varchar(256)'
35
+ column :info, :jsonb
36
+ column :labels, :"varchar(30)[]"
37
+ end
38
+
39
+ connection.disconnect
40
+ end
41
+
42
+ Ree.enable_irb_mode
43
+
44
+ module ReeDaoTestPg
45
+ include Ree::PackageDSL
46
+
47
+ package do
48
+ depends_on :ree_dao
49
+ depends_on :ree_mapper
50
+ end
51
+ end
52
+
53
+ class ReeDaoTestPg::MapperFactory
54
+ include Ree::BeanDSL
55
+
56
+ bean :mapper_factory do
57
+ factory :build
58
+ singleton
59
+
60
+ link :build_mapper_factory, from: :ree_mapper
61
+ link :build_mapper_strategy, from: :ree_mapper
62
+ end
63
+
64
+ def build
65
+ factory = build_mapper_factory(strategies: [
66
+ build_mapper_strategy(method: :cast, dto: Hash),
67
+ build_mapper_strategy(method: :serialize, dto: Hash),
68
+ build_mapper_strategy(method: :db_dump, dto: Hash),
69
+ build_mapper_strategy(method: :db_load, dto: Object, always_optional: true)
70
+ ])
71
+
72
+ factory
73
+ .register_wrapper(:pg_jsonb, ReeDao::PgJsonb)
74
+ .register_wrapper(:pg_array, ReeDao::PgArray)
75
+
76
+ factory
77
+ end
78
+ end
79
+
80
+ require "sequel/extensions/pg_json_ops"
81
+ require "sequel/extensions/pg_array_ops"
82
+
83
+ class ReeDaoTestPg::Db
84
+ include Ree::BeanDSL
85
+
86
+ bean :db do
87
+ singleton
88
+ factory :build
89
+
90
+ link :build_pg_connection, from: :ree_dao
91
+ end
92
+
93
+ def build
94
+ Sequel.extension :pg_json_ops
95
+ Sequel.extension :pg_array_ops
96
+
97
+ build_pg_connection(
98
+ DB_CONFIG,
99
+ **{extensions: [:pg_array, :pg_json]}
100
+ )
101
+ end
102
+ end
103
+
104
+ class ReeDaoTestPg::Product
105
+ include ReeDto::EntityDSL
106
+
107
+ properties(
108
+ id: Nilor[Integer],
109
+ title: String,
110
+ info: Hash,
111
+ labels: ArrayOf[String]
112
+ )
113
+
114
+ attr_accessor :title, :info
115
+ end
116
+
117
+ class ReeDaoTestPg::ProductsDao
118
+ include ReeDao::DSL
119
+
120
+ dao :products_dao do
121
+ link :db
122
+ end
123
+
124
+ schema ReeDaoTestPg::Product do
125
+ integer :id, null: true
126
+ string :title
127
+ pg_jsonb :info, any
128
+ pg_array :labels, string
129
+ end
130
+ end
131
+
132
+ let(:products_dao) { ReeDaoTestPg::ProductsDao.new }
133
+
134
+ it {
135
+ products_dao.delete_all
136
+
137
+ product = ReeDaoTestPg::Product.new(title: "Product", info: { price: 1337, count: 200 }, labels: ["Sale"])
138
+ products_dao.put(product)
139
+
140
+ product.info[:price] = 1440
141
+ product.labels << "New"
142
+
143
+ products_dao.update(product)
144
+
145
+ product = products_dao.find(product.id)
146
+ expect(product.title).to eq("Product")
147
+ expect(product.info[:price]).to eq(1440)
148
+ expect(product.labels).to eq(["Sale", "New"])
149
+ }
150
+ end
@@ -5,6 +5,7 @@ package_require('ree_dto/entity_dsl')
5
5
 
6
6
  RSpec.describe :build_sqlite_connection do
7
7
  link :build_sqlite_connection, from: :ree_dao
8
+ link :dao_cache, from: :ree_dao
8
9
 
9
10
  after do
10
11
  Ree.disable_irb_mode
@@ -140,7 +141,7 @@ RSpec.describe :build_sqlite_connection do
140
141
 
141
142
  expect(u).to be_a(ReeDaoTest::User)
142
143
 
143
- state = u.instance_variable_get(:@persistence_state)
144
+ state = dao_cache.get(:users, u.id)
144
145
 
145
146
  expect(state).to be_a(Hash)
146
147
  expect(state[:id]).to eq(user.id)
@@ -154,16 +155,16 @@ RSpec.describe :build_sqlite_connection do
154
155
  user = ReeDaoTest::User.new(name: 'John', age: 30)
155
156
  dao.put(user)
156
157
 
157
- u = dao.find(user.id, :read)
158
+ u = dao.find(user.id)
158
159
  expect(u).to be_a(ReeDaoTest::User)
159
- expect(u.instance_variable_get(:@persistence_state)).to eq(nil)
160
+ expect(dao_cache.get(:users, u.id)).to_not eq(nil)
160
161
 
161
162
  all = dao.all
162
163
  expect(all.size).to eq(1)
163
164
  u = all.first
164
165
 
165
166
  expect(u).to be_a(ReeDaoTest::User)
166
- expect(u.instance_variable_get(:@persistence_state)).to be_a(Hash)
167
+ expect(dao_cache.get(:users, u.id)).to be_a(Hash)
167
168
  }
168
169
 
169
170
  it {
@@ -246,7 +247,7 @@ RSpec.describe :build_sqlite_connection do
246
247
  }
247
248
  end
248
249
 
249
- context "uodate by entity" do
250
+ context "update by entity" do
250
251
  it {
251
252
  dao.delete_all
252
253
 
@@ -82,6 +82,10 @@ module ReeMapper
82
82
  pckg_name = ReeString::Underscore.new.call(mod.name)
83
83
  factory_path = "#{pckg_name}/mapper_factory"
84
84
 
85
+ if Ree.irb_mode? && Object.const_defined?("#{mod.name}::MapperFactory") && mod != self
86
+ return Object.const_get("#{mod.name}::MapperFactory").new
87
+ end
88
+
85
89
  mapper_factory_klass = if package_file_exists?(factory_path) && mod != self
86
90
  package_require(factory_path)
87
91
  Object.const_get("#{mod.name}::MapperFactory")
@@ -16,7 +16,10 @@ RSpec.describe ReeRoda::App do
16
16
  module ReeRodaTest
17
17
  include Ree::PackageDSL
18
18
 
19
- package
19
+ package do
20
+ depends_on :ree_actions
21
+ depends_on :ree_dao
22
+ end
20
23
  end
21
24
 
22
25
  class ReeRodaTest::Cmd
@@ -11,7 +11,10 @@ RSpec.describe :build_routing_tree do
11
11
  module ReeRodaTestTree
12
12
  include Ree::PackageDSL
13
13
 
14
- package
14
+ package do
15
+ depends_on :ree_actions
16
+ depends_on :ree_dao
17
+ end
15
18
  end
16
19
 
17
20
  class ReeRodaTestTree::Cmd
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ReeLib
4
- VERSION = "1.0.42"
4
+ VERSION = "1.0.43"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ree_lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.42
4
+ version: 1.0.43
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruslan Gatiyatov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-23 00:00:00.000000000 Z
11
+ date: 2023-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ree
@@ -293,26 +293,35 @@ files:
293
293
  - lib/ree_lib/packages/ree_dao/bin/console
294
294
  - lib/ree_lib/packages/ree_dao/package/ree_dao.rb
295
295
  - lib/ree_lib/packages/ree_dao/package/ree_dao/beans/connections.rb
296
+ - lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
296
297
  - lib/ree_lib/packages/ree_dao/package/ree_dao/dataset_extensions.rb
297
298
  - lib/ree_lib/packages/ree_dao/package/ree_dao/dsl.rb
298
299
  - lib/ree_lib/packages/ree_dao/package/ree_dao/functions/build_connection.rb
299
300
  - lib/ree_lib/packages/ree_dao/package/ree_dao/functions/build_dao.rb
300
301
  - lib/ree_lib/packages/ree_dao/package/ree_dao/functions/build_pg_connection.rb
301
302
  - lib/ree_lib/packages/ree_dao/package/ree_dao/functions/build_sqlite_connection.rb
303
+ - lib/ree_lib/packages/ree_dao/package/ree_dao/functions/drop_cache.rb
304
+ - lib/ree_lib/packages/ree_dao/package/ree_dao/functions/extract_changes.rb
305
+ - lib/ree_lib/packages/ree_dao/package/ree_dao/functions/init_cache.rb
302
306
  - lib/ree_lib/packages/ree_dao/package/ree_dao/functions/one_to_many.rb
303
307
  - lib/ree_lib/packages/ree_dao/package/ree_dao/functions/one_to_one.rb
304
308
  - lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
305
309
  - lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb
306
310
  - lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb
307
311
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/beans/connections.schema.json
312
+ - lib/ree_lib/packages/ree_dao/schemas/ree_dao/beans/dao_cache.schema.json
308
313
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_connection.schema.json
309
314
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_dao.schema.json
310
315
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_pg_connection.schema.json
311
316
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/build_sqlite_connection.schema.json
317
+ - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/drop_cache.schema.json
318
+ - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/extract_changes.schema.json
319
+ - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/init_cache.schema.json
312
320
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/one_to_many.schema.json
313
321
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/one_to_one.schema.json
314
322
  - lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/persist_assoc.schema.json
315
323
  - lib/ree_lib/packages/ree_dao/spec/package_schema_spec.rb
324
+ - lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/build_pg_connection_spec.rb
316
325
  - lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/build_sqlite_connection/max_connections_spec.rb
317
326
  - lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/build_sqlite_connection_spec.rb
318
327
  - lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/one_to_many_spec.rb