Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for filtering by compound id #1806

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Changes from all commits
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
18 changes: 13 additions & 5 deletions packages/server/src/api/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1236,11 +1236,11 @@ class RequestHandler extends APIHandlerBase {
return r.toString();
}

private makePrismaIdFilter(idFields: FieldInfo[], resourceId: string) {
private makePrismaIdFilter(idFields: FieldInfo[], resourceId: string, nested: boolean = true) {
const decodedId = decodeURIComponent(resourceId);
if (idFields.length === 1) {
return { [idFields[0].name]: this.coerce(idFields[0].type, decodedId) };
} else {
} else if (nested) {
return {
// TODO: support `@@id` with custom name
[idFields.map((idf) => idf.name).join(prismaIdDivider)]: idFields.reduce(
Expand All @@ -1251,6 +1251,14 @@ class RequestHandler extends APIHandlerBase {
{}
),
};
} else {
return idFields.reduce(
(acc, curr, idx) => ({
...acc,
[curr.name]: this.coerce(curr.type, decodedId.split(this.idDivider)[idx]),
}),
{}
);
thomassnielsen marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -1594,11 +1602,11 @@ class RequestHandler extends APIHandlerBase {
const values = value.split(',').filter((i) => i);
const filterValue =
values.length > 1
? { OR: values.map((v) => this.makePrismaIdFilter(info.idFields, v)) }
: this.makePrismaIdFilter(info.idFields, value);
? { OR: values.map((v) => this.makePrismaIdFilter(info.idFields, v, false)) }
: this.makePrismaIdFilter(info.idFields, value, false);
return { some: filterValue };
} else {
return { is: this.makePrismaIdFilter(info.idFields, value) };
return { is: this.makePrismaIdFilter(info.idFields, value, false) };
ymc9 marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
const coerced = this.coerce(fieldInfo.type, value);
Expand Down
Loading