Skip to main content

PostgresRepository

Struct PostgresRepository 

Source
pub struct PostgresRepository<T>
where T: for<'a> FromRow<'a, PgRow> + Send + Unpin,
{ /* private fields */ }
Expand description

PostgreSQL repository implementation with JSON-based dynamic queries

Implementations§

Source§

impl<T> PostgresRepository<T>
where T: for<'a> FromRow<'a, PgRow> + Send + Unpin,

Source

pub fn new(pool: Pool<Postgres>, table_name: &str) -> PostgresRepository<T>

Source

pub fn pool(&self) -> &Pool<Postgres>

Source

pub fn table_name(&self) -> &str

Source

pub async fn list_with_filters( &self, pagination: PaginationParams, filters: &HashMap<String, String>, column_types: &HashMap<String, String>, search_fields: &[&str], ) -> Result<PaginatedResult<T>, Error>
where T: Send + Sync,

List entities with pagination and advanced filtering

This method provides comprehensive filtering capabilities similar to Laravel’s Filter Query String.

§Supported Filter Operators
  • field[eq]=value - Equal
  • field[notEq]=value - Not equal
  • field[gt]=value - Greater than
  • field[gte]=value - Greater than or equal
  • field[lt]=value - Less than
  • field[lte]=value - Less than or equal
  • field[like]=value - LIKE (case-sensitive)
  • field[ilike]=value - ILIKE (case-insensitive)
  • field[notlike]=value - NOT LIKE
  • field[contain]=value - Contains (%value%)
  • field[notcontain]=value - Does not contain
  • field[startwith]=value - Starts with (value%)
  • field[endwith]=value - Ends with (%value)
  • field[in]=val1,val2 - IN array
  • field[notin]=val1,val2 - NOT IN array
  • field[between]=val1,val2 - BETWEEN
  • field[notbetween]=val1,val2 - NOT BETWEEN
  • field[isnull] - IS NULL
  • field[isnotnull] - IS NOT NULL
§Special Parameters
  • search=value&searchFields=field1,field2 - Search in multiple fields
  • orderby=field or orderby[field]=asc - Sort results
  • limit=10 - Limit results
  • page=1 - Page number
§Column Type Casting

The column_types HashMap maps field names to their PostgreSQL types for proper casting. For example, {"status": "user_status"} will cast the status parameter to user_status enum type.

§Example
ⓘ
let mut filters = HashMap::new();
filters.insert("username[contain]".to_string(), "john".to_string());
filters.insert("age[gt]".to_string(), "18".to_string());

let mut column_types = HashMap::new();
column_types.insert("status".to_string(), "user_status".to_string());

let result = repo.list_with_filters(
    PaginationParams::new(1, 10),
    &filters,
    &column_types,
    &["username", "email"]  // search fields
).await?;
Source

pub async fn list_with_filters_whitelisted( &self, pagination: PaginationParams, filters: &HashMap<String, String>, column_types: &HashMap<String, String>, search_fields: &[&str], allowed_fields: Option<&HashSet<String>>, ) -> Result<PaginatedResult<T>, Error>
where T: Send + Sync,

List entities with pagination, filtering, and field whitelist enforcement

Similar to list_with_filters but accepts an optional set of allowed field names. When provided, only filter conditions on whitelisted fields are applied; conditions on unknown fields are silently dropped.

This prevents clients from filtering on internal or sensitive columns (e.g., password_hash, internal_notes).

§Arguments
  • pagination - Page and limit parameters
  • filters - HTTP query parameters (e.g., field[operator]=value)
  • column_types - PostgreSQL type mappings for enum casting
  • search_fields - Fields to search when search parameter is present
  • allowed_fields - Optional whitelist of field names; None allows all fields
§Example
ⓘ
let allowed: HashSet<String> = ["username", "email", "status"]
    .iter().map(|s| s.to_string()).collect();

let result = repo.list_with_filters_whitelisted(
    PaginationParams::new(1, 10),
    &filters,
    &column_types,
    &["username", "email"],
    Some(&allowed),
).await?;
Source§

impl<T> PostgresRepository<T>
where T: for<'a> FromRow<'a, PgRow> + Send + Unpin,

Source

pub async fn aggregate_with_filters( &self, spec: &AggregateSpec, filters: &HashMap<String, String>, column_types: &HashMap<String, String>, search_fields: &[&str], ) -> Result<AggregateResult, Error>

Group and reduce rows in one statement, under the same filters, the same soft-delete convention and the same tenancy fence as the list endpoint.

Groups and the overall total come back from a single GROUPING SETS query, which is what lets a caller draw a chart and its headline from one reply, and what keeps the two numbers consistent — a separate total query could observe a different set of rows.

Trait Implementations§

Source§

impl<T> DatabaseOperations<T> for PostgresRepository<T>
where T: for<'a> FromRow<'a, PgRow> + Send + Sync + Unpin + Serialize,

Source§

fn create<'life0, 'life1, 'async_trait>( &'life0 self, entity: &'life1 T, ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, PostgresRepository<T>: 'async_trait,

Create a new entity
Source§

fn find_by_id<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<T>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, PostgresRepository<T>: 'async_trait,

Find entity by ID
Source§

fn find_all<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<T>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, PostgresRepository<T>: 'async_trait,

Find all entities
Source§

fn update<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, id: &'life1 str, entity: &'life2 T, ) -> Pin<Box<dyn Future<Output = Result<Option<T>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, PostgresRepository<T>: 'async_trait,

Update an existing entity
Source§

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, PostgresRepository<T>: 'async_trait,

Delete an entity
Source§

fn count<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, PostgresRepository<T>: 'async_trait,

Count all entities
Source§

fn exists<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, PostgresRepository<T>: 'async_trait,

Check if entity exists
Source§

fn execute_query<'life0, 'life1, 'async_trait>( &'life0 self, query: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, PostgresRepository<T>: 'async_trait,

Execute custom query

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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