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

fix(loopback4-example-shopping): add unique email constraint to user … #136

Merged
merged 2 commits into from
Jun 18, 2019
Merged

fix(loopback4-example-shopping): add unique email constraint to user … #136

merged 2 commits into from
Jun 18, 2019

Conversation

dougal83
Copy link
Contributor

@dougal83 dougal83 commented May 27, 2019

…model

add unique index to email in user model in file user.model.ts

fix #103

@bajtos
Copy link
Member

bajtos commented Jun 4, 2019

Thank you for the pull request. Can you add a test please?

@dougal83
Copy link
Contributor Author

dougal83 commented Jun 5, 2019

@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?

@dougal83
Copy link
Contributor Author

dougal83 commented Jun 6, 2019

Since this is an example I opted to add check to the controller. Seemed simpler than to involve the db migrate functionality.

@bajtos
Copy link
Member

bajtos commented Jun 7, 2019

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 this.userRepository.create (e.g. in a new controller method, or perhaps a script seeding database) and bypassing your check. At minimum, the check needs to be implemented in UserRepository.

(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:

  1. Request1 arrives, sends count query to the database
  2. Request2 arrives, sends count query to the database
  3. count returns 0, Request1 proceeds to create a new user
  4. count returns 0, Request2 proceeds to create a new user
  5. BOOM. We have two users with the same email.

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.

@bajtos
Copy link
Member

bajtos commented Jun 7, 2019

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 automigrate from a before hook to ensure the database is properly set up for the tests.

@dougal83
Copy link
Contributor Author

dougal83 commented Jun 7, 2019

@bajtos PTAL

Few issues:

  • Is it OK to assume the MongoError 11000 duplicate key is email at this point? (EDIT: added extra check)
  • Is the test OK for double post or is there a better way to double post the same user?
  • Would it be helpful to add migrate.ts and associated package script too or should I leave that out of PR? (EDIT: added extra commit JIC)

Also thank you for collating all the relevant posts. Very helpful.

@dougal83
Copy link
Contributor Author

dougal83 commented Jun 14, 2019

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.

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome 👏

dougal83 added 2 commits June 18, 2019 09:38
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]>
@bajtos bajtos merged commit 889125c into loopbackio:master Jun 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Login issue: different user can use the same email.
2 participants