Drizzle always says data exists even tho it can be undefined in ts.

export const selectRenter = async (name: string) => {
return db.select().from(renter).where(eq(renter.name, name));
};
export const selectRenter = async (name: string) => {
return db.select().from(renter).where(eq(renter.name, name));
};
This is my service. Then i use it like this:
const [renter] = await getRenter(name);
const [renter] = await getRenter(name);
If i hover over renter it gives the data type without undefined being a possibility. This causes for me to forget to check if the data exists or not because linter does not warn. I think im doing something wrong would like your help
5 Replies
Mario564
Mario5644d ago
Not a definitive solution, but you can do this:
export const selectRenter = async (name: string) => {
return db.select().from(renter).where(eq(renter.name, name)).then((rows) => rows.at(0));
};
export const selectRenter = async (name: string) => {
return db.select().from(renter).where(eq(renter.name, name)).then((rows) => rows.at(0));
};
jafar3599
jafar3599OP4d ago
Hmm. looks very hacky There is no other better way? I mean is it a very specific thing that I want?
Mario564
Mario5644d ago
There is no other way, since it's not possible for Drizzle to know at the type level how many rows will be returned by a query
jafar3599
jafar3599OP3d ago
Ok the way you proposed works great. But is it really "standard" way to use drizzle orm? Or should i just leave it as array and have no typesafety and just check the length of array then select the first item?
Mario564
Mario5643d ago
We are indifferent as to the use of the then method, the point of Drizzle is for it to be flexible and suit your needs. If you're in a team and said team doesn't mind then you can absolutely feel free to use it
Want results from more Discord servers?
Add your server