Siraj ul Haq
Siraj ul Haq

Reputation: 885

Handle Duplicate insertion node js

What is the best way to handle duplicate insertion?

Either we should check before insertion if item already exist then notify user for duplicate entry or we can handle error message and let user know that its a duplicate entry.

Using first approach will cost us an extra database call.

Or if there is any other better approach to handle this please let me know.

Upvotes: 0

Views: 1474

Answers (2)

Somil
Somil

Reputation: 597

Duplicate insertion is at database level.

  • Your call to the api must be coming from front end.So you need to ensure that duplicate call is avoided at first place e.g you should disable the button as soon as user clicks it first time.

Or

  • You can add database schema level check like primary key so that if duplicate data comes error is thrown and same can be forwarded to user.

Or

Checking Whether data exists before insertion is a expensive call and that too you will have to hit on master so try to avoid that.

Upvotes: 1

PeterVC
PeterVC

Reputation: 4604

The best approach is to use a primary key based on the data. If this is not possible with your data then you'll have to query the database before insertion.

Upvotes: 0

Related Questions