occams-record 1.1.0.beta2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -28
- data/lib/occams-record/eager_loaders/context.rb +1 -1
- data/lib/occams-record/merge.rb +4 -4
- data/lib/occams-record/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a403231d294ef71185626a819905f486206a5fdf6b94e2d0cd7eb459393a5be8
|
4
|
+
data.tar.gz: 0a19d6226484a10da785bcc2dff3d4f8a068b376b8b9443f0f2a1c6eebefe50d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '05977e1eb8936770d167e200db0925d0b6e93422b72b88925e71ef0d50df703f5e1171b175630f63ac6f4b02696ef9d5320f6b770d7414aea2c2458d1e0038f6'
|
7
|
+
data.tar.gz: eace78527961d84999ad34e99be7a71be64bcbc64377f9cf907c9cc35f6d25f110586e5991f0e315246bc5b5fb3bd4b59a2550f5b58965aab72fb2ec1e3905ca
|
data/README.md
CHANGED
@@ -2,13 +2,11 @@
|
|
2
2
|
|
3
3
|
> Do not multiply entities beyond necessity. -- Occam's Razor
|
4
4
|
|
5
|
-
**Breaking change since pre-1.0** See [HISTORY.md](https://github.com/jhollinger/occams-record/blob/master/HISTORY.md#100rc1-2018-12-01).
|
6
|
-
|
7
5
|
Occam's Record is a high-efficiency, advanced query library for ActiveRecord apps. It is **not** an ORM or an ActiveRecord replacement. Use it to solve pain points in your existing ActiveRecord app. Occams Record gives you two things:
|
8
6
|
|
9
7
|
**Performance**
|
10
8
|
|
11
|
-
* 3x-5x faster than ActiveRecord queries
|
9
|
+
* 3x-5x faster than ActiveRecord queries, *minimum*.
|
12
10
|
* Uses 1/3 the memory of ActiveRecord query results.
|
13
11
|
* Eliminates the N+1 query problem.
|
14
12
|
|
@@ -61,31 +59,29 @@ Occams Record has great support for raw SQL queries too, but we'll get to those
|
|
61
59
|
|
62
60
|
## Basic eager loading
|
63
61
|
|
64
|
-
Eager loading is similiar to ActiveRecord's `preload
|
62
|
+
Eager loading is similiar to ActiveRecord's `preload`: each association is loaded in a separate query. Unlike ActiveRecord, nested associations use blocks instead of Hashes. More importantly, if you try to use an association you didn't eager load *an exception will be raised*. In other words, the N+1 query problem simply doesn't exist.
|
65
63
|
|
66
64
|
```ruby
|
67
|
-
|
65
|
+
OccamsRecord.
|
68
66
|
query(q).
|
69
67
|
eager_load(:customer).
|
70
68
|
eager_load(:line_items) {
|
71
69
|
eager_load(:product)
|
72
70
|
eager_load(:something_else)
|
73
71
|
}.
|
74
|
-
|
75
|
-
|
76
|
-
order
|
77
|
-
puts
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
OccamsRecord::MissingEagerLoadError: Association 'category' is unavailable on Product because it was not eager loaded!
|
83
|
-
}
|
72
|
+
find_each { |order|
|
73
|
+
puts order.customer.name
|
74
|
+
order.line_items.each { |line_item|
|
75
|
+
puts line_item.product.name
|
76
|
+
puts line_item.product.category.name
|
77
|
+
OccamsRecord::MissingEagerLoadError: Association 'category' is unavailable on Product because it was not eager loaded!
|
78
|
+
}
|
79
|
+
}
|
84
80
|
```
|
85
81
|
|
86
82
|
## Advanced eager loading
|
87
83
|
|
88
|
-
Occams Record allows you to
|
84
|
+
Occams Record allows you to tweak the SQL of any eager load. Pull back only the columns you need, change the order, add a `WHERE` clause, etc.
|
89
85
|
|
90
86
|
```ruby
|
91
87
|
orders = OccamsRecord.
|
@@ -93,8 +89,7 @@ orders = OccamsRecord.
|
|
93
89
|
# Only SELECT the columns you need. Your DBA will thank you.
|
94
90
|
eager_load(:customer, select: "id, name").
|
95
91
|
|
96
|
-
# A Proc can
|
97
|
-
# builders and any scopes you've defined on the LineItem model.
|
92
|
+
# A Proc can use ActiveRecord's query builder
|
98
93
|
eager_load(:line_items, ->(q) { q.active.order("created_at") }) {
|
99
94
|
eager_load(:product)
|
100
95
|
eager_load(:something_else)
|
@@ -130,7 +125,7 @@ OccamsRecord.
|
|
130
125
|
|
131
126
|
**Eager loading**
|
132
127
|
|
133
|
-
To use `eager_load` with a raw SQL query you must tell Occams what the base model is. (That doesn't apply if you're loading an ad hoc, raw SQL association. We'll get to those
|
128
|
+
To use `eager_load` with a raw SQL query you must tell Occams what the base model is. (That doesn't apply if you're loading an ad hoc, raw SQL association. We'll get to those next.)
|
134
129
|
|
135
130
|
```ruby
|
136
131
|
orders = OccamsRecord.
|
@@ -181,13 +176,11 @@ products = OccamsRecord.
|
|
181
176
|
run
|
182
177
|
```
|
183
178
|
|
184
|
-
`eager_load_many`
|
185
|
-
|
186
|
-
The SQL string and binds should be familiar by now. `%{ids}` will be provided for you - just stick it in the right place. Note that it won't always be called *ids*; the name will be the plural version of the key in your mapping.
|
179
|
+
`eager_load_many` is declaring an ad hoc *has_many* association called *customers*. The `{:id => :product_id}` Hash defines the mapping: *id* in the parent record maps to *product_id* in the child records.
|
187
180
|
|
188
|
-
|
181
|
+
The SQL string and binds should be familiar. `%{ids}` will be provided for you - just stick it in the right place. Note that it won't always be called *ids*; the name will be the plural version of the key in your mapping.
|
189
182
|
|
190
|
-
|
183
|
+
`eager_load_one` defines an ad hoc `has_one`/`belongs_to` association. It and `eager_load_many` are available with both `OccamsRecord.query` and `OccamsRecord.sql`.
|
191
184
|
|
192
185
|
## Injecting instance methods
|
193
186
|
|
@@ -214,10 +207,6 @@ orders = OccamsRecord.
|
|
214
207
|
run
|
215
208
|
```
|
216
209
|
|
217
|
-
## Ugly Module
|
218
|
-
|
219
|
-
`OccamsRecord::Ugly` contains helpers for things that shouldn't, but sometimes must, be done in legacy codebases. See the docs.
|
220
|
-
|
221
210
|
---
|
222
211
|
|
223
212
|
# Unsupported features
|
data/lib/occams-record/merge.rb
CHANGED
@@ -51,7 +51,7 @@ module OccamsRecord
|
|
51
51
|
rescue NoMethodError => e
|
52
52
|
raise MissingColumnError.new(row, e.name)
|
53
53
|
end
|
54
|
-
row.send
|
54
|
+
row.send(@assign, attr ? assoc_rows_by_ids[attr] : nil)
|
55
55
|
end
|
56
56
|
|
57
57
|
# Slower but works with any number of mapping key pairs
|
@@ -72,7 +72,7 @@ module OccamsRecord
|
|
72
72
|
rescue NoMethodError => e
|
73
73
|
raise MissingColumnError.new(row, e.name)
|
74
74
|
end
|
75
|
-
row.send
|
75
|
+
row.send(@assign, attrs.any? ? assoc_rows_by_ids[attrs] : nil)
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
@@ -108,7 +108,7 @@ module OccamsRecord
|
|
108
108
|
rescue NoMethodError => e
|
109
109
|
raise MissingColumnError.new(row, e.name)
|
110
110
|
end
|
111
|
-
row.send
|
111
|
+
row.send(@assign, assoc_rows_by_attr[pkey] || [])
|
112
112
|
end
|
113
113
|
|
114
114
|
# Slower but works with any number of mapping key pairs
|
@@ -127,7 +127,7 @@ module OccamsRecord
|
|
127
127
|
rescue NoMethodError => e
|
128
128
|
raise MissingColumnError.new(row, e.name)
|
129
129
|
end
|
130
|
-
row.send
|
130
|
+
row.send(@assign, assoc_rows_by_attrs[pkeys] || [])
|
131
131
|
end
|
132
132
|
end
|
133
133
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: occams-record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jordan Hollinger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -77,12 +77,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
77
|
version: 2.3.0
|
78
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
82
|
+
version: '0'
|
83
83
|
requirements: []
|
84
84
|
rubyforge_project:
|
85
|
-
rubygems_version: 2.7.6
|
85
|
+
rubygems_version: 2.7.6.2
|
86
86
|
signing_key:
|
87
87
|
specification_version: 4
|
88
88
|
summary: The missing high-efficiency query API for ActiveRecord
|