Ir al contenido

Count Vs Exists (ARCH3063)

Esta página aún no está disponible en tu idioma.

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.

const count = await userRepo.count({ where: { email } });
if (count > 0) {
throw new Error('already exists');
}
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.