UNPKG

@supabase/postgrest-js

Version:
381 lines 13.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const PostgrestTransformBuilder_1 = __importDefault(require("./PostgrestTransformBuilder")); class PostgrestFilterBuilder extends PostgrestTransformBuilder_1.default { /** * Match only rows where `column` is equal to `value`. * * To check if the value of `column` is NULL, you should use `.is()` instead. * * @param column - The column to filter on * @param value - The value to filter with */ eq(column, value) { this.url.searchParams.append(column, `eq.${value}`); return this; } /** * Match only rows where `column` is not equal to `value`. * * @param column - The column to filter on * @param value - The value to filter with */ neq(column, value) { this.url.searchParams.append(column, `neq.${value}`); return this; } /** * Match only rows where `column` is greater than `value`. * * @param column - The column to filter on * @param value - The value to filter with */ gt(column, value) { this.url.searchParams.append(column, `gt.${value}`); return this; } /** * Match only rows where `column` is greater than or equal to `value`. * * @param column - The column to filter on * @param value - The value to filter with */ gte(column, value) { this.url.searchParams.append(column, `gte.${value}`); return this; } /** * Match only rows where `column` is less than `value`. * * @param column - The column to filter on * @param value - The value to filter with */ lt(column, value) { this.url.searchParams.append(column, `lt.${value}`); return this; } /** * Match only rows where `column` is less than or equal to `value`. * * @param column - The column to filter on * @param value - The value to filter with */ lte(column, value) { this.url.searchParams.append(column, `lte.${value}`); return this; } /** * Match only rows where `column` matches `pattern` case-sensitively. * * @param column - The column to filter on * @param pattern - The pattern to match with */ like(column, pattern) { this.url.searchParams.append(column, `like.${pattern}`); return 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, patterns) { this.url.searchParams.append(column, `like(all).{${patterns.join(',')}}`); return 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, patterns) { this.url.searchParams.append(column, `like(any).{${patterns.join(',')}}`); return this; } /** * Match only rows where `column` matches `pattern` case-insensitively. * * @param column - The column to filter on * @param pattern - The pattern to match with */ ilike(column, pattern) { this.url.searchParams.append(column, `ilike.${pattern}`); return 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, patterns) { this.url.searchParams.append(column, `ilike(all).{${patterns.join(',')}}`); return 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, patterns) { this.url.searchParams.append(column, `ilike(any).{${patterns.join(',')}}`); return this; } /** * 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 `null`. * * For boolean columns, you can also set `value` to `true` or `false` and it * will behave the same way as `.eq()`. * * @param column - The column to filter on * @param value - The value to filter with */ is(column, value) { this.url.searchParams.append(column, `is.${value}`); return this; } /** * Match only rows where `column` is included in the `values` array. * * @param column - The column to filter on * @param values - The values array to filter with */ in(column, values) { const cleanedValues = Array.from(new Set(values)) .map((s) => { // handle postgrest reserved characters // https://postgrest.org/en/v7.0.0/api.html#reserved-characters if (typeof s === 'string' && new RegExp('[,()]').test(s)) return `"${s}"`; else return `${s}`; }) .join(','); this.url.searchParams.append(column, `in.(${cleanedValues})`); return this; } /** * Only relevant for jsonb, array, and range columns. Match only rows where * `column` contains every element appearing in `value`. * * @param column - The jsonb, array, or range column to filter on * @param value - The jsonb, array, or range value to filter with */ contains(column, value) { if (typeof value === 'string') { // range types can be inclusive '[', ']' or exclusive '(', ')' so just // keep it simple and accept a string this.url.searchParams.append(column, `cs.${value}`); } else if (Array.isArray(value)) { // array this.url.searchParams.append(column, `cs.{${value.join(',')}}`); } else { // json this.url.searchParams.append(column, `cs.${JSON.stringify(value)}`); } return this; } /** * Only relevant for jsonb, array, and range columns. Match only rows where * every element appearing in `column` is contained by `value`. * * @param column - The jsonb, array, or range column to filter on * @param value - The jsonb, array, or range value to filter with */ containedBy(column, value) { if (typeof value === 'string') { // range this.url.searchParams.append(column, `cd.${value}`); } else if (Array.isArray(value)) { // array this.url.searchParams.append(column, `cd.{${value.join(',')}}`); } else { // json this.url.searchParams.append(column, `cd.${JSON.stringify(value)}`); } return this; } /** * Only relevant for range columns. Match only rows where every element in * `column` is greater than any element in `range`. * * @param column - The range column to filter on * @param range - The range to filter with */ rangeGt(column, range) { this.url.searchParams.append(column, `sr.${range}`); return this; } /** * 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`. * * @param column - The range column to filter on * @param range - The range to filter with */ rangeGte(column, range) { this.url.searchParams.append(column, `nxl.${range}`); return this; } /** * Only relevant for range columns. Match only rows where every element in * `column` is less than any element in `range`. * * @param column - The range column to filter on * @param range - The range to filter with */ rangeLt(column, range) { this.url.searchParams.append(column, `sl.${range}`); return this; } /** * 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`. * * @param column - The range column to filter on * @param range - The range to filter with */ rangeLte(column, range) { this.url.searchParams.append(column, `nxr.${range}`); return this; } /** * 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. * * @param column - The range column to filter on * @param range - The range to filter with */ rangeAdjacent(column, range) { this.url.searchParams.append(column, `adj.${range}`); return this; } /** * Only relevant for array and range columns. Match only rows where * `column` and `value` have an element in common. * * @param column - The array or range column to filter on * @param value - The array or range value to filter with */ overlaps(column, value) { if (typeof value === 'string') { // range this.url.searchParams.append(column, `ov.${value}`); } else { // array this.url.searchParams.append(column, `ov.{${value.join(',')}}`); } return this; } /** * Only relevant for text and tsvector columns. Match only rows where * `column` matches the query string in `query`. * * @param column - The text or tsvector column to filter on * @param query - The query text to match with * @param options - Named parameters * @param options.config - The text search configuration to use * @param options.type - Change how the `query` text is interpreted */ textSearch(column, query, { config, type } = {}) { let typePart = ''; if (type === 'plain') { typePart = 'pl'; } else if (type === 'phrase') { typePart = 'ph'; } else if (type === 'websearch') { typePart = 'w'; } const configPart = config === undefined ? '' : `(${config})`; this.url.searchParams.append(column, `${typePart}fts${configPart}.${query}`); return this; } /** * Match only rows where each column in `query` keys is equal to its * associated value. Shorthand for multiple `.eq()`s. * * @param query - The object to filter with, with column names as keys mapped * to their filter values */ match(query) { Object.entries(query).forEach(([column, value]) => { this.url.searchParams.append(column, `eq.${value}`); }); return this; } /** * Match only rows which doesn't satisfy the filter. * * Unlike most filters, `opearator` and `value` are used as-is and need to * follow [PostgREST * syntax](https://postgrest.org/en/stable/api.html#operators). You also need * to make sure they are properly sanitized. * * @param column - The column to filter on * @param operator - The operator to be negated to filter with, following * PostgREST syntax * @param value - The value to filter with, following PostgREST syntax */ not(column, operator, value) { this.url.searchParams.append(column, `not.${operator}.${value}`); return this; } /** * Match only rows which satisfy at least one of the filters. * * Unlike most filters, `filters` is used as-is and needs to follow [PostgREST * syntax](https://postgrest.org/en/stable/api.html#operators). You also need * to make sure it's properly sanitized. * * It's currently not possible to do an `.or()` filter across multiple tables. * * @param filters - The filters to use, following PostgREST syntax * @param options - Named parameters * @param options.referencedTable - Set this to filter on referenced tables * instead of the parent table * @param options.foreignTable - Deprecated, use `referencedTable` instead */ or(filters, { foreignTable, referencedTable = foreignTable, } = {}) { const key = referencedTable ? `${referencedTable}.or` : 'or'; this.url.searchParams.append(key, `(${filters})`); return this; } /** * Match only rows which satisfy the filter. This is an escape hatch - you * should use the specific filter methods wherever possible. * * Unlike most filters, `opearator` and `value` are used as-is and need to * follow [PostgREST * syntax](https://postgrest.org/en/stable/api.html#operators). You also need * to make sure they are properly sanitized. * * @param column - The column to filter on * @param operator - The operator to filter with, following PostgREST syntax * @param value - The value to filter with, following PostgREST syntax */ filter(column, operator, value) { this.url.searchParams.append(column, `${operator}.${value}`); return this; } } exports.default = PostgrestFilterBuilder; //# sourceMappingURL=PostgrestFilterBuilder.js.map