Skip to main content

SchemaCache

Struct SchemaCache 

Source
pub struct SchemaCache {
    pub tables: TablesMap,
    pub relationships: RelationshipsMap,
    pub routines: RoutineMap,
    pub timezones: HashSet<String>,
    pub pg_version: i32,
    pub media_handlers: MediaHandlerMap,
    pub representations: HashMap<(String, String), String>,
}
Expand description

Cached PostgreSQL schema metadata.

Serializable, so a host that cannot afford to load it on every cold start – a Cloudflare Worker – can keep a copy elsewhere. The maps keyed by more than a string are written as lists of [key, value] pairs, since a JSON object can only be keyed by a string.

Fields§

§tables: TablesMap

Tables and views by qualified identifier.

§relationships: RelationshipsMap

Relationships between tables.

§routines: RoutineMap

Stored functions/procedures.

§timezones: HashSet<String>

Valid timezone names.

§pg_version: i32

PostgreSQL version.

§media_handlers: MediaHandlerMap

User-defined renderers for media types, by (schema, media type).

§representations: HashMap<(String, String), String>

Casts between a domain and json/text, by (source, target) type.

A schema uses these to decide how a value of one of its domains is written on the wire and how one written by a client is read back.

Implementations§

Source§

impl SchemaCache

Source

pub async fn load(pool: &PgPool, schemas: &[String]) -> Result<Self>

Load schema cache from the database.

Source

pub async fn load_with_search_path( pool: &PgPool, schemas: &[String], extra_search_path: &[String], ) -> Result<Self>

Load the cache, resolving functions along extra_search_path as well. See Self::load_with_search_path_from.

Source

pub async fn load_from<D: Db>(db: &D, schemas: &[String]) -> Result<Self>

Load schema cache through any Db – a sqlx pool, or the socket driver a Cloudflare Worker uses.

Source

pub async fn load_with_search_path_from<D: Db>( db: &D, schemas: &[String], extra_search_path: &[String], ) -> Result<Self>

Load the cache, resolving functions along extra_search_path as well.

A computed column’s function is reached the way the database reaches it, which is along the search path – so it may live in a schema the API does not expose. The table it reads must still be exposed.

Every catalog query runs on one transaction, through the same interface a request uses, so any driver that can serve requests can load the cache.

Source

pub fn get_table(&self, qi: &QualifiedIdentifier) -> Option<&Table>

Get a table by qualified identifier.

Source

pub fn require_table(&self, qi: &QualifiedIdentifier) -> Result<&Table>

Get a table, returning an error if not found.

Source

pub fn get_relationships( &self, qi: &QualifiedIdentifier, schema: &str, ) -> Option<&Vec<Relationship>>

Get relationships for a table.

Source

pub fn get_routines(&self, qi: &QualifiedIdentifier) -> Option<&Vec<Routine>>

Get a routine by qualified identifier.

Source

pub fn is_valid_timezone(&self, tz: &str) -> bool

Check if a timezone is valid.

Source

pub fn summary(&self) -> String

Get a summary of the cached schema.

Source

pub fn routine_returned_table(&self, qi: &QualifiedIdentifier) -> Option<&Table>

The table whose rows a function returns, if it returns rows of one.

This is what makes a function behave like a table for everything after the call: its result can be selected from, filtered, ordered, paged and embedded on, all of which need a table to resolve columns against.

format_type may or may not qualify the name, depending on the search path it was rendered against, so both spellings are tried – and an unqualified one is looked for in the function’s own schema.

Source

pub fn returned_table(&self, routine: &Routine) -> Option<&Table>

The same, for a routine already chosen.

A name may carry several signatures returning different things, so which overload was selected decides which table the result is shaped by – and only the caller knows that.

Source

pub fn representation(&self, pg_type: &str, target: &str) -> Option<&str>

The function that renders a value of pg_type as target, if the schema declared one.

Source

pub fn media_handler<'a>( &'a self, schema: &str, media_type: &'a str, table: &QualifiedIdentifier, default_select: bool, ) -> Option<(&'a MediaHandler, &'a str)>

Find a user-defined renderer for a media type on a given table.

A handler declared for the table itself wins over one taking anyelement, which is the schema-wide fallback. A schema may also declare a handler for */*, which renders whatever was asked for. What comes back is the handler and the media type the response should be labelled with – not always the one requested, since */* is not a type a response can claim to be. PostgREST resolves it to application/octet-stream, which is what a body of unnamed bytes is, and this follows it. A handler declared for one table renders that table as it is, so it only applies to a request that asked for the table as it is: ?select=id is a different shape from the one the handler was written against, and running it over that shape produces either nonsense or an error. PostgREST guards both table-specific lookups on the selection being the default one, and default_select is that guard.

Source

pub fn relationship_not_found( &self, from: &QualifiedIdentifier, to_name: &str, hint: Option<&str>, schema: &str, ) -> Error

The error to report when no relationship connects two resources.

Built here rather than at the call site because the suggestion needs the relationships the origin actually has, which is what the client most likely meant to name.

Source

pub fn similar_routine(&self, qi: &QualifiedIdentifier) -> Option<String>

The exposed function whose name is closest to one that does not exist.

Only when the name itself is wrong: an existing name whose arguments do not match is a different mistake, and the overloads answer it better.

Source

pub fn find_relationship( &self, from: &QualifiedIdentifier, to_name: &str, hint: Option<&str>, schema: &str, ) -> Result<Option<&Relationship>>

Find a relationship between two tables by name.

hint is the !hint of person_detail!message_sender_fkey(name), which names the relationship when the two resources are connected by more than one.

Returns Ok(None) when nothing matches, and an error when several do and no hint told them apart – picking one arbitrarily would answer a question the client did not ask.

Trait Implementations§

Source§

impl Clone for SchemaCache

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SchemaCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for SchemaCache

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for SchemaCache

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

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

fn in_current_span(self) -> Instrumented<Self> ⓘ

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

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T> MaybeSync for T
where T: Sync + ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

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

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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