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(adapter:) ⇒ QueryService

Returns a new instance of QueryService.



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

def initialize(adapter:)
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

Instance Method Details

#custom_queriesObject



123
124
125
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 123

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

#find_allArray<Valkyrie::Resource>

Returns All objects in the persistence backend.

Returns:



18
19
20
21
22
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 18

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>

Returns All objects in the persistence backend with the given class.

Parameters:

  • model (Class)

    Class to query for.

Returns:

  • (Array<Valkyrie::Resource>)

    All objects in the persistence backend with the given class.



25
26
27
28
29
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 25

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

Returns The object being searched for.

Parameters:

Returns:

Raises:



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

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_inverse_references_by(resource:, property:) ⇒ Array<Valkyrie::Resource>

Returns All resources in the persistence backend which have the ID of the given ‘resource` in their `property` property. Not in order.

Parameters:

  • resource (Valkyrie::Resource)

    The resource which is being referenced by other resources.

  • property (Symbol)

    The property which, on other resources, is referencing the given ‘resource`

Returns:

  • (Array<Valkyrie::Resource>)

    All resources in the persistence backend which have the ID of the given ‘resource` in their `property` property. Not in order.

Raises:

  • (ArgumentError)

    Raised when the ID is not in the persistence backend.



77
78
79
80
81
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 77

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_queryObject



108
109
110
111
112
113
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 108

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

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

Returns All requested objects that were found.

Parameters:

  • ids (Array<Valkyrie::ID, String>)

    The IDs to query for.

Returns:

Raises:

  • (ArgumentError)

    Raised when any ID is not a String or a Valkyrie::ID



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 41

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
rescue ActiveRecord::RecordNotFound
  raise Valkyrie::Persistence::ObjectNotFoundError
end

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

Returns child objects of type ‘model` referenced by `resource`’s ‘member_ids` method. Returned in order.

Parameters:

  • resource (Valkyrie::Resource)

    Model whose members are being searched for.

  • model (Class) (defaults to: nil)

    Class to query for. (optional)

Returns:

  • (Array<Valkyrie::Resource>)

    child objects of type ‘model` referenced by `resource`’s ‘member_ids` method. Returned in order.



56
57
58
59
60
61
62
63
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 56

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_queryObject



89
90
91
92
93
94
95
96
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 89

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_queryObject



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

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_parents(resource:) ⇒ Array<Valkyrie::Resource>

Returns All resources which are parents of the given ‘resource`. This means the resource’s ‘id` appears in their `member_ids` array.

Parameters:

Returns:

  • (Array<Valkyrie::Resource>)

    All resources which are parents of the given ‘resource`. This means the resource’s ‘id` appears in their `member_ids` array.



66
67
68
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 66

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

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

Returns All objects which are referenced by the ‘property` property on `resource`. Not necessarily in order.

Parameters:

  • resource (Valkyrie::Resource)

    Model whose property is being searched.

  • property (Symbol)

    Property which, on the ‘resource`, contains IDs which are to be de-referenced.

Returns:

  • (Array<Valkyrie::Resource>)

    All objects which are referenced by the ‘property` property on `resource`. Not necessarily in order.



71
72
73
74
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 71

def find_references_by(resource:, property:)
  return [] if resource.id.blank? || resource[property].blank?
  run_query(find_references_query, property, resource.id.to_s)
end

#find_references_queryObject



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

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) ⇒ Object



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

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