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: TablesMapTables and views by qualified identifier.
relationships: RelationshipsMapRelationships between tables.
routines: RoutineMapStored functions/procedures.
timezones: HashSet<String>Valid timezone names.
pg_version: i32PostgreSQL version.
media_handlers: MediaHandlerMapUser-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
impl SchemaCache
Sourcepub async fn load(pool: &PgPool, schemas: &[String]) -> Result<Self>
pub async fn load(pool: &PgPool, schemas: &[String]) -> Result<Self>
Load schema cache from the database.
Sourcepub async fn load_with_search_path(
pool: &PgPool,
schemas: &[String],
extra_search_path: &[String],
) -> Result<Self>
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.
Sourcepub async fn load_from<D: Db>(db: &D, schemas: &[String]) -> Result<Self>
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.
Sourcepub async fn load_with_search_path_from<D: Db>(
db: &D,
schemas: &[String],
extra_search_path: &[String],
) -> Result<Self>
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.
Sourcepub fn get_table(&self, qi: &QualifiedIdentifier) -> Option<&Table>
pub fn get_table(&self, qi: &QualifiedIdentifier) -> Option<&Table>
Get a table by qualified identifier.
Sourcepub fn require_table(&self, qi: &QualifiedIdentifier) -> Result<&Table>
pub fn require_table(&self, qi: &QualifiedIdentifier) -> Result<&Table>
Get a table, returning an error if not found.
Sourcepub fn get_relationships(
&self,
qi: &QualifiedIdentifier,
schema: &str,
) -> Option<&Vec<Relationship>>
pub fn get_relationships( &self, qi: &QualifiedIdentifier, schema: &str, ) -> Option<&Vec<Relationship>>
Get relationships for a table.
Sourcepub fn get_routines(&self, qi: &QualifiedIdentifier) -> Option<&Vec<Routine>>
pub fn get_routines(&self, qi: &QualifiedIdentifier) -> Option<&Vec<Routine>>
Get a routine by qualified identifier.
Sourcepub fn is_valid_timezone(&self, tz: &str) -> bool
pub fn is_valid_timezone(&self, tz: &str) -> bool
Check if a timezone is valid.
Sourcepub fn routine_returned_table(&self, qi: &QualifiedIdentifier) -> Option<&Table>
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.
Sourcepub fn returned_table(&self, routine: &Routine) -> Option<&Table>
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.
Sourcepub fn representation(&self, pg_type: &str, target: &str) -> Option<&str>
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.
Sourcepub fn media_handler<'a>(
&'a self,
schema: &str,
media_type: &'a str,
table: &QualifiedIdentifier,
default_select: bool,
) -> Option<(&'a MediaHandler, &'a str)>
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.
Sourcepub fn relationship_not_found(
&self,
from: &QualifiedIdentifier,
to_name: &str,
hint: Option<&str>,
schema: &str,
) -> Error
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.
Sourcepub fn similar_routine(&self, qi: &QualifiedIdentifier) -> Option<String>
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.
Sourcepub fn find_relationship(
&self,
from: &QualifiedIdentifier,
to_name: &str,
hint: Option<&str>,
schema: &str,
) -> Result<Option<&Relationship>>
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
impl Clone for SchemaCache
Source§impl Debug for SchemaCache
impl Debug for SchemaCache
Source§impl<'de> Deserialize<'de> for SchemaCache
impl<'de> Deserialize<'de> for SchemaCache
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for SchemaCache
impl RefUnwindSafe for SchemaCache
impl Send for SchemaCache
impl Sync for SchemaCache
impl Unpin for SchemaCache
impl UnsafeUnpin for SchemaCache
impl UnwindSafe for SchemaCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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