I discovered this strange behaviour of typeorm, which is that if the property in the condition of WHERE clause of the FIND query (find, findOne, findBy etc) is undefined, then it behaves like a true condition and returns records (first record for findOne and all records for find).
I know this can be prevented using validators or an IF statement to check against undefined property, but I want to know a direct way with typeorm itself to handle it, apart from this.
Here is what I am doing:
const client = await this.clientRepository.findOne({
where: {
id: payload.clientId //undefined
}
});
I got the first record of the client table, even though the clientId was undefined.
This is a known issue
The easiest solution using typeORM itself is to use Equal()
function of typeORM.
const client = await this.clientRepository.findOne({
where: {
id: Equal(payload.clientId)
}
});