valkyrie-sequel 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a74b8f8a7ba365deb8fcda493a2289a579580018ba4d9251684a9bcc02d3665
4
- data.tar.gz: 70774d437ef4fdc8e75566ca4c1942bbc66f247eee19b49a3017cea3097b66af
3
+ metadata.gz: 89a976b76238995b845b6b1d5d82cbf3a24afa1e92c795590f8045fa19a9295c
4
+ data.tar.gz: 63c6b8e510197296ccc2b83428b00d40ab1f974b18f6db05c7dc28c62a955ec9
5
5
  SHA512:
6
- metadata.gz: dad38881c3959f86f9febc7fe1a1ea5e62fea9310af9715c27da3087cfe4af6ebfed20ab9e48de6b99e775901f818aeca5beb2e3fd215b3a7adf81cb87ceff1e
7
- data.tar.gz: 160c5f59b084e3594995d492773d16734daaaa98141b9340f4859914a7710fa3426d3f5b38e97efd56b19f527ac618210280e96de2f8521b6afca55de352d05d
6
+ metadata.gz: 71293e3bc4635ba2df250558612088e8dfcb2a7a77cc6923c27c1da7567b09e3c112d462ab0063985084f5a89c32a33ec4fd9aea2ff0120df74ce1583aad67c3
7
+ data.tar.gz: 9ec49446c6f760ddba814e51154cde6e842d86fc76fbc81bf8b484f3478a2a23e1a27befca96239d41e0a14da561174b4e032902e8809143d861c7c00b4aa121
@@ -29,6 +29,13 @@ module Valkyrie::Sequel
29
29
  end
30
30
  end
31
31
 
32
+ # Count all records for a specific resource type
33
+ # @param [Class] model
34
+ # @return integer
35
+ def count_all_of_model(model:)
36
+ resources.where(internal_resource: model.to_s).count
37
+ end
38
+
32
39
  def find_many_by_ids(ids:)
33
40
  ids = ids.map do |id|
34
41
  id = Valkyrie::ID.new(id.to_s) if id.is_a?(String)
@@ -44,22 +51,26 @@ module Valkyrie::Sequel
44
51
  end
45
52
  end
46
53
 
47
- def find_references_by(resource:, property:)
54
+ def find_references_by(resource:, property:, model: nil)
48
55
  return [] if resource.id.blank? || resource[property].blank?
49
56
  # only return ordered if needed to avoid performance penalties
50
57
  if ordered_property?(resource: resource, property: property)
51
- run_query(find_ordered_references_query, property.to_s, resource.id.to_s)
58
+ find_ordered_references_by(resource: resource, property: property, model: model)
52
59
  else
53
- run_query(find_references_query, property.to_s, resource.id.to_s)
60
+ find_unordered_references_by(resource: resource, property: property, model: model)
54
61
  end
55
62
  end
56
63
 
57
- def find_inverse_references_by(resource: nil, id: nil, property:)
64
+ def find_inverse_references_by(resource: nil, id: nil, model: nil, property:)
58
65
  raise ArgumentError, "Provide resource or id" unless resource || id
59
66
  ensure_persisted(resource) if resource
60
67
  id ||= resource.id
61
68
  internal_array = { property => [id: id.to_s] }
62
- run_query(find_inverse_references_query, internal_array.to_json)
69
+ if model
70
+ run_query(find_inverse_references_with_model_query, internal_array.to_json, model.to_s)
71
+ else
72
+ run_query(find_inverse_references_query, internal_array.to_json)
73
+ end
63
74
  end
64
75
 
65
76
  # Find and a record using a Valkyrie ID for an alternate ID, and construct
@@ -150,6 +161,14 @@ module Valkyrie::Sequel
150
161
  SQL
151
162
  end
152
163
 
164
+ def find_inverse_references_with_model_query
165
+ <<-SQL
166
+ SELECT * FROM orm_resources WHERE
167
+ metadata @> ?
168
+ AND internal_resource = ?
169
+ SQL
170
+ end
171
+
153
172
  # Generate the SQL query for retrieving member resources in PostgreSQL using a
154
173
  # JSON object literal and resource ID as arguments.
155
174
  # @see https://guides.rubyonrails.org/active_record_querying.html#array-conditions
@@ -167,6 +186,15 @@ module Valkyrie::Sequel
167
186
  SQL
168
187
  end
169
188
 
189
+ def find_references_with_type_query
190
+ <<-SQL
191
+ SELECT DISTINCT member.* FROM orm_resources a,
192
+ jsonb_array_elements(a.metadata->?) AS b(member)
193
+ JOIN orm_resources member ON (b.member->>'id')::#{id_type} = member.id WHERE a.id = ?
194
+ AND member.internal_resource = ?
195
+ SQL
196
+ end
197
+
170
198
  def find_ordered_references_query
171
199
  <<-SQL
172
200
  SELECT member.* FROM orm_resources a,
@@ -176,6 +204,16 @@ module Valkyrie::Sequel
176
204
  SQL
177
205
  end
178
206
 
207
+ def find_ordered_references_with_type_query
208
+ <<-SQL
209
+ SELECT member.* FROM orm_resources a,
210
+ jsonb_array_elements(a.metadata->?) WITH ORDINALITY AS b(member, member_pos)
211
+ JOIN orm_resources member ON (b.member->>'id')::#{id_type} = member.id WHERE a.id = ?
212
+ AND member.internal_resource = ?
213
+ ORDER BY b.member_pos
214
+ SQL
215
+ end
216
+
179
217
  # Accesses the data type in PostgreSQL used for the primary key
180
218
  # (For example, a UUID)
181
219
  # @see https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaCache.html#method-i-columns_hash
@@ -201,5 +239,21 @@ module Valkyrie::Sequel
201
239
  def ordered_property?(resource:, property:)
202
240
  resource.ordered_attribute?(property)
203
241
  end
242
+
243
+ def find_ordered_references_by(resource:, property:, model: nil)
244
+ if model
245
+ run_query(find_ordered_references_with_type_query, property.to_s, resource.id.to_s, model.to_s)
246
+ else
247
+ run_query(find_ordered_references_query, property.to_s, resource.id.to_s)
248
+ end
249
+ end
250
+
251
+ def find_unordered_references_by(resource:, property:, model: nil)
252
+ if model
253
+ run_query(find_references_with_type_query, property.to_s, resource.id.to_s, model.to_s)
254
+ else
255
+ run_query(find_references_query, property.to_s, resource.id.to_s)
256
+ end
257
+ end
204
258
  end
205
259
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module Valkyrie
3
3
  module Sequel
4
- VERSION = "2.0.1"
4
+ VERSION = "2.1.0"
5
5
  end
6
6
  end
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_dependency "sequel"
24
24
  spec.add_dependency "sequel_pg"
25
- spec.add_dependency "valkyrie", '~> 2.0.0'
25
+ spec.add_dependency "valkyrie", '~> 2.1.0'
26
26
  spec.add_dependency "oj"
27
27
  spec.add_development_dependency "bundler", "~> 2.0"
28
28
  spec.add_development_dependency "rake"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valkyrie-sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trey Pendragon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-04 00:00:00.000000000 Z
11
+ date: 2020-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 2.0.0
47
+ version: 2.1.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 2.0.0
54
+ version: 2.1.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: oj
57
57
  requirement: !ruby/object:Gem::Requirement