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:
export const getLatestRecords = db.query.Record.findMany({
  with: {
    owner: true
  },
  limit: sql.placeholder('limit'),
  orderBy: (records, { desc }) => [desc(records.createdAt)]
});


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🙏
Was this page helpful?