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(supabase): fixed 'in'-filter when used inside 'or' #6652

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions .changeset/good-eyes-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@refinedev/supabase": patch
---

fix: fixed 'in'-filter when used inside 'or' not generating correct supabase syntax

When using the 'in'-filter inside a Conditional 'or' Filter the syntax being pushed to supabase would be incorrect.

This resolves #6651
3 changes: 3 additions & 0 deletions packages/supabase/src/utils/generateFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export const generateFilter = (filter: CrudFilter, query: any) => {
if (item.operator === "endswith") {
value = `%${value}`;
}
if (item.operator === "in") {
value = `(${item.value.map((val: any) => `"${val}"`).join(",")})`;
}

return `${item.field}.${mapOperator(item.operator)}.${value}`;
}
Expand Down
74 changes: 74 additions & 0 deletions packages/supabase/test/getList/index.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2073,3 +2073,77 @@ nock("https://iwdfzvfqbtokqetmbmbp.supabase.co:443", {
'h3=":443"; ma=86400',
],
);

nock("https://iwdfzvfqbtokqetmbmbp.supabase.co:443", {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's please record the actualy request. Here we are providing the mock data, without making the request, and we are testing it based on the data provided by us. It's not really testing anything.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, already thought so... is there a reason why the other tests also don't make actual requests or am i reading something wrong?

nevertheless will change to a real request

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are recorded from actual requests, so next time you do the same request, nock will return recorded response. You can check jest.setup.ts file, there should be a commented line goes like nock.recorder.rec(), you can uncomment it and run the single test you need, it should print request/response to the console. Then you can paste it here.

encodedQueryParams: true,
})
.get("/rest/v1/posts")
.query({
select: "%2A",
offset: "0",
limit: "10",
or: "%28id.in.%28%221%22%2C%222%22%29%29",
})
.reply(
200,
[
{
id: 1,
title: "Black Psorotichia Lichen",
slug: "61a31089-c85d-48a0-a4be-d5dce5c96b6a",
createdAt: "2024-04-24T13:20:10.200327+00:00",
content:
"Integer tincidunt ante vel ipsum. Praesent blandit lacinia erat. Vestibulum sed magna at nunc commodo placerat.\n\nPraesent blandit. Nam nulla. Integer pede justo, lacinia eget, tincidunt eget, tempus vel, pede.\n\nMorbi porttitor lorem id ligula. Suspendisse ornare consequat lectus. In est risus, auctor sed, tristique in, tempus sit amet, sem.",
categoryId: 7,
images: null,
},
{
id: 2,
title: "Dust Lichen",
slug: "ca038c02-0c6f-417b-aaf8-6f51b777d1f5",
createdAt: "2024-04-24T13:20:10.200327+00:00",
content:
"Fusce posuere felis sed lacus. Morbi sem mauris, laoreet ut, rhoncus aliquet, pulvinar sed, nisl. Nunc rhoncus dui vel sem.\n\nSed sagittis. Nam congue, risus semper porta volutpat, quam pede lobortis ligula, sit amet eleifend pede libero quis orci. Nullam molestie nibh in lectus.",
categoryId: 7,
images: null,
},
],
[
"Date",
"Thu, 25 Apr 2024 07:26:59 GMT",
"Content-Type",
"application/json; charset=utf-8",
"Transfer-Encoding",
"chunked",
"Connection",
"close",
"Content-Range",
"0-1/2",
"CF-Ray",
"879c9ba35d5b68ae-IST",
"CF-Cache-Status",
"DYNAMIC",
"Access-Control-Allow-Origin",
"*",
"Content-Location",
"/posts?limit=10&offset=0&or=%28title.ilike.%25Black+Psorotichia%25%2Ccontent.ilike.%25Sed+sagittis%25%29&select=%2A",
"Strict-Transport-Security",
"max-age=15552000; includeSubDomains",
"Vary",
"Accept-Encoding",
"Via",
"kong/2.8.1",
"content-profile",
"public",
"sb-gateway-version",
"1",
"x-kong-proxy-latency",
"1",
"x-kong-upstream-latency",
"3",
"Server",
"cloudflare",
"alt-svc",
'h3=":443"; ma=86400',
],
);
17 changes: 17 additions & 0 deletions packages/supabase/test/getList/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,23 @@ describe("filtering", () => {
expect(total).toBe(2);
});

it("in operator should work correctly with or", async () => {
const { data, total } = await dataProvider(supabaseClient).getList({
resource: "posts",
filters: [
{
operator: "or",
value: [{ field: "id", operator: "in", value: [1, 2] }],
},
],
});

expect(data).toHaveLength(2);
expect(data[0].title).toBe("Black Psorotichia Lichen");
expect(data[1].title).toBe("Dust Lichen");
expect(total).toBe(2);
});

it("should change schema", async () => {
const { data } = await dataProvider(supabaseClient).getList({
resource: "posts",
Expand Down