esse-active_record 0.3.5 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/Gemfile.lock +1 -1
- data/README.md +29 -0
- data/ci/Gemfile.rails-5.2.lock +1 -1
- data/ci/Gemfile.rails-6.0.lock +1 -1
- data/ci/Gemfile.rails-6.1.lock +1 -1
- data/ci/Gemfile.rails-7.0.lock +1 -1
- data/ci/Gemfile.rails-7.1.lock +1 -1
- data/lib/esse/active_record/callbacks/indexing_on_update.rb +1 -8
- data/lib/esse/active_record/callbacks.rb +2 -1
- data/lib/esse/active_record/collection.rb +35 -8
- data/lib/esse/active_record/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 412017405772bf674a32c43510c0fdd15c0d1ac9de09357e523aa87c7d8bb0ab
|
4
|
+
data.tar.gz: 869613dcb7b78c03097989dbeb2d565dc790b119043d7a1a7e7183ccdca1ba5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3247f133ad930ea4ac1e69d3a242113c7ac26bc8de33737f350321fbe3b9de01ec21648310ff1b7c7ba326bca763107b4c313c663ae82686bb24da7d47e1cd3
|
7
|
+
data.tar.gz: 63593435b2cea9284e530351398960f5b73d2aceaad6cb336b30cec90e11cb419b4ad7a55a80265a91cb5b2b9c7704572758129d135ddc5f611e0f98f1be0de6
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## 0.3.7 - 2024-08-05
|
8
|
+
* Add `connected_to` to the collection for custom connection handling
|
9
|
+
|
10
|
+
## 0.0.1
|
11
|
+
The first release of the esse-active_record plugin
|
12
|
+
* Added: Initial implementation of the plugin
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -210,6 +210,35 @@ User.without_indexing(AccountsIndex) do
|
|
210
210
|
end
|
211
211
|
```
|
212
212
|
|
213
|
+
### Asynchronous Indexing
|
214
|
+
|
215
|
+
If you are using a background job processor like Sidekiq or Faktory, you may be interested in indexing documents asynchronously. For this, you can use the [esse-async_indexing](https://github.com/marcosgz/esse-async_indexing) gem.
|
216
|
+
|
217
|
+
Add the `esse-async_indexing` gem to your Gemfile and require the `esse/async_indexing/active_record` file in your application initialization. Make sure to setup the gem configurationg according to the [esse-async_indexing documentation](https://github.com/marcosgz/esse-async_indexing).
|
218
|
+
|
219
|
+
|
220
|
+
```ruby
|
221
|
+
require 'esse/async_indexing/active_record'
|
222
|
+
```
|
223
|
+
|
224
|
+
Then, you can use the `async_index_callback` or `async_update_lazy_attribute_callback` methods to push the indexing job to the background job processor.
|
225
|
+
|
226
|
+
```diff
|
227
|
+
class City < ApplicationRecord
|
228
|
+
include Esse::ActiveRecord::Model
|
229
|
+
- include Esse::ActiveRecord::Model
|
230
|
+
+ include Esse::AsyncIndexing::ActiveRecord::Model
|
231
|
+
|
232
|
+
belongs_to :state, optional: true
|
233
|
+
|
234
|
+
|
235
|
+
async_indexing_callback('geos_index:city') { id }
|
236
|
+
- index_callback('geos_index:city') { id }
|
237
|
+
- update_lazy_attribute_callback('geos_index:state', 'cities_count', if: :state_id?) { state_id }
|
238
|
+
+ async_index_callback('geos_index:city', service_name: :sidekiq) { id }
|
239
|
+
+ async_update_lazy_attribute_callback('geos_index:state', 'cities_count', if: :state_id?, service_name: :sidekiq) { state_id }
|
240
|
+
end
|
241
|
+
```
|
213
242
|
|
214
243
|
## Development
|
215
244
|
|
data/ci/Gemfile.rails-5.2.lock
CHANGED
data/ci/Gemfile.rails-6.0.lock
CHANGED
data/ci/Gemfile.rails-6.1.lock
CHANGED
data/ci/Gemfile.rails-7.0.lock
CHANGED
data/ci/Gemfile.rails-7.1.lock
CHANGED
@@ -3,13 +3,6 @@
|
|
3
3
|
module Esse::ActiveRecord
|
4
4
|
module Callbacks
|
5
5
|
class IndexingOnUpdate < Callback
|
6
|
-
attr_reader :update_with
|
7
|
-
|
8
|
-
def initialize(with: :index, **kwargs, &block)
|
9
|
-
@update_with = with
|
10
|
-
super(**kwargs, &block)
|
11
|
-
end
|
12
|
-
|
13
6
|
def call(model)
|
14
7
|
record = block_result || model
|
15
8
|
|
@@ -43,7 +36,7 @@ module Esse::ActiveRecord
|
|
43
36
|
def update_document(document)
|
44
37
|
return if document.ignore_on_index?
|
45
38
|
|
46
|
-
if
|
39
|
+
if @with == :update || (@with.nil? && repo.lazy_document_attributes.any?)
|
47
40
|
begin
|
48
41
|
repo.index.update(document, **options)
|
49
42
|
rescue Esse::Transport::NotFoundError
|
@@ -5,8 +5,9 @@ module Esse
|
|
5
5
|
class Callback
|
6
6
|
attr_reader :repo, :options, :block_result
|
7
7
|
|
8
|
-
def initialize(repo:, block_result: nil, **kwargs)
|
8
|
+
def initialize(repo:, block_result: nil, with: nil, **kwargs)
|
9
9
|
@repo = repo
|
10
|
+
@with = with
|
10
11
|
@options = kwargs
|
11
12
|
@block_result = block_result
|
12
13
|
end
|
@@ -21,6 +21,12 @@ module Esse
|
|
21
21
|
class_attribute :batch_contexts
|
22
22
|
self.batch_contexts = {}
|
23
23
|
|
24
|
+
# Connects to a database or role (ex writing, reading, or another custom role) for the collection query
|
25
|
+
# @param [Symbol] role The role to connect to
|
26
|
+
# @param [Symbol] shard The shard to connect to
|
27
|
+
class_attribute :connect_with
|
28
|
+
self.connect_with = nil
|
29
|
+
|
24
30
|
class << self
|
25
31
|
def inspect
|
26
32
|
return super unless self < Esse::ActiveRecord::Collection
|
@@ -40,6 +46,7 @@ module Esse
|
|
40
46
|
|
41
47
|
subclass.scopes = scopes.dup
|
42
48
|
subclass.batch_contexts = batch_contexts.dup
|
49
|
+
subclass.connect_with = connect_with&.dup
|
43
50
|
end
|
44
51
|
|
45
52
|
def scope(name, proc = nil, override: false, &block)
|
@@ -57,6 +64,10 @@ module Esse
|
|
57
64
|
|
58
65
|
batch_contexts[name.to_sym] = proc
|
59
66
|
end
|
67
|
+
|
68
|
+
def connected_to(**kwargs)
|
69
|
+
self.connect_with = kwargs
|
70
|
+
end
|
60
71
|
end
|
61
72
|
|
62
73
|
attr_reader :start, :finish, :batch_size, :params
|
@@ -74,23 +85,29 @@ module Esse
|
|
74
85
|
end
|
75
86
|
|
76
87
|
def each
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
88
|
+
with_connection do
|
89
|
+
dataset.find_in_batches(**batch_options) do |rows|
|
90
|
+
kwargs = params.dup
|
91
|
+
self.class.batch_contexts.each do |name, proc|
|
92
|
+
kwargs[name] = proc.call(rows, **params)
|
93
|
+
end
|
94
|
+
yield(rows, **kwargs)
|
81
95
|
end
|
82
|
-
yield(rows, **kwargs)
|
83
96
|
end
|
84
97
|
end
|
85
98
|
|
86
99
|
def each_batch_ids
|
87
|
-
|
88
|
-
|
100
|
+
with_connection do
|
101
|
+
dataset.select(:id).except(:includes, :preload, :eager_load).find_in_batches(**batch_options) do |rows|
|
102
|
+
yield(rows.map(&:id))
|
103
|
+
end
|
89
104
|
end
|
90
105
|
end
|
91
106
|
|
92
107
|
def count
|
93
|
-
|
108
|
+
with_connection do
|
109
|
+
dataset.except(:includes, :preload, :eager_load, :group, :order, :limit, :offset).count
|
110
|
+
end
|
94
111
|
end
|
95
112
|
alias_method :size, :count
|
96
113
|
|
@@ -127,6 +144,16 @@ module Esse
|
|
127
144
|
|
128
145
|
protected
|
129
146
|
|
147
|
+
def with_connection
|
148
|
+
if self.class.connect_with&.any?
|
149
|
+
::ActiveRecord::Base.connected_to(**self.class.connect_with) do
|
150
|
+
yield
|
151
|
+
end
|
152
|
+
else
|
153
|
+
yield
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
130
157
|
def batch_options
|
131
158
|
{
|
132
159
|
batch_size: batch_size
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: esse-active_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcos G. Zimmermann
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: esse
|
@@ -206,6 +206,7 @@ extensions: []
|
|
206
206
|
extra_rdoc_files: []
|
207
207
|
files:
|
208
208
|
- ".rubocop.yml"
|
209
|
+
- CHANGELOG.md
|
209
210
|
- Gemfile
|
210
211
|
- Gemfile.lock
|
211
212
|
- LICENSE.txt
|