use limit or undefined

const limit = searchParams?.get('limit'); const shipmentsQuery = db .select() .from(shipment) .orderBy(desc(shipment.createdAt)) .limit(limit? limit : undefined); i want to use limit when is has value if not then use without a limit?
9 Replies
Pradip Chaudhary
Hey @yasserconnect , You need to parse the limit value as int because that's what the limit function expects. const limit = searchParams?.get("limit"); const limitValue = limit ? parseInt(limit, 10) : null; const result = await db.select().from(shipment).orderBy(desc(shipment.createdAt)); if (limitValue) { result.limit(limitValue); } You can also set limitValue to 0 if limit is not there as drizzle treats 0 as having no limit only. const limit = searchParams?.get("limit"); const limitValue = limit ? parseInt(limit, 10) : 0; const result = await db.select().from(shipment).orderBy(desc(shipment.createdAt)).limit(limitValue);
yasserconnect
yasserconnect3mo ago
nice, i don't know about 0 i was use undefined.. thank you.
Pradip Chaudhary
Yess, that's what I did 😊
eXecuteye
eXecuteye4w ago
This approach does not work anymore from 0.32.1 onwards...
Pradip Chaudhary
Which one exactly @eXecuteye ?
eXecuteye
eXecuteye4w ago
passing a 0 does apply the limit since 0.32.1 (as highlighted in the release notes) -> it returns 0 rows https://github.com/drizzle-team/drizzle-orm/releases/tag/0.32.1 So this workaround with passing 0 does not work anymore and we also cannot pass null or undefined to "ignore" the limit...
GitHub
Release 0.32.1 · drizzle-team/drizzle-orm
Fix typings for indexes and allow creating indexes on 3+ columns mixing columns and expressions - thanks @lbguilherme! Added support for "limit 0" in all dialects - closes #2011 - thanks ...
rphlmr ⚡
rphlmr ⚡4w ago
https://github.com/drizzle-team/drizzle-orm/pull/2255/files I would try passing a negative number
const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0)
? sql` limit ${limit}`
: undefined;
const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0)
? sql` limit ${limit}`
: undefined;
Pradip Chaudhary
Thanks @eXecuteye and @Raphaël M (@rphlmr) ⚡ for this, I will keep this in mind now
Want results from more Discord servers?
Add your server