module Sequent::Core::Helpers::UniqueKeys
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.
Public Class Methods
Source
# File lib/sequent/core/helpers/unique_keys.rb, line 75 def self.included(host_class) host_class.extend(ClassMethods) end
Public Instance Methods
Source
# File lib/sequent/core/helpers/unique_keys.rb, line 65 def unique_keys return {} if self.class.unique_key_definitions.nil? self.class.unique_key_definitions &.transform_values do |attributes| attributes.transform_values { |block| instance_exec(&block) }.compact end &.delete_if { |_, value| value.empty? } end
Returns the unique keys for the current instance based on the ‘unique_key` defintions. You can also override it if you need more compicated logic.
Example return value:
“‘ {
user_email: { email: 'bob@example.com' }
} “‘