Class: Valkyrie::Persistence::Postgres::QueryService

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/persistence/postgres/query_service.rb

Overview

Query Service for the Postgres Metadata Adapter

Most queries are delegated through to the ActiveRecord model ORM::Resource

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_factory:) ⇒ QueryService



14
15
16
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 14

def initialize(resource_factory:)
  @resource_factory = resource_factory
end

Instance Attribute Details

#resource_factoryObject (readonly)

Returns the value of attribute resource_factory.



10
11
12
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 10

def resource_factory
  @resource_factory
end

Instance Method Details

#custom_queriesValkyrie::Persistence::CustomQueryContainer

Constructs a Valkyrie::Persistence::CustomQueryContainer using this query service



208
209
210
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 208

def custom_queries
  @custom_queries ||= ::Valkyrie::Persistence::CustomQueryContainer.new(query_service: self)
end

#find_allArray<Valkyrie::Resource>

Retrieve all records for the resource and construct Valkyrie Resources

for each record


21
22
23
24
25
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 21

def find_all
  orm_class.all.lazy.map do |orm_object|
    resource_factory.to_resource(object: orm_object)
  end
end

#find_all_of_model(model:) ⇒ Array<Valkyrie::Resource>

Retrieve all records for a specific resource type and construct Valkyrie

Resources for each record


31
32
33
34
35
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 31

def find_all_of_model(model:)
  orm_class.where(internal_resource: model.to_s).lazy.map do |orm_object|
    resource_factory.to_resource(object: orm_object)
  end
end

#find_by(id:) ⇒ Valkyrie::Resource

Find a record using a Valkyrie ID, and map it to a Valkyrie Resource



41
42
43
44
45
46
47
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 41

def find_by(id:)
  id = Valkyrie::ID.new(id.to_s) if id.is_a?(String)
  validate_id(id)
  resource_factory.to_resource(object: orm_class.find(id.to_s))
rescue ActiveRecord::RecordNotFound
  raise Valkyrie::Persistence::ObjectNotFoundError
end

#find_by_alternate_identifier(alternate_identifier:) ⇒ Valkyrie::Resource

Find and a record using a Valkyrie ID for an alternate ID, and construct

a Valkyrie Resource


53
54
55
56
57
58
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 53

def find_by_alternate_identifier(alternate_identifier:)
  alternate_identifier = Valkyrie::ID.new(alternate_identifier.to_s) if alternate_identifier.is_a?(String)
  validate_id(alternate_identifier)
  internal_array = "{\"alternate_ids\": [{\"id\": \"#{alternate_identifier}\"}]}"
  run_query(find_inverse_references_query, internal_array).first || raise(Valkyrie::Persistence::ObjectNotFoundError)
end

#find_inverse_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>

Find all resources referencing a given Valkyrie Resource by a property



114
115
116
117
118
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 114

def find_inverse_references_by(resource:, property:)
  ensure_persisted(resource)
  internal_array = "{\"#{property}\": [{\"id\": \"#{resource.id}\"}]}"
  run_query(find_inverse_references_query, internal_array)
end

#find_inverse_references_queryString

Generate the SQL query for retrieving member resources in PostgreSQL using a

JSON object literal as an argument (e. g. { "alternate_ids": [{"id": "d6e88f80-41b3-4dbf-a2a0-cd79e20f6d10"}] }).

This uses JSON functions in order to retrieve JSON property values



173
174
175
176
177
178
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 173

def find_inverse_references_query
  <<-SQL
    SELECT * FROM orm_resources WHERE
    metadata @> ?
  SQL
end

#find_many_by_ids(ids:) ⇒ Array<Valkyrie::Resource>

Find records using a set of Valkyrie IDs, and map each to Valkyrie

Resources


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 64

def find_many_by_ids(ids:)
  ids.map! do |id|
    id = Valkyrie::ID.new(id.to_s) if id.is_a?(String)
    validate_id(id)
    id.to_s
  end

  orm_class.where(id: ids).map do |orm_resource|
    resource_factory.to_resource(object: orm_resource)
  end
end

#find_members(resource:, model: nil) ⇒ Array<Valkyrie::Resource>

Find all member resources for a given Valkyrie Resource



80
81
82
83
84
85
86
87
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 80

def find_members(resource:, model: nil)
  return [] if resource.id.blank?
  if model
    run_query(find_members_with_type_query, resource.id.to_s, model.to_s)
  else
    run_query(find_members_query, resource.id.to_s)
  end
end

#find_members_queryString

Note:

this uses a CROSS JOIN for all combinations of member IDs with the IDs of their parents

Generate the SQL query for retrieving member resources in PostgreSQL using a

resource ID as an argument.

This also uses JSON functions in order to retrieve JSON property values



139
140
141
142
143
144
145
146
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 139

def find_members_query
  <<-SQL
    SELECT member.* FROM orm_resources a,
    jsonb_array_elements(a.metadata->'member_ids') WITH ORDINALITY AS b(member, member_pos)
    JOIN orm_resources member ON (b.member->>'id')::#{id_type} = member.id WHERE a.id = ?
    ORDER BY b.member_pos
  SQL
end

#find_members_with_type_queryString

Note:

this uses a CROSS JOIN for all combinations of member IDs with the IDs of their parents

Generate the SQL query for retrieving member resources in PostgreSQL using a

resource ID and resource type as arguments.

This also uses JSON functions in order to retrieve JSON property values



157
158
159
160
161
162
163
164
165
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 157

def find_members_with_type_query
  <<-SQL
    SELECT member.* FROM orm_resources a,
    jsonb_array_elements(a.metadata->'member_ids') WITH ORDINALITY AS b(member, member_pos)
    JOIN orm_resources member ON (b.member->>'id')::#{id_type} = member.id WHERE a.id = ?
    AND member.internal_resource = ?
    ORDER BY b.member_pos
  SQL
end

#find_ordered_references_queryObject



197
198
199
200
201
202
203
204
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 197

def find_ordered_references_query
  <<-SQL
    SELECT member.* FROM orm_resources a,
    jsonb_array_elements(a.metadata->?) WITH ORDINALITY AS b(member, member_pos)
    JOIN orm_resources member ON (b.member->>'id')::#{id_type} = member.id WHERE a.id = ?
    ORDER BY b.member_pos
  SQL
end

#find_parents(resource:) ⇒ Array<Valkyrie::Resource>

Find all parent resources for a given Valkyrie Resource



92
93
94
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 92

def find_parents(resource:)
  find_inverse_references_by(resource: resource, property: :member_ids)
end

#find_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>

Find all resources related to a given Valkyrie Resource by a property



100
101
102
103
104
105
106
107
108
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 100

def find_references_by(resource:, property:)
  return [] if resource.id.blank? || resource[property].blank?
  # only return ordered if needed to avoid performance penalties
  if ordered_property?(resource: resource, property: property)
    run_query(find_ordered_references_query, property, resource.id.to_s)
  else
    run_query(find_references_query, property, resource.id.to_s)
  end
end

#find_references_queryString

Note:

this uses a CROSS JOIN for all combinations of member IDs with the IDs of their parents

Generate the SQL query for retrieving member resources in PostgreSQL using a

JSON object literal and resource ID as arguments.

This also uses JSON functions in order to retrieve JSON property values



189
190
191
192
193
194
195
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 189

def find_references_query
  <<-SQL
    SELECT member.* FROM orm_resources a,
    jsonb_array_elements(a.metadata->?) AS b(member)
    JOIN orm_resources member ON (b.member->>'id')::#{id_type} = member.id WHERE a.id = ?
  SQL
end

#run_query(query, *args) ⇒ Array<Valkyrie::Resource>

Execute a query in SQL for resource records and map them to Valkyrie

Resources


124
125
126
127
128
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 124

def run_query(query, *args)
  orm_class.find_by_sql(([query] + args)).lazy.map do |object|
    resource_factory.to_resource(object: object)
  end
end