Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit 7841c35

Browse files
authored
feat: support embedded or filter (#160)
1 parent 791643b commit 7841c35

File tree

3 files changed

+152
-2
lines changed

3 files changed

+152
-2
lines changed

src/lib/PostgrestFilterBuilder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ export default class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder
4747
* Finds all rows satisfying at least one of the filters.
4848
*
4949
* @param filters The filters to use, separated by commas.
50+
* @param foreignTable The foreign table to use (if `column` is a foreign column).
5051
*/
51-
or(filters: string): this {
52-
this.url.searchParams.append('or', `(${filters})`)
52+
or(filters: string, { foreignTable }: { foreignTable?: string } = {}): this {
53+
const key = typeof foreignTable === 'undefined' ? 'or' : `${foreignTable}.or`
54+
this.url.searchParams.append(key, `(${filters})`)
5355
return this
5456
}
5557

test/__snapshots__/index.test.ts.snap

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,140 @@ Object {
790790
}
791791
`;
792792

793+
exports[`embedded filters embedded or 1`] = `
794+
Object {
795+
"body": Array [
796+
Object {
797+
"messages": Array [
798+
Object {
799+
"channel_id": 1,
800+
"data": null,
801+
"id": 1,
802+
"message": "Hello World 👋",
803+
"username": "supabot",
804+
},
805+
Object {
806+
"channel_id": 2,
807+
"data": null,
808+
"id": 2,
809+
"message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.",
810+
"username": "supabot",
811+
},
812+
],
813+
},
814+
Object {
815+
"messages": Array [],
816+
},
817+
Object {
818+
"messages": Array [],
819+
},
820+
Object {
821+
"messages": Array [],
822+
},
823+
],
824+
"count": null,
825+
"data": Array [
826+
Object {
827+
"messages": Array [
828+
Object {
829+
"channel_id": 1,
830+
"data": null,
831+
"id": 1,
832+
"message": "Hello World 👋",
833+
"username": "supabot",
834+
},
835+
Object {
836+
"channel_id": 2,
837+
"data": null,
838+
"id": 2,
839+
"message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.",
840+
"username": "supabot",
841+
},
842+
],
843+
},
844+
Object {
845+
"messages": Array [],
846+
},
847+
Object {
848+
"messages": Array [],
849+
},
850+
Object {
851+
"messages": Array [],
852+
},
853+
],
854+
"error": null,
855+
"status": 200,
856+
"statusText": "OK",
857+
}
858+
`;
859+
860+
exports[`embedded filters embedded or with and 1`] = `
861+
Object {
862+
"body": Array [
863+
Object {
864+
"messages": Array [
865+
Object {
866+
"channel_id": 1,
867+
"data": null,
868+
"id": 1,
869+
"message": "Hello World 👋",
870+
"username": "supabot",
871+
},
872+
Object {
873+
"channel_id": 2,
874+
"data": null,
875+
"id": 2,
876+
"message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.",
877+
"username": "supabot",
878+
},
879+
],
880+
},
881+
Object {
882+
"messages": Array [],
883+
},
884+
Object {
885+
"messages": Array [],
886+
},
887+
Object {
888+
"messages": Array [],
889+
},
890+
],
891+
"count": null,
892+
"data": Array [
893+
Object {
894+
"messages": Array [
895+
Object {
896+
"channel_id": 1,
897+
"data": null,
898+
"id": 1,
899+
"message": "Hello World 👋",
900+
"username": "supabot",
901+
},
902+
Object {
903+
"channel_id": 2,
904+
"data": null,
905+
"id": 2,
906+
"message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.",
907+
"username": "supabot",
908+
},
909+
],
910+
},
911+
Object {
912+
"messages": Array [],
913+
},
914+
Object {
915+
"messages": Array [],
916+
},
917+
Object {
918+
"messages": Array [],
919+
},
920+
],
921+
"error": null,
922+
"status": 200,
923+
"statusText": "OK",
924+
}
925+
`;
926+
793927
exports[`embedded select 1`] = `
794928
Object {
795929
"body": Array [

test/resource-embedding.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ describe('embedded filters', () => {
1313
const res = await postgrest.from('users').select('messages(*)').eq('messages.channel_id', 1)
1414
expect(res).toMatchSnapshot()
1515
})
16+
test('embedded or', async () => {
17+
const res = await postgrest
18+
.from('users')
19+
.select('messages(*)')
20+
.or('channel_id.eq.2,message.eq.Hello World 👋', { foreignTable: 'messages' })
21+
expect(res).toMatchSnapshot()
22+
})
23+
test('embedded or with and', async () => {
24+
const res = await postgrest
25+
.from('users')
26+
.select('messages(*)')
27+
.or('channel_id.eq.2,and(message.eq.Hello World 👋,username.eq.supabot)', { foreignTable: 'messages' })
28+
expect(res).toMatchSnapshot()
29+
})
1630
})
1731

1832
describe('embedded transforms', () => {

0 commit comments

Comments
 (0)