Skip to content

Cratis Chronicle unique property constraints

A Cratis Chronicle unique property constraint prevents different event sources from claiming the same value, such as an email address or handle. The source that owns a value can append it again without a violation, so use this rule to protect ownership of a value rather than to prevent repeated events.

In .NET, apply [Unique] to the event property. The client discovers the declaration on registered event types and sends its constraint definition to the Kernel when it starts.

The declaration below reserves the email address for the event source that appends the registration event. Another source cannot claim that address while it is held:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
[EventType]
public record ConstraintsModelBoundUniqueProjectCreated([property: Unique] string Name, string Description);

View C# snippet source on GitHub

A unique property value belongs to an event source, not to a single append. The same event source may append its current value again without violating the constraint. If it claims a different value, the new value replaces its previous claim, freeing the old value for another event source.

Do not constrain a property containing the event source’s own id to prevent a second event on that source: it will re-claim its own value and pass. To allow an event type only once per event source, use a unique event type constraint instead.

If one project accumulates many version ids, retaining only its latest id lets another project reuse an earlier one. In .NET, set Mode = UniqueConstraintMode.PerValue to reserve every value until you explicitly release it. The default, PerEventSource, keeps the replacement behavior described above. Elixir exposes the same retention choice through :mode; the example here uses the .NET attribute.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
[EventType]
public record ScheduleVersionRegistered(
[property: Unique("ScheduleVersionId", Mode = UniqueConstraintMode.PerValue)] string VersionId);
[EventType]
[RemoveConstraint("ScheduleVersionId", Properties = [nameof(VersionId)])]
public record ScheduleVersionRemoved(string VersionId);
[EventType]
[RemoveConstraint("ScheduleVersionId")]
public record ScheduleVersionsRemoved;

View C# snippet source on GitHub

Appending two different version ids for the same project now keeps both reserved. Another project cannot claim either; the original project may append either again without a violation. ScheduleVersionRemoved releases only the id carried by that event, and only if its event source owns that value. ScheduleVersionsRemoved, which declares no removal properties, releases all values held by that event source in the constraint’s scope.

For a composite value, list removal properties in the same order as the constrained properties. A value-carrying event with no constrained properties present claims nothing and does not release earlier values. Properties sharing a constraint name must declare the same mode.

In .NET, [Unique] also works on event properties typed as ConceptAs<T>. A concept gives a domain value its own type, such as EmailAddress, while wrapping a primitive such as string or Guid. Chronicle checks uniqueness using that underlying value:

using Cratis.Concepts;
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
public record ConstraintsModelBoundUniqueEmailAddress(string Value) : ConceptAs<string>(Value);
[EventType]
public record ConstraintsModelBoundUniqueAuthorRegistered([property: Unique(name: "UniqueAuthorEmail")] ConstraintsModelBoundUniqueEmailAddress Email);

View C# snippet source on GitHub

When multiple event types share the same constraint name, Chronicle groups them into a single constraint. A value introduced by any of the participating events is checked against all others:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
[EventType]
public record ConstraintsModelBoundUniqueUserRegistered([property: Unique(name: "UniqueEmail")] string Email, string DisplayName);
[EventType]
public record ConstraintsModelBoundUniqueUserEmailChanged([property: Unique(name: "UniqueEmail")] string NewEmail);

View C# snippet source on GitHub

Both events now participate in UniqueEmail. A user can change to an unclaimed email address, but neither the registration event nor the email-change event can claim an address held by another event source. Under the default retention mode, changing the address frees the user’s previous one.

An event sequence is a separately indexed collection of events, such as the event log or outbox. By default, a constraint applies to every sequence its event type is appended to. Forwarding an event to the outbox therefore claims its value in the outbox’s index too; a removal event in the log does not release that outbox claim.

If the rule should protect only the event log, set EventSequences on the .NET [Unique] attribute. The Kernel neither validates nor indexes the constraint for sequences outside that selection:

using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
[EventType]
[RemoveConstraint("ConstraintsModelBoundUniqueEventLogInvitationEmail")]
public record ConstraintsModelBoundUniqueEventLogInvitationRevoked;
[EventType]
public record ConstraintsModelBoundUniqueEventLogInvitationSent(
[property: Unique("ConstraintsModelBoundUniqueEventLogInvitationEmail", EventSequences = [EventSequenceId.LogId])] string Email);

View C# snippet source on GitHub

When properties share a constraint name, their event sequence selections are combined. If any of those properties leaves EventSequences unset, the combined constraint applies to every sequence. Set the selection on every participating property to keep the rule limited to the log.

Changing the sequences takes effect when the client next registers its constraints. The kernel then rebuilds, in every namespace, the index of each sequence the constraint now applies to but did not before, from the events already in that sequence. A sequence the constraint no longer applies to stops validating it, and its index is no longer read until the constraint applies there again, when it is rebuilt.

Supply a message to explain why the caller’s append was rejected. Without one, Chronicle uses its default violation message. The .NET, TypeScript, Kotlin, and Java model-bound declarations accept fixed messages. Kotlin and Java use the annotations in io.cratis:chronicle. Elixir accepts :message as a string or a {Module, :function} callback:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
[EventType]
public record ConstraintsModelBoundUniqueMessageProjectCreated([property: Unique(message: "A project with this name already exists.")] string Name, string Description);

View C# snippet source on GitHub

Declare which event releases the value when its owner no longer needs it. In .NET, apply [RemoveConstraint] to that event type. Appending it releases the named constraint’s claims for that event source, so another source can claim the freed values:

using Cratis.Concepts;
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
public record ConstraintsModelBoundUniqueUserId(Guid Value) : ConceptAs<Guid>(Value);
[EventType]
[RemoveConstraint("UniqueEmail")]
public record ConstraintsModelBoundUniqueUserRemoved(ConstraintsModelBoundUniqueUserId UserId);

View C# snippet source on GitHub

The removal declaration must exactly match the constraint name. In .NET, an unnamed property constraint defaults to the property name. Use an explicit name when several event types share the rule.

An event type can release more than one constraint by stacking multiple attributes:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
[EventType]
[RemoveConstraint("UniqueEmail")]
[RemoveConstraint("UniqueUsername")]
public record ConstraintsModelBoundUniqueMultiRemoveUserRemoved(ConstraintsModelBoundUniqueUserId UserId);

View C# snippet source on GitHub

In .NET, more than one event type can release the same constraint. Name it from each event that ends the lifecycle so each can free the held values. Kotlin and Java accept only one removal event type per constraint name and keep the first one discovered:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
[EventType]
[RemoveConstraint("UniqueInvitedEmail")]
public record ConstraintsModelBoundUniqueSeveralInvitationAccepted;
[EventType]
[RemoveConstraint("UniqueInvitedEmail")]
public record ConstraintsModelBoundUniqueSeveralInvitationRevoked;
[EventType]
[RemoveConstraint("UniqueInvitedEmail")]
public record ConstraintsModelBoundUniqueSeveralInvitationExpired;

View C# snippet source on GitHub

The client reads the model-bound declarations and registers constraint definitions. The Kernel builds the indexes and checks appends against them server-side. Clients also resolve custom violation messages for the append results they return.

For case-insensitive matching or scope configuration beyond the attribute’s options, use the declarative unique property constraint.