Skip to main content

GenericCrudRepository

Struct GenericCrudRepository 

Source
pub struct GenericCrudRepository<T, D = SoftDelete>
where T: for<'r> FromRow<'r, PgRow> + Send + Unpin,
{ /* private fields */ }
Expand description

Generic PostgreSQL CRUD repository parameterised by entity type T and delete strategy D (SoftDelete or HardDelete).

All standard CRUD methods are implemented here once. Generated repository structs wrap this type via newtype + Deref and only add entity-specific methods (find_by_*, exists_by_*, partial_update, filtered listings).

Implementations§

Source§

impl<T, D> GenericCrudRepository<T, D>
where T: for<'r> FromRow<'r, PgRow> + Send + Unpin,

Source

pub fn new(pool: PgPool, table_name: &str) -> Self

Source

pub fn pool(&self) -> &PgPool

Source

pub fn table_name(&self) -> &str

Source

pub async fn create(&self, entity: &T) -> Result<T>
where T: Serialize + Send + Sync,

Insert a new entity row and return the created row.

Source

pub async fn bulk_create(&self, entities: &[T]) -> Result<Vec<T>>
where T: Serialize + Send + Sync,

Insert multiple entity rows inside a single transaction.

Source

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

Execute a filtered / paginated query against this entity’s table.

base_condition — when Some, it is inserted as the __base_condition filter key which the ORM injects verbatim into the WHERE clause. Use this to add soft-delete guards without touching the caller-supplied filters.

NOTE: prefer the *_scoped entry points for anything a client can reach — they compose the company fence into this condition. This raw form applies no fence and is for internal/job callers that have already established scope.

Source

pub async fn run_aggregate_query( &self, spec: &AggregateSpec, base_condition: Option<&str>, filters: &HashMap<String, String>, column_types: &HashMap<String, String>, search_fields: &[&str], ) -> Result<AggregateResult>
where T: EntityRepoMeta + Send + Sync,

The aggregate twin of Self::run_filtered_query.

Takes the same base_condition so an aggregate is computed over exactly the row set the matching list would return — a total that counted soft-deleted rows, or another tenant’s, would be wrong in a way no caller could see.

Source§

impl<T> GenericCrudRepository<T, SoftDelete>
where T: for<'r> FromRow<'r, PgRow> + Send + Unpin,

Source

pub async fn find_by_text_field( &self, field: &str, value: &str, ) -> Result<Option<T>>

Find an active entity by a unique text field.

Filters metadata->>'deleted_at' IS NULL automatically.

Source

pub async fn exists_by_text_field( &self, field: &str, value: &str, ) -> Result<bool>

Check existence by a unique text field (active records only).

Source

pub async fn find_by_uuid_field( &self, field: &str, value: Uuid, ) -> Result<Option<T>>

Find an active entity by a unique UUID field.

Source

pub async fn exists_by_uuid_field( &self, field: &str, value: Uuid, ) -> Result<bool>

Check existence by a unique UUID field (active records only).

Source

pub async fn list_paginated_filtered( &self, pagination: PaginationParams, filters: Option<&HashMap<String, String>>, ) -> Result<PaginatedResult<T>>
where T: EntityRepoMeta + Send + Sync,

Paginate active entities with filter and search support.

Requires T: EntityRepoMeta for column type hints and search fields.

Source

pub async fn aggregate_filtered( &self, spec: &AggregateSpec, filters: Option<&HashMap<String, String>>, ) -> Result<AggregateResult>
where T: EntityRepoMeta + Send + Sync,

Group and reduce active entities, skipping the soft-deleted.

Source

pub async fn list_paginated_filtered_scoped( &self, pagination: PaginationParams, filters: Option<&HashMap<String, String>>, company: Option<Uuid>, ) -> Result<PaginatedResult<T>>
where T: EntityRepoMeta + Send + Sync,

Paginate active entities, fenced to company.

The company-scoped counterpart of Self::list_paginated_filtered and the one any client-reachable read should use. Fails closed: a fenced entity with no company returns MissingCompanyScope rather than every company’s rows.

The fence is ANDed into the same __base_condition the soft-delete guard uses, so it lands in SQL — which is what keeps total honest. A post-filter in the handler would fetch limit rows and return the survivors, leaving callers unable to tell “end of data” from “filtered”, and could not fix COUNT at all.

Source

pub async fn list_deleted_filtered_scoped( &self, pagination: PaginationParams, filters: Option<&HashMap<String, String>>, company: Option<Uuid>, ) -> Result<PaginatedResult<T>>
where T: EntityRepoMeta + Send + Sync,

Paginate soft-deleted entities, fenced to company.

/trash is the worse leak of the two: it serves rows whose owners were told the data is gone. Same fence, same fail-closed contract.

Source

pub async fn list_deleted_filtered( &self, pagination: PaginationParams, filters: Option<&HashMap<String, String>>, ) -> Result<PaginatedResult<T>>
where T: EntityRepoMeta + Send + Sync,

Paginate soft-deleted entities with filter support.

Source

pub async fn find_by_id(&self, id: &str) -> Result<Option<T>>

Find an active (non-deleted) entity by primary key.

Source

pub async fn find_all(&self) -> Result<Vec<T>>

Return all active (non-deleted) entities.

Source

pub async fn update(&self, id: &str, entity: &T) -> Result<Option<T>>
where T: Serialize + Send + Sync,

Full update — skips silently if the record is already soft-deleted.

Source

pub async fn delete(&self, id: &str) -> Result<bool>

Soft-delete an entity (sets metadata.deleted_at).

Source

pub async fn count(&self) -> Result<u64>

Count active (non-deleted) entities.

Source

pub async fn exists(&self, id: &str) -> Result<bool>

Return true if an active entity with the given ID exists.

Source

pub async fn list_paginated( &self, pagination: PaginationParams, ) -> Result<PaginatedResult<T>>

Paginate active entities (most-recent-first by ID).

Source

pub async fn soft_delete(&self, id: &str) -> Result<bool>

Set metadata.deleted_at to NOW() (soft delete).

Source

pub async fn restore(&self, id: &str) -> Result<Option<T>>

Remove deleted_at from metadata, restoring the entity.

Source

pub async fn list_deleted( &self, pagination: PaginationParams, ) -> Result<PaginatedResult<T>>

Paginate soft-deleted entities (trash view).

Source

pub async fn empty_trash(&self) -> Result<u64>

Permanently delete all soft-deleted rows (empty trash).

Source

pub async fn find_deleted_by_id(&self, id: &str) -> Result<Option<T>>

Find a soft-deleted entity by primary key.

Source

pub async fn permanent_delete(&self, id: &str) -> Result<bool>

Permanently delete a soft-deleted entity by primary key.

Source

pub async fn count_active(&self) -> Result<u64>

Count active (non-deleted) entities.

Source

pub async fn count_deleted(&self) -> Result<u64>

Count soft-deleted entities.

Source

pub async fn bulk_soft_delete(&self, ids: &[String]) -> Result<u64>

Soft-delete many active rows atomically.

Source

pub async fn bulk_restore(&self, ids: &[String]) -> Result<Vec<T>>

Restore many soft-deleted rows atomically, returning the restored rows.

Source

pub async fn bulk_permanent_delete(&self, ids: &[String]) -> Result<u64>

Permanently delete many soft-deleted rows atomically.

Source

pub async fn restore_all(&self) -> Result<Vec<T>>

Restore every soft-deleted row, returning the restored rows. A single UPDATE ... RETURNING is atomic, and the returned rows let the service layer emit a Restored event per entity.

Source

pub async fn bulk_update(&self, entities: &[T]) -> Result<Vec<T>>
where T: Serialize + Send + Sync,

Update many active rows atomically. Every entity must reference an existing active (non-soft-deleted) row or the whole batch is rolled back.

Source§

impl<T> GenericCrudRepository<T, HardDelete>
where T: for<'r> FromRow<'r, PgRow> + Send + Sync + Unpin + Serialize,

Source

pub async fn find_by_text_field( &self, field: &str, value: &str, ) -> Result<Option<T>>

Find an entity by a unique text field (no soft-delete guard).

Source

pub async fn exists_by_text_field( &self, field: &str, value: &str, ) -> Result<bool>

Check existence by a unique text field.

Source

pub async fn find_by_uuid_field( &self, field: &str, value: Uuid, ) -> Result<Option<T>>

Find an entity by a unique UUID field.

Source

pub async fn exists_by_uuid_field( &self, field: &str, value: Uuid, ) -> Result<bool>

Check existence by a unique UUID field.

Source

pub async fn list_paginated_filtered( &self, pagination: PaginationParams, filters: Option<&HashMap<String, String>>, ) -> Result<PaginatedResult<T>>
where T: EntityRepoMeta + Send + Sync,

Paginate entities with filter and search support.

Source

pub async fn aggregate_filtered( &self, spec: &AggregateSpec, filters: Option<&HashMap<String, String>>, ) -> Result<AggregateResult>
where T: EntityRepoMeta + Send + Sync,

Group and reduce entities. This mode has no trash to exclude.

Source

pub async fn find_by_id(&self, id: &str) -> Result<Option<T>>

Find an entity by primary key.

Source

pub async fn find_all(&self) -> Result<Vec<T>>

Return all entities.

Source

pub async fn update(&self, id: &str, entity: &T) -> Result<Option<T>>

Full update.

Source

pub async fn delete(&self, id: &str) -> Result<bool>

Permanently delete an entity by primary key.

Source

pub async fn count(&self) -> Result<u64>

Count all entities.

Source

pub async fn exists(&self, id: &str) -> Result<bool>

Return true if an entity with the given ID exists.

Source

pub async fn list_paginated( &self, pagination: PaginationParams, ) -> Result<PaginatedResult<T>>

Paginate entities (most-recent-first by ID).

Source

pub async fn bulk_delete(&self, ids: &[String]) -> Result<u64>

Hard-delete many rows atomically. Rolls back unless every id matched.

Source

pub async fn bulk_update(&self, entities: &[T]) -> Result<Vec<T>>

Update many rows atomically. No soft-delete guard in this mode.

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