View Source Supabase.PostgREST.FilterBuilder (supabase_postgrest v1.0.0)

Manages filtering logic for queries.

This module allows you to define conditions that restrict the data returned by the query. Filters can include equality checks, range conditions, pattern matching, and more. These operations translate into query parameters that control the subset of data fetched or manipulated.

Summary

Functions

Applies an "AND" condition to a query, allowing multiple conditions on different columns. This can also be scoped to a foreign table if specified.

Applies an "OR" condition to a query, combining multiple conditions on different columns where at least one condition must be met. This can also be scoped to a foreign table.

Only relevant for jsonb, array, and range columns. Match only rows where every element appearing in column is contained by value.

Only relevant for jsonb, array, and range columns. Match only rows where column contains every element appearing in value.

Match only rows where column is equal to value.

Match only rows which satisfy the filter. This is an escape hatch - you hould use the specific filter methods wherever possible.

Adds a 'greater than' filter to the query, specifying that the column's value must be greater than the specified value.

Adds a 'greater than or equal to' filter to the query, specifying that the column's value must be greater than or equal to the specified value.

Adds an 'ilike' filter to the query, allowing for case-insensitive pattern matching (SQL ILIKE).

Match only rows where column matches all of patterns case-insensitively.

Match only rows where column matches any of patterns case-insensitively.

Match only rows where column IS value.

Guard to validates if the filter operator passed to __MODULE__.filter/3 is a valid operator.

Adds a 'like' filter to the query, allowing for simple pattern matching (SQL LIKE).

Match only rows where column matches all of patterns case-sensitively.

Match only rows where column matches any of patterns case-sensitively.

Adds a 'less than' filter to the query, specifying that the column's value must be less than the specified value.

Adds a 'less than or equal to' filter to the query, specifying that the column's value must be less than or equal to the specified value.

Match only rows where each column in query keys is equal to its associated value. Shorthand for multiple .eq()s.

Applies a "NOT" condition to the query, negating a specified condition.

Match only rows where column is not equal to value.

Only relevant for array and range columns. Match only rows where column and value have an element in common.

Only relevant for range columns. Match only rows where column is mutually exclusive to range and there can be no element between the two ranges.

Only relevant for range columns. Match only rows where every element in column is greater than any element in range.

Only relevant for range columns. Match only rows where every element in column is either contained in range or greater than any element in range.

Only relevant for range columns. Match only rows where every element in column is less than any element in range.

Only relevant for range columns. Match only rows where every element in column is either contained in range or less than any element in range.

Only relevant for text and tsvector columns. Match only rows where column matches the query string in query.

Filters the query by checking if the column's value is within an array of specified values.

Functions

all_of(builder, patterns, opts \\ [])

Applies an "AND" condition to a query, allowing multiple conditions on different columns. This can also be scoped to a foreign table if specified.

Unlike most filters, filters is used as-is and needs to follow PostgREST syntax. You also need to make sure it's properly sanitized.

It's currently not possible to do an .and() filter across multiple tables.

You can optionally use the custom DSL to represent conditions instead of a raw string, look at the examples and the Supabase.PostgREST.FilterBuilder.Behaviour.condition() type spec.

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • columns: A list of conditions that should all be met.
  • opts: Optional parameters, which can include specifying a foreign table.

Examples

iex> PostgREST.all_of(builder, [{:gt, "age", 18}, {:eq, "status", "active"}])
iex> PostgREST.all_of([
iex>      {:gt, "age", 18},
iex>      {:and, [
iex>        {:lt, "salary", 5000},
iex>        {:eq, "role", "junior"}
iex>      ]}
iex>    ])

See also

any_of(builder, patterns, opts \\ [])

Applies an "OR" condition to a query, combining multiple conditions on different columns where at least one condition must be met. This can also be scoped to a foreign table.

Unlike most filters, filters is used as-is and needs to follow PostgREST syntax. You also need to make sure it's properly sanitized.

It's currently not possible to do an .and() filter across multiple tables.

You can optionally use the custom DSL to represent conditions instead of a raw string, look at the examples and the Supabase.PostgREST.FilterBuilder.Behaviour.condition() type spec.

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • columns: A list of conditions where at least one should be met.
  • opts: Optional parameters, which can include specifying a foreign table.

Examples

iex> PostgREST.any_of(builder, [{:gt, "age", 18}, {:eq, "status", "active"}])
iex> PostgREST.any_of([
iex>      {:gt, "age", 18},
iex>      {:or, [
iex>        {:eq, "status", "active"},
iex>        {:eq, "status", "pending"}
iex>      ]},
iex>    ])

See also

contained_by(b, column, value)

Only relevant for jsonb, array, and range columns. Match only rows where every element appearing in column is contained by value.

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • column: The column to apply the filter.
  • values: It can be a single value (string), a list of values to filter or a map (aka json)

Examples

iex> PostgREST.contained_by(builder, "tags", ["urgent", "new", "old"])

See also

contains(b, column, value)

Only relevant for jsonb, array, and range columns. Match only rows where column contains every element appearing in value.

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • column: The column to apply the filter.
  • values: It can be a single value (string), a list of values to filter or a map (aka json)

Examples

iex> PostgREST.contains(builder, "tags", ["urgent", "new"])

See also

eq(f, column, value)

Match only rows where column is equal to value.

To check if the value of column is NULL, you should use .is() instead.

Parameters

Examples

iex> PostgREST.eq(builder, "id", 123)

See also

filter(b, column, op, value)

Match only rows which satisfy the filter. This is an escape hatch - you hould use the specific filter methods wherever possible.

Unlike most filters, opearator and value are used as-is and need to follow PostgREST syntax. You also need to make sure they are properly sanitized.

Parameters

  • column - The column to filter on
  • operator - The operator to filter with, following PostgREST syntax
  • value - The value to filter with, following PostgREST syntax, must implement the String.Chars protocol

Examples

iex> PostgREST.filter(builder, "id", "not", 12)

gt(f, column, value)

Adds a 'greater than' filter to the query, specifying that the column's value must be greater than the specified value.

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • column: The column to apply the filter.
  • value: The value that the column must be greater than, must implement the String.Chars protocol

Examples

iex> PostgREST.gt(builder, "age", 21)

See also

gte(f, column, value)

Adds a 'greater than or equal to' filter to the query, specifying that the column's value must be greater than or equal to the specified value.

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • column: The column to apply the filter.
  • value: The value that the column must be greater than or equal to, must implement the String.Chars protocol

Examples

iex> PostgREST.gte(builder, "age", 21)

See also

ilike(f, column, value)

Adds an 'ilike' filter to the query, allowing for case-insensitive pattern matching (SQL ILIKE).

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • column: The column to apply the filter.
  • value: The pattern to match against the column's value, ignoring case, must implement the String.Chars protocol

Examples

iex> PostgREST.ilike(builder, "name", "%john%")

See also

ilike_all_of(f, column, values)

Match only rows where column matches all of patterns case-insensitively.

Params

  • column: the column to apply the filter
  • values: a list of patterns of filters (needs to implement the String.Chars protocol)

Examples

iex> PostgREST.ilike_all_of(builder, "name", ["jhon", "maria", "joão"])

ilike_any_of(f, column, values)

Match only rows where column matches any of patterns case-insensitively.

Params

  • column: the column to apply the filter
  • values: a list of patterns of filters (needs to implement the String.Chars protocol)

Examples

iex> PostgREST.ilike_any_of(builder, "name", ["jhon", "maria", "joão"])

is(f, column, value)

Match only rows where column IS value.

For non-boolean columns, this is only relevant for checking if the value of column is NULL by setting value to nil.

For boolean columns, you can also set value to true or false and it will behave the same way as .eq().

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • column: The column to apply the filter.
  • value: The value to check the column against (typically nil or a boolean).

Examples

iex> PostgREST.is(builder, "name", nil)

See also

is_filter_op(op)

(macro)

Guard to validates if the filter operator passed to __MODULE__.filter/3 is a valid operator.

like(f, column, value)

Adds a 'like' filter to the query, allowing for simple pattern matching (SQL LIKE).

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • column: The column to apply the filter.
  • value: The pattern to match against the column's value, must implement the String.Chars protocol

Examples

iex> PostgREST.like(builder, "name", "%John%")

See also

like_all_of(b, column, values)

Match only rows where column matches all of patterns case-sensitively.

Params

  • column: the column to apply the filter
  • values: a list of patterns of filters (needs to implement the String.Chars protocol)

Examples

iex> PostgREST.like_all_of(builder, "name", ["jhon", "maria", "joão"])

like_any_of(b, column, values)

Match only rows where column matches any of patterns case-sensitively.

Params

  • column: the column to apply the filter
  • values: a list of patterns of filters (needs to implement the String.Chars protocol)

Examples

iex> PostgREST.like_any_of(builder, "name", ["jhon", "maria", "joão"])

lt(f, column, value)

Adds a 'less than' filter to the query, specifying that the column's value must be less than the specified value.

Parameters

Examples

iex> PostgREST.lt(builder, "age", 65)

See also

lte(f, column, value)

Adds a 'less than or equal to' filter to the query, specifying that the column's value must be less than or equal to the specified value.

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • column: The column to apply the filter.
  • value: The value that the column must be less than or equal to, must implement the String.Chars protocol

Examples

iex> PostgREST.lte(builder, "age", 65)

See also

match(b, query)

Match only rows where each column in query keys is equal to its associated value. Shorthand for multiple .eq()s.

Parameters

  • query - The object to filter with, with column names as keys mapped to their filter values, and all values must implement the String.Chars protocol

Examples

iex> PostgREST.match(builder, %{"col1" => true, "col2" => false})

See also

negate(b, column, op, value)

Applies a "NOT" condition to the query, negating a specified condition.

Unlike most filters, opearator and value are used as-is and need to follow PostgREST syntax. You also need to make sure they are properly sanitized.

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • column: The column to apply the negation.
  • op: The operator used in the condition (e.g., "eq", "gt").
  • value: The value to compare against, must implement the String.Chars protocol

Examples

iex> PostgREST.not(builder, "status", "eq", "active")

See also

neq(f, column, value)

Match only rows where column is not equal to value.

Parameters

Examples

iex> PostgREST.neq(builder, "status", "inactive")

See also

overlaps(b, column, value)

Only relevant for array and range columns. Match only rows where column and value have an element in common.

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • column: The column to apply the filter.
  • values: The array of values that must overlap with the column's value, all elements must implement the String.Chars protocol

Examples

iex> PostgREST.overlaps(builder, "tags", ["urgent", "old"])

See also

process_condition(arg)

range_adjacent(f, column, value)

Only relevant for range columns. Match only rows where column is mutually exclusive to range and there can be no element between the two ranges.

Parameters

Examples

iex> PostgREST.range_adjacent(builder, "scheduled_time", "2021-01-01T10:00:00Z/2021-01-01T12:00:00Z")

See also

range_gt(f, column, value)

Only relevant for range columns. Match only rows where every element in column is greater than any element in range.

Parameters

  • builder: The Request instance.
  • column: The column to apply the filter.
  • value: The lower bound value of the range, must implement the String.Chars protocol

Examples

iex> PostgREST.range_gt(builder, "age", 20)

See also

range_gte(f, column, value)

Only relevant for range columns. Match only rows where every element in column is either contained in range or greater than any element in range.

Parameters

  • builder: The Request instance.
  • column: The column to apply the filter.
  • value: The starting value of the range, must implement the String.Chars protocol

Examples

iex> PostgREST.range_gte(builder, "age", 18)

See also

range_lt(f, column, value)

Only relevant for range columns. Match only rows where every element in column is less than any element in range.

Parameters

Examples

iex> PostgREST.range_lt(builder, "age", 30)

See also

range_lte(f, column, value)

Only relevant for range columns. Match only rows where every element in column is either contained in range or less than any element in range.

Parameters

  • builder: The Request instance.
  • column: The column to apply the filter.
  • value: The ending value of the range, must implement the String.Chars protocol

Examples

iex> PostgREST.range_lte(builder, "age", 65)

See also

text_search(f, column, query, opts \\ [])

Only relevant for text and tsvector columns. Match only rows where column matches the query string in query.

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • column: The column to search.
  • query: The text query for the search.
  • opts: Options for the search, such as type of search (:plain, :phrase, or :websearch) and configuration.

Examples

iex> PostgREST.text_search(builder, "description", "elixir supabase", type: :plain)

See also

within(f, column, values)

Filters the query by checking if the column's value is within an array of specified values.

Parameters

  • builder: The Supabase.Fetcher.Request instance.
  • column: The column to filter.
  • values: A list of acceptable values for the column, all elements must implement the String.Chars protocol

Examples

iex> PostgREST.in(builder, "status", ["active", "pending", "closed"])

See also