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
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);
nice, i don't know about 0 i was use undefined.. thank you.
Yes, you can try it here https://drizzle.run/uv3grzocedu7ljmuz3ocowpc
Drizzle Run
Optional limit - Drizzle Run
Yess, that's what I did 😊
This approach does not work anymore from 0.32.1 onwards...
Which one exactly @eXecuteye ?
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 ...
https://github.com/drizzle-team/drizzle-orm/pull/2255/files
I would try passing a negative number
Thanks @eXecuteye and @Raphaël M (@rphlmr) ⚡ for this, I will keep this in mind now