diff --git a/lib/sequent/core/aggregate_root.rb b/lib/sequent/core/aggregate_root.rb index 67f031a2..e6c1de10 100644 --- a/lib/sequent/core/aggregate_root.rb +++ b/lib/sequent/core/aggregate_root.rb @@ -98,6 +98,26 @@ def to_s "#{self.class.name}: #{@id}" end + # Some aggregates represent a unique external entity (e.g. a + # user's email address or login name) and this uniqueness needs + # to be enforced. For each unique key the returned object should + # have an entry where the key of the entry describes the scope + # of the constraint (e.g. `user_email` or `login_name`) and the + # value represents the unique value. Values can be any JSON + # value (string, object, array, etc). Note that uniqueness is + # enforced across all aggregate types if the same scope is used. + # + # An `AggregateKeyNotUniqueError` is raised if a unique + # constrained is violated when committing the events to the + # database. + # + # Sample return value: + # + # ``` + # { + # user_email: { email: 'bob@example.com' } + # } + # ``` def unique_keys {} end diff --git a/lib/sequent/core/event_store.rb b/lib/sequent/core/event_store.rb index 4af13063..929b7a15 100644 --- a/lib/sequent/core/event_store.rb +++ b/lib/sequent/core/event_store.rb @@ -9,6 +9,9 @@ module Sequent module Core + class AggregateKeyNotUniqueError < RuntimeError + end + class EventStore include Helpers::PgsqlHelpers include SnapshotStore @@ -18,9 +21,6 @@ class EventStore class OptimisticLockingError < RuntimeError end - class AggregateKeyNotUniqueError < RuntimeError - end - class DeserializeEventError < RuntimeError attr_reader :event_hash diff --git a/spec/lib/sequent/core/aggregate_repository_spec.rb b/spec/lib/sequent/core/aggregate_repository_spec.rb index 6439960f..6e6ae85d 100644 --- a/spec/lib/sequent/core/aggregate_repository_spec.rb +++ b/spec/lib/sequent/core/aggregate_repository_spec.rb @@ -509,7 +509,7 @@ def unique_keys Sequent.aggregate_repository.add_aggregate(dummy2) expect { Sequent.aggregate_repository.commit(DummyCommand.new) } - .to raise_error Sequent::Core::EventStore::AggregateKeyNotUniqueError + .to raise_error Sequent::Core::AggregateKeyNotUniqueError end end end