-
Notifications
You must be signed in to change notification settings - Fork 208
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
fix(loopback4-example-shopping): add unique email constraint to user … #136
Conversation
Thank you for the pull request. Can you add a test please? |
@bajtos Sorry, I will add tests and actually commit the controller additions. My mistake. Quick question before I resubmit. What would be your implementation recommendation, should I rely on the database to enforce constraint(as I originally intended) or should I search for existing email in controller? |
Since this is an example I opted to add check to the controller. Seemed simpler than to involve the db migrate functionality. |
We should rely on the database, that's the only reliable option. The solution you are proposing at controller level has two problems: (1) It's possible to write code that's calling (2) But even then, there is a race condition. If two requests to create a new user arrive at the same time, it's possible that they are handled in the following order:
See also loopbackio/loopback-next#2331 where we are discussing the problem of referential integrity & unique constraints, and related items like loopbackio/loopback-next#2606 & loopbackio/loopback-next#2712. |
IMO, the solution in 994ba09 was the right one. IIRC, we are using MongoDB to store the data and it is able to define unique constraints. So let's call |
@bajtos PTAL Few issues:
Also thank you for collating all the relevant posts. Very helpful. |
Right I'll stop fiddling, lack of precision was irking me. Refactored to use a named index (if there is a better way please enlighten me): 3c76834. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome 👏
Add migration.ts and package scripts Signed-off-by: dougal83 <[email protected]>
Prevent creation of user with email value that belongs to an existing user fix #103 Signed-off-by: dougal83 <[email protected]>
…model
add unique index to email in user model in file user.model.ts
fix #103