-
Notifications
You must be signed in to change notification settings - Fork 23
Persistence
Requirements for a model can be seen in https://github.com/samvera-labs/valkyrie/blob/main/lib/valkyrie/specs/shared_specs/resource.rb.
A typical model declaration looks like this:
# frozen_string_literal: true
class Book < Valkyrie::Resource
attribute :id, Valkyrie::Types::ID.optional
attribute :title, Valkyrie::Types::Set.optional # default type if none is specified
attribute :member_ids, Valkyrie::Types::Array
end
where Valkyrie::Types::Set is a Valkyrie type implemented via Dry-Types
There are a variety of metadata adapters available, and the most common way of
using them is to instantiate them and register them with Valkyrie::MetadataAdapter
via a short-name, for access later.
# in config/initializers/valkyrie.rb
Valkyrie::MetadataAdapter.register(
Valkyrie::Persistence::Memory::MetadataAdapter.new,
:memory
)
When registered, an adapter instance can be recalled via Valkyrie::MetadataAdapter.find
[1] pry(main)> Valkyrie::MetadataAdapter.find(:memory)
=> #<Valkyrie::Persistence::Memory::MetadataAdapter:0x007fa6ec031bd8>
Metadata Adapter instances must respond to two methods, #persister
and #query_service
Persistence in Valkyrie is done by passing an object to a persistence object, which has a defined interface (enforced by the shared spec.)
metadata_adapter = Valkyrie::MetadataAdapter.find(:memory)
book = Book.new # => #<Book:0x007fe383479988 @id=nil, @title=[], @member_ids=[]>
book = metadata_adapter.persister.save(resource: book)
book.id # => #<Valkyrie::ID:0x007fe38353ac78 @id="692ccfd5-414a-42f9-a8d5-c1605e7baef9">
adapter1 = Valkyrie::MetadataAdapter.find(:postgres)
adapter2 = Valkyrie::MetadataAdapter.find(:fedora)
composite_persister = Valkyrie::Persistence::CompositePersister.new(adapter1.persister, adapter2.persister)
book = Book.new
composite_persister.save(resource: book)
For more on saving to two backends, see https://github.com/samvera-labs/valkyrie/wiki/Combining-Persistence-&-Indexing
metadata_adapter.persister.delete(resource: book)
metadata_adapter.persister.wipe!
Continue on to Queries