Skip to main content

PersistentRepository

Struct PersistentRepository 

Source
pub struct PersistentRepository<E>{
    pub db: DatabaseConnection,
    /* private fields */
}
Expand description

Generic database access for one SeaORM entity.

E is the SeaORM entity; its model must implement BaseEntity. Holds a cloned DatabaseConnection used by every operation.

Fields§

§db: DatabaseConnection

Connection used by every operation.

Implementations§

Source§

impl<E> PersistentRepository<E>

Source

pub fn new(db: DatabaseConnection) -> Self

Creates a repository holding the given connection.

Source

pub fn db(&self) -> &DatabaseConnection

Returns the underlying connection.

Source

pub fn initialize(db: &DatabaseConnection) -> Arc<Self>

Registers a repository for E in the process-wide registry and returns it.

Panics if a repository for E was already initialized.

Source

pub fn find() -> Arc<Self>

Returns the registered repository for E.

Panics if PersistentRepository::initialize was not called first.

Source

pub fn try_find() -> Option<Arc<Self>>

Returns the registered repository for E, or None when uninitialized.

Source

pub fn new_ephemeral(db: DatabaseConnection) -> Self

Creates a repository without touching the shared registry.

Source

pub fn start(&self) -> QueryData<E>
where E::ModelEx: BaseEntity,

Starts a fresh QueryData over E::find().

Source

pub fn start_delete(&self) -> DeleteQueryData<E>

Starts a fresh DeleteQueryData over E::delete_many().

Source

pub fn is_owner<U>(&self, model: &E::ModelEx, user: &U) -> bool
where U: BaseEntity,

Returns true when the model and user share the same id.

Source

pub fn is_owner_auditable<U>(&self, model: &E::ModelEx, user: &U) -> bool
where U: BaseEntity + Clone + Send + Sync, E::ModelEx: BaseAuditableEntity<User = U>,

Returns true when the model’s creator id matches the user id.

Source

pub fn is_accessible<U>(&self, model: &E::ModelEx, user: &U) -> bool
where U: BaseEntity,

Returns true when the user may access the model; currently same as PersistentRepository::is_owner.

Source

pub fn is_accessible_auditable<U>(&self, model: &E::ModelEx, user: &U) -> bool
where U: BaseEntity + Clone + Send + Sync, E::ModelEx: BaseAuditableEntity<User = U>,

Returns true when the user created or last updated the model.

Source

pub async fn transaction<F, T, EType>(&self, f: F) -> Result<T, EType>
where EType: From<DbErr> + Display + Send, F: FnOnce(Arc<DatabaseTransaction>) -> BoxFuture<'static, Result<T, EType>> + Send, T: Send,

Runs the closure inside a transaction, committing on success.

The closure receives an Arc<DatabaseTransaction> so the handle can be freely shared — e.g., moved into RepositoryOptions::with_transaction for nested repository calls. Do not retain clones beyond the closure: committing requires sole ownership of the handle, and a closure error drops it uncommitted, rolling the transaction back.

Returns the closure value or the DbErr from beginning, the closure, or commit.

Source

pub async fn transaction_with_opts<U, F, T, EType>( &self, opts: RepositoryOptions<U>, f: F, ) -> Result<T, EType>
where EType: From<DbErr> + Display + Send, U: BaseEntity + Clone + Send + Sync + 'static, F: FnOnce(Arc<DatabaseTransaction>) -> BoxFuture<'static, Result<T, EType>> + Send, T: Send,

Runs the closure inside opts.txn when one is attached, else in a fresh transaction.

When opts carries a transaction, the closure receives that shared handle directly and no commit/rollback is performed here — the owner of that transaction controls its lifecycle. Otherwise a new transaction is begun from the repository connection and committed on success.

Source

pub async fn get_count<U>( &self, filter: Option<QueryData<E>>, opts: Option<RepositoryOptions<U>>, ) -> Result<u64, DbErr>
where U: BaseEntity + Clone + Send + Sync + 'static, E::ModelEx: BaseEntity,

Counts rows matching the filter, applying the cursor when one is set.

U is the RepositoryOptions identity type. The cursor comes from opts when present, else from the QueryData options. Runs against opts.txn when attached, else against the repository connection.

Source

pub async fn get_count_txn( &self, filter: Option<QueryData<E>>, txn: &DatabaseTransaction, ) -> Result<u64, DbErr>
where E::ModelEx: BaseEntity,

Counts rows matching the filter inside the given transaction.

Source

pub async fn count(&self, filter: Option<QueryData<E>>) -> Result<u64, DbErr>
where E::ModelEx: BaseEntity,

Counts rows matching the filter with default options.

Source

pub async fn get_paginated_view<U>( &self, filter: Option<QueryData<E>>, opts: RepositoryOptions<U>, ) -> Result<PageResult<E::ModelEx>, DbErr>
where U: BaseEntity + Clone + Send + Sync + 'static, E::ModelEx: BaseEntity,

Returns one cursor page: totals ignore cursor/limit, rows fetch limit + 1 to detect has_next.

U is the RepositoryOptions identity type. The next-page cursor encodes limit and the last row id; it is None when there is no next page. Runs against opts.txn when attached, else against the repository connection.

Source

pub async fn paginated( &self, select: Select<E>, opts: RepositoryOptions<NoUser>, ) -> Result<PageResult<E::ModelEx>, DbErr>
where E::ModelEx: BaseEntity, E::Model: Into<E::ModelEx> + Send + Sync,

Paginates a raw Select: totals ignore cursor/limit and cursors use id > cursor.

Unlike PersistentRepository::get_paginated_view, no recorded ordering direction is consulted because the select carries none. Runs against opts.txn when attached, else against the repository connection.

Source

pub async fn get_all<U>( &self, filter: Option<QueryData<E>>, opts: Option<RepositoryOptions<U>>, ) -> Result<Vec<E::ModelEx>, DbErr>
where U: BaseEntity + Clone + Send + Sync + 'static, E::ModelEx: BaseEntity,

Returns all matching rows, applying cursor (opts cursor wins over QueryData cursor) and distinct.

U is the RepositoryOptions identity type. Runs against opts.txn (or the QueryData transaction) when attached, else against the repository connection.

Source

pub async fn get_many<U>( &self, filter: Option<QueryData<E>>, opts: RepositoryOptions<U>, ) -> Result<Vec<E::ModelEx>, DbErr>
where U: BaseEntity + Clone + Send + Sync + 'static, E::ModelEx: BaseEntity,

Returns up to opts.limit matching rows, applying cursor and distinct.

U is the RepositoryOptions identity type. The opts cursor is preferred when set, else the QueryData cursor is used. Runs against opts.txn (or the QueryData transaction) when attached.

Source

pub async fn find_many<U>( &self, filter: Option<QueryData<E>>, opts: RepositoryOptions<U>, ) -> Result<Vec<E::ModelEx>, DbErr>
where U: BaseEntity + Clone + Send + Sync + 'static, E::ModelEx: BaseEntity,

Source

pub async fn find_all( &self, filter: Option<QueryData<E>>, ) -> Result<Vec<E::ModelEx>, DbErr>
where E::ModelEx: BaseEntity,

Returns all matching rows with default options.

Source

pub async fn get_distinct_rows<U>( &self, filter: Option<QueryData<E>>, opts: RepositoryOptions<U>, ) -> Result<Vec<E::ModelEx>, DbErr>
where U: BaseEntity + Clone + Send + Sync + 'static, E::ModelEx: BaseEntity,

Returns up to opts.limit distinct rows matching the filter, applying cursor.

U is the RepositoryOptions identity type. Runs against opts.txn (or the QueryData transaction) when attached.

Source

pub async fn get_one<U>( &self, filter: Option<QueryData<E>>, opts: Option<RepositoryOptions<U>>, ) -> Result<Option<E::ModelEx>, DbErr>
where U: BaseEntity + Clone + Send + Sync + 'static, E::ModelEx: BaseEntity,

Returns the first matching row, if any, applying cursor when one is set.

U is the RepositoryOptions identity type. Runs against opts.txn (or the QueryData transaction) when attached.

Source

pub async fn find_one( &self, filter: Option<QueryData<E>>, ) -> Result<Option<E::ModelEx>, DbErr>
where E::ModelEx: BaseEntity,

Returns the first matching row with default options.

Source

pub async fn get_by_id(&self, id: i64) -> Result<Option<E::ModelEx>, DbErr>

Finds a row by primary key converted from id; returns None when absent.

Source

pub async fn find_by_id(&self, id: i64) -> Result<Option<E::ModelEx>, DbErr>

Source

pub async fn get_by_id_with_opts<U>( &self, id: i64, opts: RepositoryOptions<U>, ) -> Result<Option<E::ModelEx>, DbErr>

Finds a row by id; runs inside opts.txn when one is attached.

U is the RepositoryOptions identity type.

Source

pub async fn create_one<A, U>( &self, active: A, opts: Option<RepositoryOptions<U>>, ) -> Result<E::ModelEx, DbErr>

Inserts one row, filling uid/createdAt/updatedAt (and audit ids when a user id is present).

A is the SeaORM active model for E; U is the RepositoryOptions identity type. Inserts inside opts.txn when attached, else on the repository connection.

Source

pub async fn create_one_simple<A>(&self, active: A) -> Result<E::ModelEx, DbErr>

Inserts one row with default options.

Source

pub async fn create_one_with_opts<A, U>( &self, active: A, opts: RepositoryOptions<U>, ) -> Result<E::ModelEx, DbErr>

Inserts one row with the given options.

A is the SeaORM active model for E; U is the RepositoryOptions identity type.

Source

pub async fn create_many<A, U>( &self, actives: Vec<A>, opts: Option<RepositoryOptions<U>>, ) -> Result<Vec<E::ModelEx>, DbErr>

Inserts each row one at a time, applying base/audit fields to every row.

A is the SeaORM active model for E; U is the RepositoryOptions identity type. Inserts inside opts.txn when attached, else on the repository connection.

Source

pub async fn create_many_simple<A>( &self, actives: Vec<A>, ) -> Result<Vec<E::ModelEx>, DbErr>

Inserts many rows with default options.

Source

pub async fn create_many_with_opts<A, U>( &self, actives: Vec<A>, opts: RepositoryOptions<U>, ) -> Result<Vec<E::ModelEx>, DbErr>

Inserts many rows with the given options.

Source

pub async fn upsert_one<A>(&self, active: A) -> Result<E::ModelEx, DbErr>

Inserts one row with fresh base fields; does not check for an existing id.

Source

pub async fn upsert_many<A>( &self, actives: Vec<A>, ) -> Result<Vec<E::ModelEx>, DbErr>

Inserts each row via PersistentRepository::upsert_one, one at a time.

Source

pub async fn upsert_one_with_id<A, F>( &self, active: A, id_fn: F, ) -> Result<E::ModelEx, DbErr>

Updates the row when id_fn yields an existing id, else inserts it.

Updates refresh updatedAt; inserts fill the full base field set.

Source

pub async fn update_many<F, U>( &self, filter: Option<QueryData<E>>, effector: F, opts: RepositoryOptions<U>, ) -> Result<ChangeResultModel<E::ModelEx>, DbErr>
where F: FnMut(&mut E::Model) -> bool + Send, U: BaseEntity + Clone + Send + Sync + 'static, E::ModelEx: Into<E::Model> + BaseEntity, E::Model: IntoActiveModel<E::ActiveModel> + Clone, E::ActiveModel: ActiveModelTrait<Entity = E> + Send, E::Column: ColumnTrait + FromStr, <E::Column as FromStr>::Err: Debug,

Loads rows via PersistentRepository::get_many, applies effector, and saves changed rows.

The effector mutates each model and returns true to persist it; untouched rows are skipped. Returns the pre-save models that were persisted. U is the RepositoryOptions identity type used for audit fields. Reads and writes run inside opts.txn when attached.

Source

pub async fn find_update_many<F, U>( &self, filter: Option<QueryData<E>>, effector: F, opts: RepositoryOptions<U>, ) -> Result<ChangeResultModel<E::ModelEx>, DbErr>
where F: FnMut(&mut E::Model) -> bool + Send, U: BaseEntity + Clone + Send + Sync + 'static, E::ModelEx: Into<E::Model> + BaseEntity, E::Model: IntoActiveModel<E::ActiveModel> + Clone, E::ActiveModel: ActiveModelTrait<Entity = E> + Send, E::Column: ColumnTrait + FromStr, <E::Column as FromStr>::Err: Debug,

Source

pub async fn update_one<F>( &self, filter: Option<QueryData<E>>, effector: F, ) -> Result<Option<E::ModelEx>, DbErr>
where F: FnMut(&mut E::Model) -> bool + Send, E::ModelEx: Into<E::Model> + BaseEntity, E::Model: IntoActiveModel<E::ActiveModel> + Clone, E::ActiveModel: ActiveModelTrait<Entity = E> + Send, E::Column: ColumnTrait + FromStr, <E::Column as FromStr>::Err: Debug,

Loads the first matching row, applies effector, and saves it when changed.

Returns None when no row matches; returns the row unchanged when the effector returns false.

Source

pub async fn update_by_id<F>( &self, id: i64, effector: F, ) -> Result<Option<E::ModelEx>, DbErr>

Loads a row by id, applies effector to the model, and saves it when changed.

Returns None when the id does not exist.

Source

pub async fn update_by_id_with_active<F>( &self, id: i64, effector: F, ) -> Result<Option<E::ModelEx>, DbErr>

Loads a row by id, applies effector to the active model, and saves it when changed.

Returns the refetched row unchanged when the effector returns false, or None when the id does not exist.

Source

pub async fn update_one_with_opts<F, U>( &self, filter: Option<QueryData<E>>, effector: F, opts: Option<RepositoryOptions<U>>, ) -> Result<Option<E::ModelEx>, DbErr>
where F: FnMut(&mut E::Model) -> bool + Send, U: BaseEntity + Clone + Send + Sync + 'static, E::ModelEx: Into<E::Model>, E::Model: IntoActiveModel<E::ActiveModel> + Clone, E::ActiveModel: ActiveModelTrait<Entity = E> + Send, E::Column: ColumnTrait + FromStr, <E::Column as FromStr>::Err: Debug,

Loads the first matching row and updates it, recording audit fields from opts when present.

U is the RepositoryOptions identity type. Reads and writes run inside opts.txn (or the filter transaction) when attached.

Source

pub async fn update_by_id_with_opts<F, U>( &self, id: i64, effector: F, opts: Option<RepositoryOptions<U>>, ) -> Result<Option<E::ModelEx>, DbErr>

Loads a row by id and updates it, recording audit fields from opts when present.

U is the RepositoryOptions identity type. Returns None when the id does not exist. Reads and writes run inside opts.txn when attached.

Source

pub async fn delete_many( &self, filter: Option<DeleteQueryData<E>>, ) -> Result<u64, DbErr>

Deletes rows matching the filter and returns the affected row count.

Source

pub async fn delete_many_with_opts<U>( &self, filter: Option<DeleteQueryData<E>>, opts: Option<RepositoryOptions<U>>, ) -> Result<u64, DbErr>
where U: BaseEntity + Clone + Send + Sync + 'static,

Deletes rows matching the filter with cursor support and returns the affected row count.

U is the RepositoryOptions identity type. Deletes run inside opts.txn when attached, else on the repository connection.

Source

pub async fn remove_many( &self, filter: Option<DeleteQueryData<E>>, ) -> Result<u64, DbErr>

Source

pub async fn remove_many_with_opts<U>( &self, filter: Option<DeleteQueryData<E>>, opts: Option<RepositoryOptions<U>>, ) -> Result<u64, DbErr>
where U: BaseEntity + Clone + Send + Sync + 'static,

Source

pub async fn delete_one( &self, filter: Option<DeleteQueryData<E>>, ) -> Result<bool, DbErr>

Deletes matching rows and returns true when at least one row was deleted.

Source

pub async fn delete_one_with_opts<U>( &self, filter: Option<DeleteQueryData<E>>, opts: Option<RepositoryOptions<U>>, ) -> Result<bool, DbErr>
where U: BaseEntity + Clone + Send + Sync + 'static,

Deletes matching rows with cursor support; returns true when at least one row was deleted.

U is the RepositoryOptions identity type. Deletes run inside opts.txn when attached.

Source

pub async fn delete_by_id(&self, id: i64) -> Result<Option<E::ModelEx>, DbErr>

Deletes the row with the given id and returns the deleted model, or None when absent.

Source

pub async fn delete_by_id_with_opts<U>( &self, id: i64, opts: Option<RepositoryOptions<U>>, ) -> Result<Option<E::ModelEx>, DbErr>

Deletes the row with the given id using opts.txn when attached.

U is the RepositoryOptions identity type. Reads and deletes run inside the attached transaction; otherwise on the repository connection.

Source

pub async fn delete_by_id_txn( &self, id: i64, txn: &DatabaseTransaction, ) -> Result<Option<E::ModelEx>, DbErr>

Deletes the row with the given id inside the transaction, returning the deleted model if found.

Source

pub async fn delete_many_txn( &self, filter: Option<DeleteQueryData<E>>, txn: &DatabaseTransaction, ) -> Result<u64, DbErr>

Deletes rows matching the filter inside the transaction, returning the affected row count.

Source

pub async fn delete_many_txn_with_opts<U>( &self, filter: Option<DeleteQueryData<E>>, opts: Option<RepositoryOptions<U>>, txn: &DatabaseTransaction, ) -> Result<u64, DbErr>
where U: BaseEntity + Clone + Send + Sync + 'static,

Deletes rows matching the filter inside the transaction with cursor support.

U is the RepositoryOptions identity type. When opts carries its own transaction it takes precedence; otherwise the explicit txn is used.

Trait Implementations§

Source§

impl<E> Clone for PersistentRepository<E>

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

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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> CompatExt for T

Source§

fn compat(self) -> Compat<T>
where T: Sized,

Applies the Compat adapter by value. Read more
Source§

fn compat_ref(&self) -> Compat<&T>

Applies the Compat adapter by shared reference. Read more
Source§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the Compat adapter by mutable reference. Read more
Source§

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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<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