Select One UX improvement

I have this
const arr = await db.select().from(worlds).where(eq(worlds.id, id)).limit(1)
const world = arr[0]
const arr = await db.select().from(worlds).where(eq(worlds.id, id)).limit(1)
const world = arr[0]
and that's fine and works, and great. but what I really want is to get rid of that second line. I don't want an array back if I know I'm only aiming to get a single record back. Is there a way to do this? Otherwise, let this be a little UX improvement suggestion and you can ignore it / adopt it as you see fit. Thanks!
5 Replies
Dan
Dan2y ago
you can use relational queries for that, they provide findFirst
iqrow
iqrowOP2y ago
Thanks, I'll check it out, looks like a different mental model and structure to the schema/queries that I'll have to pick up to use it. I saw these before when searching for findFirst but what I'm after isn't semantically a "relationship". I'm getting one from a table of one, no relationship with other entities there.
Dan
Dan2y ago
you can use it without any joined relations it's called relational QB because it allows querying relational data, but that's entirely optional you can use it just as a higher-level query builder
marcus.og
marcus.og2y ago
you are mixing promises with await/async...
Want results from more Discord servers?
Add your server