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



129
130
131
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 129

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_by_alternate_identifier(alternate_identifier:) ⇒ Valkyrie::Resource

Returns The object being searched for.

Parameters:

  • alternate_identifier (Valkyrie::ID)

    The alternate identifier to query for.

Returns:

Raises:

  • (Valkyrie::Persistence::ObjectNotFoundError)

    Raised when the alternate identifier isn’t in the persistence backend.

  • (ArgumentError)

    Raised when alternate identifier is not a String or a Valkyrie::ID



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

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>

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.



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

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



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

def find_inverse_references_query
  "    SELECT * FROM orm_resources WHERE\n    metadata @> ?\n  SQL\nend\n"

#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



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 49

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>

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.



62
63
64
65
66
67
68
69
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 62

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



95
96
97
98
99
100
101
102
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 95

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

#find_members_with_type_queryObject



104
105
106
107
108
109
110
111
112
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 104

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

#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.



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

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.



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

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



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

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

#run_query(query, *args) ⇒ Object



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

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