pub struct Builder<'a> { /* private fields */ }
Expand description

QueryBuilder struct

Implementations

Creates a new Builder with the specified schema.

Authenticates the request with JWT.

Example
use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
client
    .from("table")
    .auth("supers.ecretjw.ttoken");

Performs horizontal filtering with SELECT.

Note

columns is whitespace-sensitive, so you need to omit them unless your column name contains whitespaces.

Example

Simple example:

use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
let resp = client
    .from("table")
    .select("*")
    .execute()
    .await?;

Renaming columns:

let resp = client
    .from("users")
    .select("name:very_very_long_column_name")
    .execute()
    .await?;

Casting columns:

let resp = client
    .from("users")
    .select("age::text")
    .execute()
    .await?;

SELECTing JSON fields:

let resp = client
    .from("users")
    .select("id,json_data->phones->0->>number")
    .execute()
    .await?;

Embedded filters (assume there is a foreign key constraint between tables users and tweets):

let resp = client
    .from("users")
    .select("*,tweets(*)")
    .execute()
    .await?;

Orders the result with the specified columns.

Example
use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
client
    .from("users")
    .select("*")
    .order("username.desc.nullsfirst,age_range");

Limits the result with the specified count.

Example
use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
client
    .from("users")
    .select("*")
    .limit(20);

Limits the result to rows within the specified range, inclusive.

Example

This retrieves the 2nd to 5th entries in the result:

use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
client
    .from("users")
    .select("*")
    .range(1, 4);

Retrieves the (accurate) total size of the result.

Example
use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
client
    .from("users")
    .select("*")
    .exact_count();

Estimates the total size of the result using PostgreSQL statistics. This is faster than using exact_count().

Example
use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
client
    .from("users")
    .select("*")
    .planned_count();

Retrieves the total size of the result using some heuristics: exact_count for smaller sizes, planned_count for larger sizes.

Example
use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
client
    .from("users")
    .select("*")
    .estimated_count();

Retrieves only one row from the result.

Example
use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
client
    .from("users")
    .select("*")
    .single();

Performs an INSERT of the body (in JSON) into the table.

Example
use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
client
    .from("users")
    .insert(r#"[{ "username": "soedirgo", "status": "online" },
                { "username": "jose", "status": "offline" }]"#);

Performs an upsert of the body (in JSON) into the table.

Note

This merges duplicates by default. Ignoring duplicates is possible via PostgREST, but is currently unsupported.

Example
use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
client
    .from("users")
    .upsert(r#"[{ "username": "soedirgo", "status": "online" },
                { "username": "jose", "status": "offline" }]"#);

Resolve upsert conflicts on unique columns other than the primary key.

Note

This informs PostgREST to resolve upsert conflicts through an alternative, unique index other than the primary key of the table. See the related PostgREST documentation.

Example
use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
// Suppose `users` are keyed an SERIAL primary key,
// but have a unique index on `username`.
client
    .from("users")
    .upsert(r#"[{ "username": "soedirgo", "status": "online" },
                { "username": "jose", "status": "offline" }]"#)
    .on_conflict("username");

Performs an UPDATE using the body (in JSON) on the table.

Example
use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
client
    .from("users")
    .eq("username", "soedirgo")
    .update(r#"{ "status": "offline" }"#);

Performs a DELETE on the table.

Example
use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
client
    .from("users")
    .eq("username", "soedirgo")
    .delete();

Performs a stored procedure call. This should only be used through the rpc() method in Postgrest.

Executes the PostgREST request.

Finds all rows which doesn’t satisfy the filter.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .not("eq", "name", "New Zealand")
    .select("*")
    .execute()
    .await?;

Finds all rows satisfying all of the filters.

Note

If your column/filter contains PostgREST’s reserved characters, you need to surround them with double quotes (not percent encoded). See here for details.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .and("name.eq.New Zealand,or(id.gte.1,capital.is.null)")
    .select("*")
    .execute()
    .await?;

Finds all rows satisfying at least one of the filters.

Note

If your column/filter contains PostgREST’s reserved characters, you need to surround them with double quotes (not percent encoded). See here for details.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .or("name.eq.New Zealand,or(id.gte.1,capital.is.null)")
    .select("*")
    .execute()
    .await?;

Finds all rows whose value on the stated column exactly matches the specified filter.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .eq("name", "New Zealand")
    .select("*")
    .execute()
    .await?;

Finds all rows whose value on the stated column doesn’t match the specified filter.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .neq("name", "China")
    .select("*")
    .execute()
    .await?;

Finds all rows whose value on the stated column is greater than the specified filter.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .gt("id", "20")
    .select("*")
    .execute()
    .await?;

Finds all rows whose value on the stated column is greater than or equal to the specified filter.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .gte("id", "20")
    .select("*")
    .execute()
    .await?;

Finds all rows whose value on the stated column is less than the specified filter.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .lt("id", "20")
    .select("*")
    .execute()
    .await?;

Finds all rows whose value on the stated column is less than or equal to the specified filter.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .lte("id", "20")
    .select("*")
    .execute()
    .await?;

Finds all rows whose value in the stated column matches the supplied pattern (case sensitive).

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .like("name", "%United%")
    .select("*")
    .execute()
    .await?;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .like("name", "%United States%")
    .select("*")
    .execute()
    .await?;

Finds all rows whose value in the stated column matches the supplied pattern (case insensitive).

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .ilike("name", "%United%")
    .select("*")
    .execute()
    .await?;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .ilike("name", "%united states%")
    .select("*")
    .execute()
    .await?;

A check for exact equality (null, true, false), finds all rows whose value on the stated column exactly match the specified filter.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .is("name", "null")
    .select("*")
    .execute()
    .await?;

Finds all rows whose value on the stated column is found on the specified values.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .in_("name", vec!["China", "France"])
    .select("*")
    .execute()
    .await?;

let resp = Postgrest::new("http://localhost:3000")
    .from("countries")
    .in_("capitals", vec!["Beijing,China", "Paris,France"])
    .select("*")
    .execute()
    .await?;

let resp = Postgrest::new("http://localhost:3000")
    .from("recipes")
    .in_("food_supplies", vec!["carrot (big)", "carrot (small)"])
    .select("*")
    .execute()
    .await?;

Finds all rows whose json, array, or range value on the stated column contains the values specified in filter.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("users")
    .cs("age_range", "(10,20)")
    .select("*")
    .execute()
    .await?;

Finds all rows whose json, array, or range value on the stated column is contained by the specified filter.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("users")
    .cd("age_range", "(10,20)")
    .select("*")
    .execute()
    .await?;

Finds all rows whose range value on the stated column is strictly to the left of the specified range.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("users")
    .sl("age_range", (10, 20))
    .select("*")
    .execute()
    .await?;

Finds all rows whose range value on the stated column is strictly to the right of the specified range.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("table")
    .sr("age_range", (10, 20))
    .select("*")
    .execute()
    .await?;

Finds all rows whose range value on the stated column does not extend to the left of the specified range.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("table")
    .nxl("age_range", (10, 20))
    .select("*")
    .execute()
    .await?;

Finds all rows whose range value on the stated column does not extend to the right of the specified range.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("table")
    .nxr("age_range", (10, 20))
    .select("*")
    .execute()
    .await?;

Finds all rows whose range value on the stated column is adjacent to the specified range.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("table")
    .adj("age_range", (10, 20))
    .select("*")
    .execute()
    .await?;

Finds all rows whose array or range value on the stated column overlaps with the specified filter.

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("table")
    .ov("age_range", "(10,20)")
    .select("*")
    .execute()
    .await?;

Finds all rows whose tsvector value on the stated column matches to_tsquery(tsquery).

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("table")
    .fts("phrase", "The Fat Cats", Some("english"))
    .select("*")
    .execute()
    .await?;

Finds all rows whose tsvector value on the stated column matches plainto_tsquery(tsquery).

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("table")
    .plfts("phrase", "The Fat Cats", None)
    .select("*")
    .execute()
    .await?;

Finds all rows whose tsvector value on the stated column matches phraseto_tsquery(tsquery).

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("table")
    .phfts("phrase", "The Fat Cats", Some("english"))
    .select("*")
    .execute()
    .await?;

Finds all rows whose tsvector value on the stated column matches websearch_to_tsquery(tsquery).

Example
use postgrest::Postgrest;

let resp = Postgrest::new("http://localhost:3000")
    .from("table")
    .wfts("phrase", "The Fat Cats", None)
    .select("*")
    .execute()
    .await?;

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more