Skip to content

Queries

Justin Coyne edited this page Oct 11, 2017 · 10 revisions

Queries in Valkyrie are being added as they're needed for the logic of a digital repository, to better enable a wide array of inter-operable backend solutions. Currently, there are only seven that are supported by all adapters (shared spec):

Find All

Use Case: Re-indexing.

metadata_adapter.query_service.find_all # => [#<Book:0x007fe383479988>]

Find all of model

Use Case: Populate a collection drop-down with all available collections.

collection = metadata_adapter.persister.save(resource: Collection.new)
metadata_adapter.query_service.find_all_of_model(model: Collection) # => [collection]

Find by ID

Use Case: Finding an object for an item record page.

metadata_adapter.query_service.find_by(id: Valkyrie::ID.new("692ccfd5-414a-42f9-a8d5-c1605e7baef9"))
  # => #<Book:0x007fe383479988
         @id=#<Valkyrie::ID:0x007fe38353ac78 @id="692ccfd5-414a-42f9-a8d5-c1605e7baef9">,
         @title=[],
         @member_ids=[]>

Find all Members

Use Case: List all members on a record page.

child_book = metadata_adapter.persister.save(resource: Book.new)
parent_book = metadata_adapter.persister.save(resource: Book.new(member_ids: child_book.id))
metadata_adapter.query_service.find_members(resource: parent_book) # => [child_book]

Find all Parents

Use Case: Clean-up parent associations when deleting a child.

child_book = metadata_adapter.persister.save(resource: Book.new)
parent_book = metadata_adapter.persister.save(resource: Book.new(member_ids: child_book.id))
metadata_adapter.query_service.find_parents(resource: child_book) # => [parent_book]

Find References By

Use Case: Find associated objects by a property, but not in order.

referenced_book = metadata_adapter.persister.save(resource: Book.new)
book = metadata_adapter.persister.save(resource: Book.new(referenced_book_id:
referenced_book.id))
metadata_adapter.query_service.find_references_by(resource: book, property: :referenced_book_id) # => [referenced_book]

Find Inverse References By

Use Case: Find everything that references me, so I can clean them up when being deleted.

referenced_book = metadata_adapter.persister.save(resource: Book.new)
book = metadata_adapter.persister.save(resource: Book.new(referenced_book_id:
referenced_book.id))
metadata_adapter.query_service.find_inverse_references_by(resource: referenced_book, property: :referenced_book_id) # => [book]