From ba8bf6820a8f19d05bf01dcf8747da9db71cb413 Mon Sep 17 00:00:00 2001 From: Thomas Sunde Nielsen Date: Thu, 24 Oct 2024 11:11:26 +0200 Subject: [PATCH 1/2] Fix for filtering by compound id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In some cases Prisma doesn’t expect these to be nested under the compound id key. --- packages/server/src/api/rest/index.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/rest/index.ts b/packages/server/src/api/rest/index.ts index 2e0bcaec..2c9dc69a 100644 --- a/packages/server/src/api/rest/index.ts +++ b/packages/server/src/api/rest/index.ts @@ -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( @@ -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]), + }), + {} + ); } } @@ -1598,7 +1606,7 @@ class RequestHandler extends APIHandlerBase { : this.makePrismaIdFilter(info.idFields, value); return { some: filterValue }; } else { - return { is: this.makePrismaIdFilter(info.idFields, value) }; + return { is: this.makePrismaIdFilter(info.idFields, value, false) }; } } else { const coerced = this.coerce(fieldInfo.type, value); From 5490bad122098909f86dc2fa15dd12b7ed61f6b6 Mon Sep 17 00:00:00 2001 From: Thomas Sunde Nielsen Date: Fri, 25 Oct 2024 08:02:17 +0200 Subject: [PATCH 2/2] Update remaining makePrismaIdFilter calls --- packages/server/src/api/rest/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/rest/index.ts b/packages/server/src/api/rest/index.ts index 2c9dc69a..e8d5138c 100644 --- a/packages/server/src/api/rest/index.ts +++ b/packages/server/src/api/rest/index.ts @@ -1602,8 +1602,8 @@ 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, false) };