Skip to content

Commit d4e03a0

Browse files
committed
feat(filters): likeAllOf, likeAnyOf, ilikeAllOf, ilikeAnyOf
1 parent 0557752 commit d4e03a0

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

src/PostgrestFilterBuilder.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,32 @@ export default class PostgrestFilterBuilder<
123123
return this
124124
}
125125

126+
likeAllOf<ColumnName extends string & keyof Row>(column: ColumnName, patterns: string[]): this
127+
likeAllOf(column: string, patterns: string[]): this
128+
/**
129+
* Match only rows where `column` matches all of `patterns` case-sensitively.
130+
*
131+
* @param column - The column to filter on
132+
* @param patterns - The patterns to match with
133+
*/
134+
likeAllOf(column: string, patterns: string[]): this {
135+
this.url.searchParams.append(column, `like(all).{${patterns.join(',')}}`)
136+
return this
137+
}
138+
139+
likeAnyOf<ColumnName extends string & keyof Row>(column: ColumnName, patterns: string[]): this
140+
likeAnyOf(column: string, patterns: string[]): this
141+
/**
142+
* Match only rows where `column` matches any of `patterns` case-sensitively.
143+
*
144+
* @param column - The column to filter on
145+
* @param patterns - The patterns to match with
146+
*/
147+
likeAnyOf(column: string, patterns: string[]): this {
148+
this.url.searchParams.append(column, `like(any).{${patterns.join(',')}}`)
149+
return this
150+
}
151+
126152
ilike<ColumnName extends string & keyof Row>(column: ColumnName, pattern: string): this
127153
ilike(column: string, pattern: string): this
128154
/**
@@ -136,6 +162,32 @@ export default class PostgrestFilterBuilder<
136162
return this
137163
}
138164

165+
ilikeAllOf<ColumnName extends string & keyof Row>(column: ColumnName, patterns: string[]): this
166+
ilikeAllOf(column: string, patterns: string[]): this
167+
/**
168+
* Match only rows where `column` matches all of `patterns` case-insensitively.
169+
*
170+
* @param column - The column to filter on
171+
* @param patterns - The patterns to match with
172+
*/
173+
ilikeAllOf(column: string, patterns: string[]): this {
174+
this.url.searchParams.append(column, `ilike(all).{${patterns.join(',')}}`)
175+
return this
176+
}
177+
178+
ilikeAnyOf<ColumnName extends string & keyof Row>(column: ColumnName, patterns: string[]): this
179+
ilikeAnyOf(column: string, patterns: string[]): this
180+
/**
181+
* Match only rows where `column` matches any of `patterns` case-insensitively.
182+
*
183+
* @param column - The column to filter on
184+
* @param patterns - The patterns to match with
185+
*/
186+
ilikeAnyOf(column: string, patterns: string[]): this {
187+
this.url.searchParams.append(column, `ilike(any).{${patterns.join(',')}}`)
188+
return this
189+
}
190+
139191
is<ColumnName extends string & keyof Row>(
140192
column: ColumnName,
141193
value: Row[ColumnName] & (boolean | null)

test/filters.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,49 @@ test('like', async () => {
182182
`)
183183
})
184184

185+
test('likeAllOf', async () => {
186+
const res = await postgrest
187+
.from('users')
188+
.select('username')
189+
.likeAllOf('username', ['%supa%', '%bot%'])
190+
expect(res).toMatchInlineSnapshot(`
191+
Object {
192+
"count": null,
193+
"data": Array [
194+
Object {
195+
"username": "supabot",
196+
},
197+
],
198+
"error": null,
199+
"status": 200,
200+
"statusText": "OK",
201+
}
202+
`)
203+
})
204+
205+
test('likeAnyOf', async () => {
206+
const res = await postgrest
207+
.from('users')
208+
.select('username')
209+
.likeAnyOf('username', ['%supa%', '%kiwi%'])
210+
expect(res).toMatchInlineSnapshot(`
211+
Object {
212+
"count": null,
213+
"data": Array [
214+
Object {
215+
"username": "supabot",
216+
},
217+
Object {
218+
"username": "kiwicopple",
219+
},
220+
],
221+
"error": null,
222+
"status": 200,
223+
"statusText": "OK",
224+
}
225+
`)
226+
})
227+
185228
test('ilike', async () => {
186229
const res = await postgrest.from('users').select('username').ilike('username', '%SUPA%')
187230
expect(res).toMatchInlineSnapshot(`
@@ -199,6 +242,49 @@ test('ilike', async () => {
199242
`)
200243
})
201244

245+
test('ilikeAllOf', async () => {
246+
const res = await postgrest
247+
.from('users')
248+
.select('username')
249+
.ilikeAllOf('username', ['%SUPA%', '%bot%'])
250+
expect(res).toMatchInlineSnapshot(`
251+
Object {
252+
"count": null,
253+
"data": Array [
254+
Object {
255+
"username": "supabot",
256+
},
257+
],
258+
"error": null,
259+
"status": 200,
260+
"statusText": "OK",
261+
}
262+
`)
263+
})
264+
265+
test('ilikeAnyOf', async () => {
266+
const res = await postgrest
267+
.from('users')
268+
.select('username')
269+
.ilikeAnyOf('username', ['%supa%', '%KIWI%'])
270+
expect(res).toMatchInlineSnapshot(`
271+
Object {
272+
"count": null,
273+
"data": Array [
274+
Object {
275+
"username": "supabot",
276+
},
277+
Object {
278+
"username": "kiwicopple",
279+
},
280+
],
281+
"error": null,
282+
"status": 200,
283+
"statusText": "OK",
284+
}
285+
`)
286+
})
287+
202288
test('is', async () => {
203289
const res = await postgrest.from('users').select('data').is('data', null)
204290
expect(res).toMatchInlineSnapshot(`

0 commit comments

Comments
 (0)