Count Vs Exists (ARCH3063)
هذا المحتوى غير متوفر بلغتك بعد.
Key typescript.orm.count-vs-exists · Severity info · Category orm
Testing for row presence with count() > 0 or a length check counts all rows when a cheaper exists/any query exists. The database can stop at the first row.
Incorrect
Section titled “Incorrect”const count = await userRepo.count({ where: { email } });if (count > 0) { throw new Error('already exists');}Correct
Section titled “Correct”const exists = await userRepo.exists({ where: { email } });if (exists) { throw new Error('already exists');}Test for presence with exists/any so the DB short-circuits at the first match.