How drizzle handle nulls and undefineds ?

I'm running this query, but some values in where clause are undefined, how do i make it optional ? Error: UNDEFINED_VALUE: Undefined values are not allowed query:
async list(companyId: number, lineClassId: number | undefined): Promise<Line[]> {
const lines = await this.dbContext.dbset
.select()
.from(lineModel)
.where(
and(eq(lineModel.companyId, companyId), eq(lineModel.status, 'ativo'), eq(lineModel.lineClass, lineClassId)),
);

return lines.map((line) => new Line(line.id, line.lineNumber, line.visa, line.companyId));
}
async list(companyId: number, lineClassId: number | undefined): Promise<Line[]> {
const lines = await this.dbContext.dbset
.select()
.from(lineModel)
.where(
and(eq(lineModel.companyId, companyId), eq(lineModel.status, 'ativo'), eq(lineModel.lineClass, lineClassId)),
);

return lines.map((line) => new Line(line.id, line.lineNumber, line.visa, line.companyId));
}
2 Replies
Angelelz
Angelelz15mo ago
There is not undefined in SQL, there is no way for drizzle to do something about it. I would create an array of eq like this:
const eqs = [
eq(lineModel.companyId, companyId),
eq(lineModel.status, 'ativo'),
];
if (lineClassId) {
eqs.push(eq(lineModel.lineClass, lineClassId));
}

...
.where(and(...eqs))
...
const eqs = [
eq(lineModel.companyId, companyId),
eq(lineModel.status, 'ativo'),
];
if (lineClassId) {
eqs.push(eq(lineModel.lineClass, lineClassId));
}

...
.where(and(...eqs))
...
victormesquita.
victormesquita.OP15mo ago
@angelelz Gonna try, thanks
Want results from more Discord servers?
Add your server