How to use findMany() with distinct column values?
Hi Everyone 🙂
I’m using the
findMany()
query in my project to retrieve the latest records from an SQLite (libsql) database:
I want to modify it to exclude any records with duplicate ownerId
column values.
If more than one record exists for a specific ownderId
, I would like to get one (the latest) and exclude all others.
Does something like distinctOn
already exist in Drizzle?
I would highly appreciate any assistance.
Thanks a lot🙏4 Replies
Hi 👋
It exists but with the select API https://orm.drizzle.team/docs/select#distinct (postgres only for distinctOn)
Drizzle ORM - Select
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Thanks @Raphaël M (@rphlmr) ⚡!
If that's the case, maybe you can try to help me think of a way to achieve the desired query? if I understand correctly, the absence of
distinctOn
means I can't use the findMany API for this specific case, right?Yes that's it.
Here a possible query: https://drizzle.run/u5l9ngj2nayw7ly7e5fhl8kg
Drizzle Run
Select distinct on - Drizzle Run