Search code examples
node.jsnode-mssql

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.


Solution

  • 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.