Enforce uniqueness with DCB
Make usernames or emails unique across all streams
You want a rule like “this username can only exist once”. That rule spans every user stream, so DCB is the right tool.
Enforce a unique username across all users using tags + append conditions.
Step 1: Tag the event
Section titled “Step 1: Tag the event”Pick a tag format and stick to it:
const tag = `username:${username.toLowerCase()}`;When you append the event, include the tag:
const event = { type: 'UserRegistered', data: { userId, username }, tags: [tag],};Step 2: Read by query
Section titled “Step 2: Read by query”Use a query that matches any existing user with the same username:
const query = { items: [ { types: ['UserRegistered'], tags: [tag], }, ],};
const { events, lastPosition } = await eventStore.readByQuery({ query, after: 0,});Step 3: Decide and append with condition
Section titled “Step 3: Decide and append with condition”if (events.length > 0) { throw new Error('Username already taken');}
await eventStore.append([event], { failIfEventsMatch: query, after: lastPosition,});If someone else registers the same username between your read and append, the append fails with a 409.
Step 4: Handle conflicts
Section titled “Step 4: Handle conflicts”try { await eventStore.append([event], { failIfEventsMatch: query, after: lastPosition, });} catch (error) { if (isAppendConditionError(error)) { throw new Error('Username already taken'); } throw error;}What’s next
Section titled “What’s next”- Use multiple tags for compound uniqueness (e.g.
tenant:acme,email:...) - Switch to DCB for other global constraints (invoice numbers, limits)
- Keep stream-based logic where it already works