Skip to content

Commit

Permalink
feat(filters): likeAllOf, likeAnyOf, ilikeAllOf, ilikeAnyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Apr 17, 2023
1 parent 0557752 commit d4e03a0
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/PostgrestFilterBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ export default class PostgrestFilterBuilder<
return this
}

likeAllOf<ColumnName extends string & keyof Row>(column: ColumnName, patterns: string[]): this
likeAllOf(column: string, patterns: string[]): this
/**
* Match only rows where `column` matches all of `patterns` case-sensitively.
*
* @param column - The column to filter on
* @param patterns - The patterns to match with
*/
likeAllOf(column: string, patterns: string[]): this {
this.url.searchParams.append(column, `like(all).{${patterns.join(',')}}`)
return this
}

likeAnyOf<ColumnName extends string & keyof Row>(column: ColumnName, patterns: string[]): this
likeAnyOf(column: string, patterns: string[]): this
/**
* Match only rows where `column` matches any of `patterns` case-sensitively.
*
* @param column - The column to filter on
* @param patterns - The patterns to match with
*/
likeAnyOf(column: string, patterns: string[]): this {
this.url.searchParams.append(column, `like(any).{${patterns.join(',')}}`)
return this
}

ilike<ColumnName extends string & keyof Row>(column: ColumnName, pattern: string): this
ilike(column: string, pattern: string): this
/**
Expand All @@ -136,6 +162,32 @@ export default class PostgrestFilterBuilder<
return this
}

ilikeAllOf<ColumnName extends string & keyof Row>(column: ColumnName, patterns: string[]): this
ilikeAllOf(column: string, patterns: string[]): this
/**
* Match only rows where `column` matches all of `patterns` case-insensitively.
*
* @param column - The column to filter on
* @param patterns - The patterns to match with
*/
ilikeAllOf(column: string, patterns: string[]): this {
this.url.searchParams.append(column, `ilike(all).{${patterns.join(',')}}`)
return this
}

ilikeAnyOf<ColumnName extends string & keyof Row>(column: ColumnName, patterns: string[]): this
ilikeAnyOf(column: string, patterns: string[]): this
/**
* Match only rows where `column` matches any of `patterns` case-insensitively.
*
* @param column - The column to filter on
* @param patterns - The patterns to match with
*/
ilikeAnyOf(column: string, patterns: string[]): this {
this.url.searchParams.append(column, `ilike(any).{${patterns.join(',')}}`)
return this
}

is<ColumnName extends string & keyof Row>(
column: ColumnName,
value: Row[ColumnName] & (boolean | null)
Expand Down
86 changes: 86 additions & 0 deletions test/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,49 @@ test('like', async () => {
`)
})

test('likeAllOf', async () => {
const res = await postgrest
.from('users')
.select('username')
.likeAllOf('username', ['%supa%', '%bot%'])
expect(res).toMatchInlineSnapshot(`
Object {
"count": null,
"data": Array [
Object {
"username": "supabot",
},
],
"error": null,
"status": 200,
"statusText": "OK",
}
`)
})

test('likeAnyOf', async () => {
const res = await postgrest
.from('users')
.select('username')
.likeAnyOf('username', ['%supa%', '%kiwi%'])
expect(res).toMatchInlineSnapshot(`
Object {
"count": null,
"data": Array [
Object {
"username": "supabot",
},
Object {
"username": "kiwicopple",
},
],
"error": null,
"status": 200,
"statusText": "OK",
}
`)
})

test('ilike', async () => {
const res = await postgrest.from('users').select('username').ilike('username', '%SUPA%')
expect(res).toMatchInlineSnapshot(`
Expand All @@ -199,6 +242,49 @@ test('ilike', async () => {
`)
})

test('ilikeAllOf', async () => {
const res = await postgrest
.from('users')
.select('username')
.ilikeAllOf('username', ['%SUPA%', '%bot%'])
expect(res).toMatchInlineSnapshot(`
Object {
"count": null,
"data": Array [
Object {
"username": "supabot",
},
],
"error": null,
"status": 200,
"statusText": "OK",
}
`)
})

test('ilikeAnyOf', async () => {
const res = await postgrest
.from('users')
.select('username')
.ilikeAnyOf('username', ['%supa%', '%KIWI%'])
expect(res).toMatchInlineSnapshot(`
Object {
"count": null,
"data": Array [
Object {
"username": "supabot",
},
Object {
"username": "kiwicopple",
},
],
"error": null,
"status": 200,
"statusText": "OK",
}
`)
})

test('is', async () => {
const res = await postgrest.from('users').select('data').is('data', null)
expect(res).toMatchInlineSnapshot(`
Expand Down

0 comments on commit d4e03a0

Please sign in to comment.