Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

PostgrestQueryBuilder

import { PostgrestQueryBuilder } from "https://esm.sh/@supabase/supabase-js@2.105.4/dist/index.d.mts";
class PostgrestQueryBuilder<ClientOptions extends ClientServerOptions, Schema extends GenericSchema, Relation$1 extends GenericTable | GenericView, RelationName = unknown, Relationships = (Relation$1 extends {
Relationships: infer R;
}
? R : unknown
)
>
{
constructor(url: URL, { headers, schema, fetch, urlLengthLimit, retry }: {
headers?: HeadersInit;
schema?: string;
fetch?: Fetch;
urlLengthLimit?: number;
retry?: boolean;
}
);
private cloneRequestState;
fetch?: Fetch;
headers: Headers;
retry?: boolean;
schema?: string;
signal?: AbortSignal;
url: URL;
urlLengthLimit: number;
 
delete({ count }?: {
count?:
| "exact"
| "planned"
| "estimated"
| (string & {});
}
): PostgrestFilterBuilder<ClientOptions, Schema, Relation$1["Row"], null, RelationName, Relationships, "DELETE">;
insert<Row extends (Relation$1 extends {
Insert: unknown;
}
? Relation$1["Insert"] : never
)
>
(values: RejectExcessProperties<Relation$1 extends {
Insert: unknown;
}
? Relation$1["Insert"] : never
, Row>
| RejectExcessProperties<Relation$1 extends {
Insert: unknown;
}
? Relation$1["Insert"] : never
, Row>
[]
, { count, defaultToNull }?: {
count?:
| "exact"
| "planned"
| "estimated"
| (string & {});
defaultToNull?: boolean;
}
): PostgrestFilterBuilder<ClientOptions, Schema, Relation$1["Row"], null, RelationName, Relationships, "POST">;
select<Query extends string = "*", ResultOne = GetResult<Schema, Relation$1["Row"], RelationName, Relationships, Query, ClientOptions>>(columns?: Query, options?: {
head?: boolean;
count?:
| "exact"
| "planned"
| "estimated"
| (string & {});
}
): PostgrestFilterBuilder<ClientOptions, Schema, Relation$1["Row"], ResultOne[], RelationName, Relationships, "GET">;
update<Row extends (Relation$1 extends {
Update: unknown;
}
? Relation$1["Update"] : never
)
>
(values: RejectExcessProperties<Relation$1 extends {
Update: unknown;
}
? Relation$1["Update"] : never
, Row>
, { count }?: {
count?:
| "exact"
| "planned"
| "estimated"
| (string & {});
}
): PostgrestFilterBuilder<ClientOptions, Schema, Relation$1["Row"], null, RelationName, Relationships, "PATCH">;
upsert<Row extends (Relation$1 extends {
Insert: unknown;
}
? Relation$1["Insert"] : never
)
>
(values: RejectExcessProperties<Relation$1 extends {
Insert: unknown;
}
? Relation$1["Insert"] : never
, Row>
| RejectExcessProperties<Relation$1 extends {
Insert: unknown;
}
? Relation$1["Insert"] : never
, Row>
[]
, { onConflict, ignoreDuplicates, count, defaultToNull }?: {
onConflict?: string;
ignoreDuplicates?: boolean;
count?:
| "exact"
| "planned"
| "estimated"
| (string & {});
defaultToNull?: boolean;
}
): PostgrestFilterBuilder<ClientOptions, Schema, Relation$1["Row"], null, RelationName, Relationships, "POST">;
}

§Type Parameters

§
ClientOptions extends ClientServerOptions
[src]
§
Schema extends GenericSchema
[src]
§
Relation$1 extends GenericTable | GenericView
[src]
§
RelationName = unknown
[src]
§
Relationships = (Relation$1 extends {
Relationships: infer R;
}
? R : unknown
)
[src]

§Constructors

§
new PostgrestQueryBuilder(url: URL, { headers, schema, fetch, urlLengthLimit, retry }: {
headers?: HeadersInit;
schema?: string;
fetch?: Fetch;
urlLengthLimit?: number;
retry?: boolean;
}
)
[src]

Creates a query builder scoped to a Postgres table or view.

@param url
  • The URL for the query
@param options
  • Named parameters
@param options.headers
  • Custom headers
@param options.schema
  • Postgres schema to use
@param options.fetch
  • Custom fetch implementation
@param options.urlLengthLimit
  • Maximum URL length before warning
@param options.retry
  • Enable automatic retries for transient errors (default: true)
@example

Using supabase-js (recommended)

import { createClient } from '@supabase/supabase-js'

const supabase = createClient('https://xyzcompany.supabase.co', 'your-publishable-key')
const { data, error } = await supabase.from('users').select('*')
@example

Standalone import for bundle-sensitive environments

import { PostgrestQueryBuilder } from '@supabase/postgrest-js'

const query = new PostgrestQueryBuilder(
  new URL('https://xyzcompany.supabase.co/rest/v1/users'),
  { headers: { apikey: 'your-publishable-key' }, retry: true }
)

§Properties

§
cloneRequestState
[src]

Clone URL and headers to prevent shared state between operations.

§
headers: Headers
[src]
§
retry: boolean
[src]

Enable or disable automatic retries for transient errors. When enabled, idempotent requests (GET/HEAD/OPTIONS) that fail with network errors or HTTP 503/520 responses are automatically retried with exponential backoff (1s, 2s, 4s, up to 3 attempts). Defaults to true when not specified.

§
schema: string
[src]
§
signal: AbortSignal
[src]
§
url: URL
[src]
§
urlLengthLimit: number
[src]

§Methods

§
delete({ count }?: {
count?:
| "exact"
| "planned"
| "estimated"
| (string & {});
}
): PostgrestFilterBuilder<ClientOptions, Schema, Relation$1["Row"], null, RelationName, Relationships, "DELETE">
[src]

Perform a DELETE on the table or view.

By default, deleted rows are not returned. To return it, chain the call with .select() after filters.

@param options
  • Named parameters
@param options.count
  • Count algorithm to use to count deleted rows.

"exact": Exact but slow count algorithm. Performs a COUNT(*) under the hood.

"planned": Approximated but fast count algorithm. Uses the Postgres statistics under the hood.

"estimated": Uses exact count for low numbers and planned count for high numbers.

@example

Delete a single record

const response = await supabase
  .from('countries')
  .delete()
  .eq('id', 1)
@example
@example
@example

Delete a record and return it

const { data, error } = await supabase
  .from('countries')
  .delete()
  .eq('id', 1)
  .select()
@example
@example
@example

Delete multiple records

const response = await supabase
  .from('countries')
  .delete()
  .in('id', [1, 2, 3])
@example
@example
§
insert<Row extends (Relation$1 extends {
Insert: unknown;
}
? Relation$1["Insert"] : never
)
>
(values: RejectExcessProperties<Relation$1 extends {
Insert: unknown;
}
? Relation$1["Insert"] : never
, Row>
| RejectExcessProperties<Relation$1 extends {
Insert: unknown;
}
? Relation$1["Insert"] : never
, Row>
[]
, { count, defaultToNull }?: {
count?:
| "exact"
| "planned"
| "estimated"
| (string & {});
defaultToNull?: boolean;
}
): PostgrestFilterBuilder<ClientOptions, Schema, Relation$1["Row"], null, RelationName, Relationships, "POST">
[src]

Perform an INSERT into the table or view.

By default, inserted rows are not returned. To return it, chain the call with .select().

@param values
  • The values to insert. Pass an object to insert a single row or an array to insert multiple rows.
@param options
  • Named parameters
@param options.count
  • Count algorithm to use to count inserted rows.

"exact": Exact but slow count algorithm. Performs a COUNT(*) under the hood.

"planned": Approximated but fast count algorithm. Uses the Postgres statistics under the hood.

"estimated": Uses exact count for low numbers and planned count for high numbers.

@param options.defaultToNull
  • Make missing fields default to null. Otherwise, use the default value for the column. Only applies for bulk inserts.
@example

Create a record

const { error } = await supabase
  .from('countries')
  .insert({ id: 1, name: 'Mordor' })
@example
@example
@example

Create a record and return it

const { data, error } = await supabase
  .from('countries')
  .insert({ id: 1, name: 'Mordor' })
  .select()
@example
@example
@example
@example

Bulk create

const { error } = await supabase
  .from('countries')
  .insert([
    { id: 1, name: 'Mordor' },
    { id: 1, name: 'The Shire' },
  ])
@example
@example
§
select<Query extends string = "*", ResultOne = GetResult<Schema, Relation$1["Row"], RelationName, Relationships, Query, ClientOptions>>(columns?: Query, options?: {
head?: boolean;
count?:
| "exact"
| "planned"
| "estimated"
| (string & {});
}
): PostgrestFilterBuilder<ClientOptions, Schema, Relation$1["Row"], ResultOne[], RelationName, Relationships, "GET">
[src]

Perform a SELECT query on the table or view.

@param columns
  • The columns to retrieve, separated by commas. Columns can be renamed when returned with customName:columnName
@param options
  • Named parameters
@param options.head
  • When set to true, data will not be returned. Useful if you only need the count.
@param options.count
  • Count algorithm to use to count rows in the table or view.

"exact": Exact but slow count algorithm. Performs a COUNT(*) under the hood.

"planned": Approximated but fast count algorithm. Uses the Postgres statistics under the hood.

"estimated": Uses exact count for low numbers and planned count for high numbers.

@example

Getting your data

const { data, error } = await supabase
  .from('characters')
  .select()
@example
@example
@example

Selecting specific columns

const { data, error } = await supabase
  .from('characters')
  .select('name')
@example
@example
@example
@example

Query referenced tables

const { data, error } = await supabase
  .from('orchestral_sections')
  .select(`
    name,
    instruments (
      name
    )
  `)
@example
@example
@example
@example

Query referenced tables with spaces in their names

const { data, error } = await supabase
  .from('orchestral sections')
  .select(`
    name,
    "musical instruments" (
      name
    )
  `)
@example
@example
@example
@example

Query referenced tables through a join table

const { data, error } = await supabase
  .from('users')
  .select(`
    name,
    teams (
      name
    )
  `)

@example
@example
@example
@example

Query the same referenced table multiple times

const { data, error } = await supabase
  .from('messages')
  .select(`
    content,
    from:sender_id(name),
    to:receiver_id(name)
  `)

// To infer types, use the name of the table (in this case `users`) and
// the name of the foreign key constraint.
const { data, error } = await supabase
  .from('messages')
  .select(`
    content,
    from:users!messages_sender_id_fkey(name),
    to:users!messages_receiver_id_fkey(name)
  `)
@example
@example
@example
@example

Query nested foreign tables through a join table

  const { data, error } = await supabase
    .from('games')
    .select(`
      game_id:id,
      away_team:teams!games_away_team_fkey (
        users (
          id,
          name
        )
      )
    `)

@example
@example
@example
@example

Filtering through referenced tables

const { data, error } = await supabase
  .from('instruments')
  .select('name, orchestral_sections(*)')
  .eq('orchestral_sections.name', 'percussion')
@example
@example
@example
@example

Querying referenced table with count

const { data, error } = await supabase
  .from('orchestral_sections')
  .select(`*, instruments(count)`)
@example
@example
@example
@example

Querying with count option

const { count, error } = await supabase
  .from('characters')
  .select('*', { count: 'exact', head: true })
@example
@example
@example
@example

Querying JSON data

const { data, error } = await supabase
  .from('users')
  .select(`
    id, name,
    address->city
  `)
@example
@example
@example
@example

Querying referenced table with inner join

const { data, error } = await supabase
  .from('instruments')
  .select('name, orchestral_sections!inner(name)')
  .eq('orchestral_sections.name', 'woodwinds')
  .limit(1)
@example
@example
@example
@example

Switching schemas per query

const { data, error } = await supabase
  .schema('myschema')
  .from('mytable')
  .select()
@example
@example
§
update<Row extends (Relation$1 extends {
Update: unknown;
}
? Relation$1["Update"] : never
)
>
(values: RejectExcessProperties<Relation$1 extends {
Update: unknown;
}
? Relation$1["Update"] : never
, Row>
, { count }?: {
count?:
| "exact"
| "planned"
| "estimated"
| (string & {});
}
): PostgrestFilterBuilder<ClientOptions, Schema, Relation$1["Row"], null, RelationName, Relationships, "PATCH">
[src]

Perform an UPDATE on the table or view.

By default, updated rows are not returned. To return it, chain the call with .select() after filters.

@param values
  • The values to update with
@param options
  • Named parameters
@param options.count
  • Count algorithm to use to count updated rows.

"exact": Exact but slow count algorithm. Performs a COUNT(*) under the hood.

"planned": Approximated but fast count algorithm. Uses the Postgres statistics under the hood.

"estimated": Uses exact count for low numbers and planned count for high numbers.

@example

Updating your data

const { error } = await supabase
  .from('instruments')
  .update({ name: 'piano' })
  .eq('id', 1)
@example
@example
@example

Update a record and return it

const { data, error } = await supabase
  .from('instruments')
  .update({ name: 'piano' })
  .eq('id', 1)
  .select()
@example
@example
@example
@example

Updating JSON data

const { data, error } = await supabase
  .from('users')
  .update({
    address: {
      street: 'Melrose Place',
      postcode: 90210
    }
  })
  .eq('address->postcode', 90210)
  .select()
@example
@example
§
upsert<Row extends (Relation$1 extends {
Insert: unknown;
}
? Relation$1["Insert"] : never
)
>
(values: RejectExcessProperties<Relation$1 extends {
Insert: unknown;
}
? Relation$1["Insert"] : never
, Row>
| RejectExcessProperties<Relation$1 extends {
Insert: unknown;
}
? Relation$1["Insert"] : never
, Row>
[]
, { onConflict, ignoreDuplicates, count, defaultToNull }?: {
onConflict?: string;
ignoreDuplicates?: boolean;
count?:
| "exact"
| "planned"
| "estimated"
| (string & {});
defaultToNull?: boolean;
}
): PostgrestFilterBuilder<ClientOptions, Schema, Relation$1["Row"], null, RelationName, Relationships, "POST">
[src]

Perform an UPSERT on the table or view. Depending on the column(s) passed to onConflict, .upsert() allows you to perform the equivalent of .insert() if a row with the corresponding onConflict columns doesn't exist, or if it does exist, perform an alternative action depending on ignoreDuplicates.

By default, upserted rows are not returned. To return it, chain the call with .select().

@param values
  • The values to upsert with. Pass an object to upsert a single row or an array to upsert multiple rows.
@param options
  • Named parameters
@param options.onConflict
  • Comma-separated UNIQUE column(s) to specify how duplicate rows are determined. Two rows are duplicates if all the onConflict columns are equal.
@param options.ignoreDuplicates
  • If true, duplicate rows are ignored. If false, duplicate rows are merged with existing rows.
@param options.count
  • Count algorithm to use to count upserted rows.

"exact": Exact but slow count algorithm. Performs a COUNT(*) under the hood.

"planned": Approximated but fast count algorithm. Uses the Postgres statistics under the hood.

"estimated": Uses exact count for low numbers and planned count for high numbers.

@param options.defaultToNull
  • Make missing fields default to null. Otherwise, use the default value for the column. This only applies when inserting new rows, not when merging with existing rows under ignoreDuplicates: false. This also only applies when doing bulk upserts.
@example

Upsert a single row using a unique key

// Upserting a single row, overwriting based on the 'username' unique column
const { data, error } = await supabase
  .from('users')
  .upsert({ username: 'supabot' }, { onConflict: 'username' })

// Example response:
// {
//   data: [
//     { id: 4, message: 'bar', username: 'supabot' }
//   ],
//   error: null
// }
@example

Upsert with conflict resolution and exact row counting

// Upserting and returning exact count
const { data, error, count } = await supabase
  .from('users')
  .upsert(
    {
      id: 3,
      message: 'foo',
      username: 'supabot'
    },
    {
      onConflict: 'username',
      count: 'exact'
    }
  )

// Example response:
// {
//   data: [
//     {
//       id: 42,
//       handle: "saoirse",
//       display_name: "Saoirse"
//     }
//   ],
//   count: 1,
//   error: null
// }
@example

Upsert your data

const { data, error } = await supabase
  .from('instruments')
  .upsert({ id: 1, name: 'piano' })
  .select()
@example
@example
@example

Bulk Upsert your data

const { data, error } = await supabase
  .from('instruments')
  .upsert([
    { id: 1, name: 'piano' },
    { id: 2, name: 'harp' },
  ])
  .select()
@example
@example
@example
@example

Upserting into tables with constraints

const { data, error } = await supabase
  .from('users')
  .upsert({ id: 42, handle: 'saoirse', display_name: 'Saoirse' })
  .select()
@example
@example