Skip to content

Unique field validation #153

@imissyouso

Description

@imissyouso

refers to #70, #91

Hi, I have the similar question: how to make checks for uniqueness in domain?
I tried to check it using eventDenormalizer by saving unique values (emails) into VM by listen domain events and then check it in preCondition:

import * as domain from "cqrs-domain";

export = domain.definePreCondition({
    // @ts-ignore
    name: ['createUser', 'updateUser'],
    priority: 1,
    payload: 'payload',
},  (data, aggregate, callback) => {
    const usersRepo = require('../viewBuilders/user/usersCollection');
    usersRepo.findViewModels({email: data.email}, (err, items) => {
        if(items.length){
            callback("Provided email is already exist in our database!");
        } else {
            callback(null);
        }
    });
});

but this way is not atomic, theoretically there can be situation when 2 users can create their accounts with the same emails. How to avoid this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions