Skip to content

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.

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],
};

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,
});
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.

try {
await eventStore.append([event], {
failIfEventsMatch: query,
after: lastPosition,
});
} catch (error) {
if (isAppendConditionError(error)) {
throw new Error('Username already taken');
}
throw error;
}
  • 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