Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique field validation #153

Open
imissyouso opened this issue Feb 10, 2020 · 7 comments
Open

Unique field validation #153

imissyouso opened this issue Feb 10, 2020 · 7 comments

Comments

@imissyouso
Copy link

imissyouso commented Feb 10, 2020

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?

@imissyouso
Copy link
Author

imissyouso commented Feb 10, 2020

Also the way

If you really want to solve it in the domain, you need only 1 aggregate instance i.e. with a fix aggregate id (i.e. USER_EMAILS)

is not a suitable because for every check we need to load the whole aggregate with full list of emails and then make check directly in code (in cycle?). What if we have > 1k users? We cannot make such complex operation just for simple checking for uniqueness.

Thanks for reply in advance.

@adrai
Copy link
Contributor

adrai commented Feb 10, 2020

Hehe... welcome to the cqrs world...
Do not mix domain and eventDenormalizer stuff that way.
To solve this you really need 1 aggregate.
To make this more efficient when having too much events, there are snapshots.

@imissyouso
Copy link
Author

imissyouso commented Feb 10, 2020

Found similar question on stackoverflow
https://stackoverflow.com/questions/31386244/cqrs-event-sourcing-check-username-is-unique-or-not-from-eventstore-while-sendin
conclusion: there is no universal way how to do that. In most cases we have to put up with possible data inconsistency in such types of architectures and do not try to hack CAP theorem.

@nanov
Copy link
Contributor

nanov commented Feb 10, 2020

Having what @adrai said in mind, there are a few patterns you can apply to solve your problem, after you have decided that cqrs/es is suitable for youe user/account management:

  • in your case you can set the aggregate id as the email, and give the command a rule that the aggregate does not exists.

  • you may take a look on this answer that i gave for similar question - as last resort this is a solution.
    In your case

@dmitry-textmagic
Copy link

The second approach is quite clear. But in the first one, how should we manage email change? If we set an email as an aggregate id, then change email (and it was aggregate id), it causes that any other entities that have this aggregate id (email, in this case) inside would be linked to non-existent/invalid aggregate.

@adrai
Copy link
Contributor

adrai commented Feb 10, 2020

This is why I said, you need 1 aggregate for all email addresses...
Or solve this outside of cqrs.
PS. sorry for not responding with a more complete/longer answer... writing from hospital

@imissyouso
Copy link
Author

imissyouso commented Feb 10, 2020

This is why I said, you need 1 aggregate for all email addresses...
Or solve this outside of cqrs.
PS. sorry for not responding with a more complete/longer answer... writing from hospital

I wish you a speedy recovery!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants