n1_loader 1.7.1 → 1.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/lib/n1_loader/active_record/associations_preloader_v5.rb +2 -2
  3. data/lib/n1_loader/active_record/associations_preloader_v6.rb +2 -2
  4. data/lib/n1_loader/active_record/associations_preloader_v7.rb +2 -2
  5. data/lib/n1_loader/ar_lazy_preload/loadable.rb +5 -14
  6. data/lib/n1_loader/ar_lazy_preload.rb +1 -1
  7. data/lib/n1_loader/core/loadable.rb +16 -59
  8. data/lib/n1_loader/core/loader.rb +8 -5
  9. data/lib/n1_loader/core/loader_builder.rb +16 -0
  10. data/lib/n1_loader/core/preloader.rb +5 -8
  11. data/lib/n1_loader/version.rb +1 -1
  12. data/lib/n1_loader.rb +1 -1
  13. metadata +3 -37
  14. data/.github/workflows/rubocop.yml +0 -24
  15. data/.github/workflows/tests.yml +0 -66
  16. data/.gitignore +0 -12
  17. data/.rspec +0 -3
  18. data/.rubocop.yml +0 -20
  19. data/CHANGELOG.md +0 -129
  20. data/CODE_OF_CONDUCT.md +0 -84
  21. data/Gemfile +0 -15
  22. data/LICENSE.txt +0 -21
  23. data/README.md +0 -276
  24. data/Rakefile +0 -12
  25. data/activerecord-gemfiles/ar_5_latest.gemfile +0 -3
  26. data/activerecord-gemfiles/ar_6_latest.gemfile +0 -3
  27. data/activerecord-gemfiles/ar_7_latest.gemfile +0 -3
  28. data/ar_lazy_preload-gemfiles/ar_lazy_preload_0.6.1.gemfile +0 -3
  29. data/ar_lazy_preload-gemfiles/ar_lazy_preload_master.gemfile +0 -3
  30. data/bin/console +0 -15
  31. data/bin/setup +0 -8
  32. data/examples/active_record_integration.rb +0 -33
  33. data/examples/ar_lazy_integration.rb +0 -36
  34. data/examples/ar_lazy_integration_with_isolated_loader.rb +0 -39
  35. data/examples/arguments_support.rb +0 -67
  36. data/examples/context/service.rb +0 -20
  37. data/examples/context/setup_ar_lazy.rb +0 -15
  38. data/examples/context/setup_database.rb +0 -26
  39. data/examples/core.rb +0 -39
  40. data/examples/graphql.rb +0 -63
  41. data/examples/isolated_loader.rb +0 -13
  42. data/examples/lazy_loading.rb +0 -26
  43. data/examples/reloading.rb +0 -32
  44. data/examples/shared_loader.rb +0 -34
  45. data/examples/single_case.rb +0 -34
  46. data/guides/enhanced-activerecord.md +0 -266
  47. data/lib/n1_loader/core/name.rb +0 -14
  48. data/n1_loader.gemspec +0 -35
data/CODE_OF_CONDUCT.md DELETED
@@ -1,84 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
-
7
- We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
-
9
- ## Our Standards
10
-
11
- Examples of behavior that contributes to a positive environment for our community include:
12
-
13
- * Demonstrating empathy and kindness toward other people
14
- * Being respectful of differing opinions, viewpoints, and experiences
15
- * Giving and gracefully accepting constructive feedback
16
- * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
- * Focusing on what is best not just for us as individuals, but for the overall community
18
-
19
- Examples of unacceptable behavior include:
20
-
21
- * The use of sexualized language or imagery, and sexual attention or
22
- advances of any kind
23
- * Trolling, insulting or derogatory comments, and personal or political attacks
24
- * Public or private harassment
25
- * Publishing others' private information, such as a physical or email
26
- address, without their explicit permission
27
- * Other conduct which could reasonably be considered inappropriate in a
28
- professional setting
29
-
30
- ## Enforcement Responsibilities
31
-
32
- Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
-
34
- Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
-
36
- ## Scope
37
-
38
- This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
-
40
- ## Enforcement
41
-
42
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
43
-
44
- All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
-
46
- ## Enforcement Guidelines
47
-
48
- Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
-
50
- ### 1. Correction
51
-
52
- **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
-
54
- **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
-
56
- ### 2. Warning
57
-
58
- **Community Impact**: A violation through a single incident or series of actions.
59
-
60
- **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
-
62
- ### 3. Temporary Ban
63
-
64
- **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
-
66
- **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
-
68
- ### 4. Permanent Ban
69
-
70
- **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
-
72
- **Consequence**: A permanent ban from any sort of public interaction within the community.
73
-
74
- ## Attribution
75
-
76
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
- available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
-
79
- Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
-
81
- [homepage]: https://www.contributor-covenant.org
82
-
83
- For answers to common questions about this code of conduct, see the FAQ at
84
- https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile DELETED
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
-
5
- # Specify your gem's dependencies in n1_loader.gemspec
6
- gemspec
7
-
8
- # Hack to make Github work with Circle CI job names with slashes
9
- gemfiles = []
10
- gemfiles << "activerecord-gemfiles/#{ENV["ACTIVERECORD_GEMFILE"]}.gemfile" if ENV["ACTIVERECORD_GEMFILE"]
11
- gemfiles << "ar_lazy_preload-gemfiles/#{ENV["AR_LAZY_PRELOAD_GEMFILE"]}.gemfile" if ENV["AR_LAZY_PRELOAD_GEMFILE"]
12
-
13
- gemfiles.each do |path|
14
- eval(File.read(path)) # rubocop:disable Security/Eval
15
- end
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2021 TODO: Write your name
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
data/README.md DELETED
@@ -1,276 +0,0 @@
1
- # N1Loader
2
-
3
- [![Gem Version][3]][4]
4
- [![][11]][12]
5
- [![][13]][14]
6
- [![][9]][10]
7
-
8
- N1Loader is designed to provide a simple way for avoiding [N+1 issues][7] of any kind.
9
- For example, it can help with resolving N+1 for:
10
- - database querying (most common case)
11
- - 3rd party service calls
12
- - complex calculations
13
- - and many more
14
-
15
- > If the project helps you or your organization, I would be very grateful if you [contribute][15] or [donate][10].
16
- > Your support is an incredible motivation and the biggest reward for my hard work.
17
-
18
- ___Support:___ ActiveRecord 5, 6, and 7.
19
-
20
- Follow me and stay tuned for the updates:
21
- - [LinkedIn](https://www.linkedin.com/in/evgeniydemin/)
22
- - [Medium](https://evgeniydemin.medium.com/)
23
- - [Twitter](https://twitter.com/EvgeniyDemin/)
24
- - [GitHub](https://github.com/djezzzl)
25
-
26
- ## Killer feature for GraphQL API
27
-
28
- N1Loader in combination with [ArLazyPreload][6] is a killer feature for your GraphQL API.
29
- Give it a try now and see incredible results instantly! Check out the [example](examples/graphql.rb) and start benefiting from it in your projects!
30
-
31
- ```ruby
32
- gem 'n1_loader', require: 'n1_loader/ar_lazy_preload'
33
- ```
34
-
35
- ## Enhance [ActiveRecord][5]
36
-
37
- Are you working with well-known Rails application? Try it out and see how well N1Loader fulfills missing gaps when you can't define ActiveRecord associations!
38
- Check out the detailed [guide](guides/enhanced-activerecord.md) with examples or its [short version](examples/active_record_integration.rb).
39
-
40
- ```ruby
41
- gem 'n1_loader', require: 'n1_loader/active_record'
42
- ```
43
-
44
- Are you ready to forget about N+1 once and for all? Install [ArLazyPreload][6] and see dreams come true!
45
-
46
- ```ruby
47
- gem 'n1_loader', require: 'n1_loader/ar_lazy_preload'
48
- ```
49
-
50
- ## Standalone mode
51
-
52
- Are you not working with [ActiveRecord][5]? N1Loader is ready to be used as standalone solution! ([full snippet](examples/core.rb))
53
-
54
- ```ruby
55
- gem 'n1_loader'
56
- ```
57
-
58
- ## How to use it?
59
-
60
- N1Loader provides DSL that allows you to define N+1 ready loaders that can
61
- be injected into your objects in a way that you can avoid N+1 issues.
62
-
63
- > _Disclaimer_: examples below are working but designed to show N1Loader potentials only.
64
- In real live applications, N1Loader can be applied anywhere and in more [elegant way](examples/isolated_loader.rb).
65
-
66
- Let's look at simple example below ([full snippet](examples/active_record_integration.rb)):
67
- ```ruby
68
- class User < ActiveRecord::Base
69
- has_many :payments
70
-
71
- n1_optimized :payments_total do |users|
72
- total_per_user =
73
- Payment.group(:user_id)
74
- .where(user: users)
75
- .sum(:amount)
76
- .tap { |h| h.default = 0 }
77
-
78
- users.each do |user|
79
- total = total_per_user[user.id]
80
- fulfill(user, total)
81
- end
82
- end
83
- end
84
-
85
- class Payment < ActiveRecord::Base
86
- belongs_to :user
87
-
88
- validates :amount, presence: true
89
- end
90
-
91
- # A user has many payments.
92
- # Assuming, we want to know for group of users, what is a total of their payments, we can do the following:
93
-
94
- # Has N+1 issue
95
- p User.all.map { |user| user.payments.sum(&:amount) }
96
-
97
- # Has no N+1 but we load too many data that we don't actually need
98
- p User.all.includes(:payments).map { |user| user.payments.sum(&:amount) }
99
-
100
- # Has no N+1 and we load only what we need
101
- p User.all.includes(:payments_total).map { |user| user.payments_total }
102
- ```
103
-
104
- Let's assume now, that we want to calculate the total of payments for the given period for a group of users.
105
- N1Loader can do that as well! ([full snippet](examples/arguments_support.rb))
106
-
107
- ```ruby
108
- class User < ActiveRecord::Base
109
- has_many :payments
110
-
111
- n1_optimized :payments_total do
112
- argument :from
113
- argument :to
114
-
115
- def perform(users)
116
- total_per_user =
117
- Payment
118
- .group(:user_id)
119
- .where(created_at: from..to)
120
- .where(user: users)
121
- .sum(:amount)
122
- .tap { |h| h.default = 0 }
123
-
124
- users.each do |user|
125
- total = total_per_user[user.id]
126
- fulfill(user, total)
127
- end
128
- end
129
- end
130
- end
131
-
132
- class Payment < ActiveRecord::Base
133
- belongs_to :user
134
-
135
- validates :amount, presence: true
136
- end
137
-
138
- # Has N+1
139
- p User.all.map { |user| user.payments.select { |payment| payment.created_at >= from && payment.created_at <= to }.sum(&:amount) }
140
-
141
- # Has no N+1 but we load too many data that we don't need
142
- p User.all.includes(:payments).map { |user| user.payments.select { |payment| payment.created_at >= from && payment.created_at <= to }.sum(&:amount) }
143
-
144
- # Has no N+1 and calculation is the most efficient
145
- p User.all.includes(:payments_total).map { |user| user.payments_total(from: from, to: to) }
146
- ```
147
-
148
- ## Features and benefits
149
-
150
- - N1Loader doesn't use Promises which means it's easy to debug
151
- - Doesn't require injection to objects, can be used in [isolation](examples/isolated_loader.rb)
152
- - Loads data [lazily](examples/lazy_loading.rb)
153
- - Loaders can be [shared](examples/shared_loader.rb) between multiple classes
154
- - Loaded data can be [re-fetched](examples/reloading.rb)
155
- - Loader can be optimized for [single cases](examples/single_case.rb)
156
- - Loader support [arguments](examples/arguments_support.rb)
157
- - Has [integration](examples/active_record_integration.rb) with [ActiveRecord][5] which makes it brilliant
158
- - Has [integration](examples/ar_lazy_integration.rb) with [ArLazyPreload][6] which makes it excellent
159
-
160
- ### Feature killer for [ArLazyPreload][6] integration with isolated loaders
161
-
162
- In [version 1.6.0](CHANGELOG.md#160---20221019) isolated loaders were integrated with [ArLazyPreload][6] context.
163
- This means, it isn't required to inject `N1Loader` into your [ActiveRecord][5] models to avoid N+1 issues out of the box.
164
- It is especially great as many engineers are trying to avoid extra coupling between their models/services when it's possible.
165
- And this feature was designed exactly for this without losing an out of a box solution for N+1.
166
-
167
- Without further ado, please have a look at the [example](examples/ar_lazy_integration_with_isolated_loader.rb).
168
-
169
- _Spoiler:_ as soon as you have your loader defined, it will be as simple as `Loader.for(element)` to get your data efficiently and without N+1.
170
-
171
- ## Funding
172
-
173
- ### Open Collective Backers
174
-
175
- You're an individual who wants to support the project with a monthly donation. Your logo will be available on the Github page. [[Become a backer](https://opencollective.com/n1_loader#backer)]
176
-
177
- <a href="https://opencollective.com/n1_loader/backer/0/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/0/avatar.svg"></a>
178
- <a href="https://opencollective.com/n1_loader/backer/1/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/1/avatar.svg"></a>
179
- <a href="https://opencollective.com/n1_loader/backer/2/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/2/avatar.svg"></a>
180
- <a href="https://opencollective.com/n1_loader/backer/3/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/3/avatar.svg"></a>
181
- <a href="https://opencollective.com/n1_loader/backer/4/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/4/avatar.svg"></a>
182
- <a href="https://opencollective.com/n1_loader/backer/5/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/5/avatar.svg"></a>
183
- <a href="https://opencollective.com/n1_loader/backer/6/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/6/avatar.svg"></a>
184
- <a href="https://opencollective.com/n1_loader/backer/7/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/7/avatar.svg"></a>
185
- <a href="https://opencollective.com/n1_loader/backer/8/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/8/avatar.svg"></a>
186
- <a href="https://opencollective.com/n1_loader/backer/9/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/9/avatar.svg"></a>
187
- <a href="https://opencollective.com/n1_loader/backer/10/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/10/avatar.svg"></a>
188
- <a href="https://opencollective.com/n1_loader/backer/11/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/11/avatar.svg"></a>
189
- <a href="https://opencollective.com/n1_loader/backer/12/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/12/avatar.svg"></a>
190
- <a href="https://opencollective.com/n1_loader/backer/13/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/13/avatar.svg"></a>
191
- <a href="https://opencollective.com/n1_loader/backer/14/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/14/avatar.svg"></a>
192
- <a href="https://opencollective.com/n1_loader/backer/15/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/15/avatar.svg"></a>
193
- <a href="https://opencollective.com/n1_loader/backer/16/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/16/avatar.svg"></a>
194
- <a href="https://opencollective.com/n1_loader/backer/17/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/17/avatar.svg"></a>
195
- <a href="https://opencollective.com/n1_loader/backer/18/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/18/avatar.svg"></a>
196
- <a href="https://opencollective.com/n1_loader/backer/19/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/19/avatar.svg"></a>
197
- <a href="https://opencollective.com/n1_loader/backer/20/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/20/avatar.svg"></a>
198
- <a href="https://opencollective.com/n1_loader/backer/21/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/21/avatar.svg"></a>
199
- <a href="https://opencollective.com/n1_loader/backer/22/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/22/avatar.svg"></a>
200
- <a href="https://opencollective.com/n1_loader/backer/23/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/23/avatar.svg"></a>
201
- <a href="https://opencollective.com/n1_loader/backer/24/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/24/avatar.svg"></a>
202
- <a href="https://opencollective.com/n1_loader/backer/25/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/25/avatar.svg"></a>
203
- <a href="https://opencollective.com/n1_loader/backer/26/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/26/avatar.svg"></a>
204
- <a href="https://opencollective.com/n1_loader/backer/27/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/27/avatar.svg"></a>
205
- <a href="https://opencollective.com/n1_loader/backer/28/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/28/avatar.svg"></a>
206
- <a href="https://opencollective.com/n1_loader/backer/29/website" target="_blank"><img src="https://opencollective.com/n1_loader/backer/29/avatar.svg"></a>
207
-
208
- ### Open Collective Sponsors
209
-
210
- You're an organization that wants to support the project with a monthly donation. Your logo will be available on the Github page. [[Become a sponsor](https://opencollective.com/n1_loader#sponsor)]
211
-
212
- <a href="https://opencollective.com/n1_loader/sponsor/0/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/0/avatar.svg"></a>
213
- <a href="https://opencollective.com/n1_loader/sponsor/1/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/1/avatar.svg"></a>
214
- <a href="https://opencollective.com/n1_loader/sponsor/2/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/2/avatar.svg"></a>
215
- <a href="https://opencollective.com/n1_loader/sponsor/3/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/3/avatar.svg"></a>
216
- <a href="https://opencollective.com/n1_loader/sponsor/4/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/4/avatar.svg"></a>
217
- <a href="https://opencollective.com/n1_loader/sponsor/5/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/5/avatar.svg"></a>
218
- <a href="https://opencollective.com/n1_loader/sponsor/6/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/6/avatar.svg"></a>
219
- <a href="https://opencollective.com/n1_loader/sponsor/7/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/7/avatar.svg"></a>
220
- <a href="https://opencollective.com/n1_loader/sponsor/8/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/8/avatar.svg"></a>
221
- <a href="https://opencollective.com/n1_loader/sponsor/9/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/9/avatar.svg"></a>
222
- <a href="https://opencollective.com/n1_loader/sponsor/10/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/10/avatar.svg"></a>
223
- <a href="https://opencollective.com/n1_loader/sponsor/11/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/11/avatar.svg"></a>
224
- <a href="https://opencollective.com/n1_loader/sponsor/12/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/12/avatar.svg"></a>
225
- <a href="https://opencollective.com/n1_loader/sponsor/13/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/13/avatar.svg"></a>
226
- <a href="https://opencollective.com/n1_loader/sponsor/14/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/14/avatar.svg"></a>
227
- <a href="https://opencollective.com/n1_loader/sponsor/15/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/15/avatar.svg"></a>
228
- <a href="https://opencollective.com/n1_loader/sponsor/16/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/16/avatar.svg"></a>
229
- <a href="https://opencollective.com/n1_loader/sponsor/17/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/17/avatar.svg"></a>
230
- <a href="https://opencollective.com/n1_loader/sponsor/18/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/18/avatar.svg"></a>
231
- <a href="https://opencollective.com/n1_loader/sponsor/19/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/19/avatar.svg"></a>
232
- <a href="https://opencollective.com/n1_loader/sponsor/20/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/20/avatar.svg"></a>
233
- <a href="https://opencollective.com/n1_loader/sponsor/21/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/21/avatar.svg"></a>
234
- <a href="https://opencollective.com/n1_loader/sponsor/22/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/22/avatar.svg"></a>
235
- <a href="https://opencollective.com/n1_loader/sponsor/23/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/23/avatar.svg"></a>
236
- <a href="https://opencollective.com/n1_loader/sponsor/24/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/24/avatar.svg"></a>
237
- <a href="https://opencollective.com/n1_loader/sponsor/25/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/25/avatar.svg"></a>
238
- <a href="https://opencollective.com/n1_loader/sponsor/26/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/26/avatar.svg"></a>
239
- <a href="https://opencollective.com/n1_loader/sponsor/27/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/27/avatar.svg"></a>
240
- <a href="https://opencollective.com/n1_loader/sponsor/28/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/28/avatar.svg"></a>
241
- <a href="https://opencollective.com/n1_loader/sponsor/29/website" target="_blank"><img src="https://opencollective.com/n1_loader/sponsor/29/avatar.svg"></a>
242
-
243
- ## Contributing
244
-
245
- Bug reports and pull requests are welcome on GitHub at https://github.com/djezzzl/n1_loader.
246
- This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).
247
-
248
- ## License
249
-
250
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
251
-
252
- ## Code of Conduct
253
-
254
- Everyone interacting in the N1Loader project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
255
-
256
- ## Changelog
257
-
258
- *N1Loader*'s changelog is available [here](CHANGELOG.md).
259
-
260
- ## Copyright
261
-
262
- Copyright (c) Evgeniy Demin. See [LICENSE.txt](LICENSE.txt) for further details.
263
-
264
- [3]: https://badge.fury.io/rb/n1_loader.svg
265
- [4]: https://badge.fury.io/rb/n1_loader
266
- [5]: https://github.com/rails/rails/tree/main/activerecord
267
- [6]: https://github.com/DmitryTsepelev/ar_lazy_preload
268
- [7]: https://stackoverflow.com/questions/97197/what-is-the-n1-selects-problem-in-orm-object-relational-mapping
269
- [8]: https://github.com/djezzzl/n1_loader
270
- [9]: https://opencollective.com/n1_loader/tiers/badge.svg
271
- [10]: https://opencollective.com/n1_loader#support
272
- [11]: https://github.com/djezzzl/n1_loader/actions/workflows/tests.yml/badge.svg?branch=master
273
- [12]: https://github.com/djezzzl/n1_loader/actions/workflows/tests.yml?query=event%3Aschedule
274
- [13]: https://github.com/djezzzl/n1_loader/actions/workflows/rubocop.yml/badge.svg?branch=master
275
- [14]: https://github.com/djezzzl/n1_loader/actions/workflows/rubocop.yml?query=event%3Aschedule
276
- [15]: https://github.com/djezzzl/n1_loader#contributing
data/Rakefile DELETED
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- require "rubocop/rake_task"
9
-
10
- RuboCop::RakeTask.new
11
-
12
- task default: %i[spec rubocop]
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- gem "activerecord", "~> 5"
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- gem "activerecord", "~> 6"
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- gem "activerecord", "~> 7"
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- gem "ar_lazy_preload", "= 0.6.1"
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- gem "ar_lazy_preload", git: "https://github.com/DmitryTsepelev/ar_lazy_preload", branch: "master"
data/bin/console DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require "bundler/setup"
5
- require "n1_loader"
6
-
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
-
10
- # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
13
-
14
- require "irb"
15
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "n1_loader/active_record"
4
-
5
- require_relative 'context/setup_database'
6
-
7
- class User < ActiveRecord::Base
8
- has_many :payments
9
-
10
- n1_optimized :payments_total do |users|
11
- total_per_user = Payment.group(:user_id).where(user: users).sum(:amount).tap { |h| h.default = 0 }
12
-
13
- users.each do |user|
14
- total = total_per_user[user.id]
15
- fulfill(user, total)
16
- end
17
- end
18
- end
19
-
20
- class Payment < ActiveRecord::Base
21
- belongs_to :user
22
-
23
- validates :amount, presence: true
24
- end
25
-
26
- fill_database
27
-
28
- # Has N+1
29
- p User.all.map { |user| user.payments.sum(&:amount) }
30
- # Has no N+1 but we load too many data that we don't need
31
- p User.all.includes(:payments).map { |user| user.payments.sum(&:amount) }
32
- # Has no N+1 and calculation is the most efficient
33
- p User.all.includes(:payments_total).map(&:payments_total)
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "n1_loader/ar_lazy_preload"
4
-
5
- require_relative 'context/setup_ar_lazy'
6
- require_relative 'context/setup_database'
7
-
8
- class User < ActiveRecord::Base
9
- has_many :payments
10
-
11
- n1_optimized :payments_total do |users|
12
- total_per_user = Payment.group(:user_id).where(user: users).sum(:amount).tap { |h| h.default = 0 }
13
-
14
- users.each do |user|
15
- total = total_per_user[user.id]
16
- fulfill(user, total)
17
- end
18
- end
19
- end
20
-
21
- class Payment < ActiveRecord::Base
22
- belongs_to :user
23
-
24
- validates :amount, presence: true
25
- end
26
-
27
- fill_database
28
-
29
- # Has N+1
30
- p User.all.map { |user| user.payments.sum(&:amount) }
31
-
32
- # Has no N+1 and loads only required data
33
- p User.preload_associations_lazily.map(&:payments_total)
34
- # or
35
- ArLazyPreload.config.auto_preload = true
36
- User.all.map(&:payments_total)
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "n1_loader/ar_lazy_preload"
4
-
5
- require_relative 'context/setup_ar_lazy'
6
- require_relative 'context/setup_database'
7
-
8
- class Loader < N1Loader::Loader
9
- def perform(users)
10
- total_per_user = Payment.group(:user_id).where(user: users).sum(:amount).tap { |h| h.default = 0 }
11
-
12
- users.each do |user|
13
- total = total_per_user[user.id]
14
- fulfill(user, total)
15
- end
16
- end
17
- end
18
-
19
- class User < ActiveRecord::Base
20
- has_many :payments
21
- end
22
-
23
- class Payment < ActiveRecord::Base
24
- belongs_to :user
25
-
26
- validates :amount, presence: true
27
- end
28
-
29
- fill_database
30
-
31
- # Has N+1 and loads redundant data
32
- p User.all.map { |user| user.payments.sum(&:amount) }
33
-
34
- # Has no N+1 and loads only required data
35
- p User.preload_associations_lazily.all.map { |user| Loader.for(user) }
36
-
37
- # or
38
- ArLazyPreload.config.auto_preload = true
39
- p User.all.map { |user| Loader.for(user) }
@@ -1,67 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "n1_loader/active_record"
4
-
5
- require_relative 'context/setup_database'
6
-
7
- class User < ActiveRecord::Base
8
- has_many :payments
9
-
10
- n1_optimized :payments_total do
11
- # Arguments can be:
12
- # argument :something, optional: true
13
- # argument :something, default: -> { 100 }
14
- #
15
- # Note: do not use mutable (mostly timing related) defaults like:
16
- # argument :from, default -> { 2.minutes.from_now }
17
- # because such values will be unique for every loader call which will make N+1 issue stay
18
- argument :from
19
- argument :to
20
-
21
- # This is used to define logic how loaders are compared to each other
22
- # default is:
23
- # cache_key { *arguments.map(&:object_id) }
24
- cache_key { [from, to] }
25
-
26
- def perform(users)
27
- total_per_user =
28
- Payment
29
- .group(:user_id)
30
- .where(created_at: from..to)
31
- .where(user: users)
32
- .sum(:amount)
33
- .tap { |h| h.default = 0 }
34
-
35
- users.each do |user|
36
- total = total_per_user[user.id]
37
- fulfill(user, total)
38
- end
39
- end
40
- end
41
- end
42
-
43
- class Payment < ActiveRecord::Base
44
- belongs_to :user
45
-
46
- validates :amount, presence: true
47
- end
48
-
49
- fill_database
50
-
51
- from = 2.days.ago
52
- to = 1.day.ago
53
-
54
- # Has N+1
55
- p User.all.map { |user|
56
- user.payments.select do |payment|
57
- payment.created_at >= from && payment.created_at <= to
58
- end.sum(&:amount)
59
- }
60
- # Has no N+1 but we load too many data that we don't need
61
- p User.all.includes(:payments).map { |user|
62
- user.payments.select do |payment|
63
- payment.created_at >= from && payment.created_at <= to
64
- end.sum(&:amount)
65
- }
66
- # Has no N+1 and calculation is the most efficient
67
- p User.all.includes(:payments_total).map { |user| user.payments_total(from: from, to: to) }