From 8e66cbe7baeb0e899a8deefe0c54af594012302c Mon Sep 17 00:00:00 2001 From: Arshdeep Singh Date: Mon, 2 Jun 2025 11:06:07 +0530 Subject: [PATCH] Enforce uniqueness on short_code + expiry_date + user_id via DB trigger --- dbConfig/index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/dbConfig/index.js b/dbConfig/index.js index 44131f9..10e38af 100644 --- a/dbConfig/index.js +++ b/dbConfig/index.js @@ -72,6 +72,15 @@ const initDB = async () => { SELECT 1 FROM urls WHERE user_id = NEW.user_id AND customized_shortner_slug = NEW.customized_shortner_slug ); + + -- Case 3: Prevent reinserting same short_code + expiry_date + user_id + slug (if slug is not null) + SELECT RAISE(ABORT, 'Duplicate URL record not allowed') + WHERE EXISTS ( + SELECT 1 FROM urls + WHERE short_code = NEW.short_code + AND expiry_date = NEW.expiry_date + AND user_id = NEW.user_id + ); END; `, function (err) {