DT
Join ServerDrizzle Team
help
select with limit of 1
Is there a cleaner way of selecting only one item with proper type safety than this?
const user: User | undefined = (await db.select().from(users).where(eq(users.id, userId)).limit(1))[0];
well, you don't need to specify the type explicitly, other than that it's fine
When i don't specify the type it's only User but it can be undefined when the user with that id does not exist in the database. That's why i do that.
idk it feels a bit weird that the select returns an array with a limit of 1
set
"noUncheckedIndexedAccess": true
in tsconfigoh didn't know that exists
ohh that produces so much chaos in my codebase 😅
you could also add
?.
before [0]
so the type is User | undefined
I've been doing something like
Which seems to get the inference right but I don't know how typesafe this is.
const [user] = await db.select().from(users).where(eq(users.id, userId)).limit(1);
Which seems to get the inference right but I don't know how typesafe this is.