Skip to content

Retrieving resources

E. Lynette Rayle edited this page Dec 18, 2019 · 11 revisions

Reference: Valkyrie Query Documentation | Valkyrie Custom Query Documentation | postgres query_service | shared specs

Terminology

  • query service
    • provides methods for finding resources
    • see postgres query service (under reference above) for an example implementation of a query service
    • see shared specs (under reference above) for tests demonstrating expected function of query service methods
  • custom query

All the examples in this section assume that you completed the examples under [Saving a resource]. If you went beyond the examples, you may see additional results beyond those shown for each example.

finding one by id

To find the book by it's id, you can either pass the string representation of the ID or the instance of Valkyrie::ID.

Substitute the actual id assigned during [Saving a resource] for AN_ID_ASSIGNED_BY_POSTGRES.

book = ValkyriePgDemo.pg_query_service.find_by(id: '_AN_ID_ASSIGNED_BY_POSTGRES_')
book.id                        # #<Valkyrie::ID:... @id="_AN_ID_ASSIGNED_BY_POSTGRES_">

same_book = ValkyriePgDemo.pg_query_service.find_by(id: book.id)
book == same_book              # true

book.persisted?                # true
book.title                     # ['Free Fall']
book.title = "Lullaby Town"
book.title                     # ["Lullaby Town"]
book.persisted?                # true - persisted? indicates only that the resource is known to exist in the database

book == same_book              # false -- since the title was changed in book, but not same_book

NOTE: Since book with the new title wasn't persisted, the database continues to have the original title ['Free Fall'].

finding one by alternate id

alternate_ids is a special attribute name which the query service knows about. There are methods defined to retrieve resources by an alternate id.

The book resource was saved with alternate_ids [#<Valkyrie::ID:0x00007fbf9c08e770 @id="b1">].

book = ValkyriePgDemo.pg_query_service.find_by_alternate_identifier(alternate_identifier: 'b1')
book.id                        # #<Valkyrie::ID:... @id="_AN_ID_ASSIGNED_BY_POSTGRES_">
book.title                     # ["Free Fall"]

finding all by model

To find all books, get all resources for a model.

books = ValkyriePgDemo.pg_query_service.find_all_of_model(model: Book)
books.each { |b| puts("class: #{b.class}    title: #{b.title}") }        
# class: Book    title: ["Free Fall"]

finding based on member_ids relationship

member_ids is a special attribute name which the query service knows about. There are methods defined to retrieve members and their parents.

child_pages = ValkyriePgDemo.pg_query_service.find_members(resource: book)
child_pages.each { |p| puts("class: #{p.class}    page_num: #{p.page_num}    structure: #{p.structure}") }
# class: Page    page_num: 1    structure: title page

parent_books = ValkyriePgDemo.pg_query_service.find_parents(resource: page)
parent_books.each { |b| puts("class: #{b.class}    title: #{b.title}") }        
# class: Book    title: ["Free Fall"]

finding based on other relationships

There are methods to find other relationships between resources that maintained in an attribute other than member_ids. See #find_references_by and #find_inverse_references in the shared specs for details on how they work.

List of all standard query methods

You can see the list of all queries that each data source adapter should support in shared_specs/queries

Custom Queries

Reference: Custom queries in Valkyrie wiki documentation

You may want to add additional queries beyond the standard set. Some reasons why you may want to implement custom queries...

  • complex query that can be completed with the standard set and is used in multiple places in the app
  • simple or complex queries that cannot be completed with the standard set. NOTE: In this case, you will need to implement these queries for each adapter your app supports.

Previous | Next