Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/runtime/src/client/crud/operations/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,21 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
// distinct
let inMemoryDistinct: string[] | undefined = undefined;
if (args?.distinct) {
const distinct = ensureArray(args.distinct);
const distinct = ensureArray(args.distinct) as string[];
if (this.dialect.supportsDistinctOn) {
query = query.distinctOn(distinct.map((f: any) => sql.ref(`${model}.${f}`)));
query = query.distinctOn(distinct.map((f) => sql.ref(`${model}.${f}`)));
} else {
// in-memory distinct after fetching all results
inMemoryDistinct = distinct;

// make sure distinct fields are selected
query = distinct.reduce(
(acc, field) =>
acc.select((eb) =>
buildFieldRef(this.schema, model, field, this.options, eb).as(`$distinct$${field}`),
),
query,
);
}
}

Expand Down Expand Up @@ -225,13 +234,20 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
const distinctResult: Record<string, unknown>[] = [];
const seen = new Set<string>();
for (const r of result as any[]) {
const key = safeJSONStringify(inMemoryDistinct.map((f) => r[f]))!;
const key = safeJSONStringify(inMemoryDistinct.map((f) => r[`$distinct$${f}`]))!;
if (!seen.has(key)) {
distinctResult.push(r);
seen.add(key);
}
}
result = distinctResult;

// clean up distinct utility fields
for (const r of result) {
for (const f of inMemoryDistinct) {
delete r[`$distinct$${f}`];
}
Comment thread
ymc9 marked this conversation as resolved.
Outdated
}
}

return result;
Expand Down
20 changes: 19 additions & 1 deletion packages/runtime/test/client-api/find.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ describe.each(createClientSpecs(PG_DB_NAME))('Client find tests for $provider',
await createUser(client, 'u1@test.com', {
name: 'Admin1',
role: 'ADMIN',
profile: { create: { bio: 'Bio1' } },
});
await createUser(client, 'u3@test.com', {
name: 'User',
Expand All @@ -252,14 +253,15 @@ describe.each(createClientSpecs(PG_DB_NAME))('Client find tests for $provider',
await createUser(client, 'u2@test.com', {
name: 'Admin2',
role: 'ADMIN',
profile: { create: { bio: 'Bio1' } },
});
await createUser(client, 'u4@test.com', {
name: 'User',
role: 'USER',
});

// single field distinct
let r = await client.user.findMany({ distinct: ['role'] });
let r: any = await client.user.findMany({ distinct: ['role'] });
expect(r).toHaveLength(2);
expect(r).toEqual(
expect.arrayContaining([
Expand All @@ -268,6 +270,22 @@ describe.each(createClientSpecs(PG_DB_NAME))('Client find tests for $provider',
]),
);

// distinct with include
r = await client.user.findMany({ distinct: ['role'], include: { profile: true } });
expect(r).toHaveLength(2);
expect(r).toEqual(
expect.arrayContaining([
expect.objectContaining({ role: 'ADMIN', profile: expect.any(Object) }),
expect.objectContaining({ role: 'USER', profile: null }),
]),
);

// distinct with select
r = await client.user.findMany({ distinct: ['role'], select: { email: true } });
console.log(r);
Comment thread
ymc9 marked this conversation as resolved.
Outdated
expect(r).toHaveLength(2);
expect(r).toEqual(expect.arrayContaining([{ email: expect.any(String) }, { email: expect.any(String) }]));

// multiple fields distinct
r = await client.user.findMany({
distinct: ['role', 'name'],
Expand Down