pub struct PersistentRepository<E>where
E: EntityTrait,
E::ModelEx: BaseEntity + Send + Sync,
<E::ModelEx as BaseEntity>::LoaderType: EntityLoaderTrait<E, ModelEx = E::ModelEx>,{
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: DatabaseConnectionConnection used by every operation.
Implementations§
Source§impl<E> PersistentRepository<E>where
E: EntityTrait,
E::ModelEx: BaseEntity + Send + Sync + Clone + Serialize,
E::Model: Into<E::ModelEx> + Send + Sync,
<E::ModelEx as BaseEntity>::LoaderType: EntityLoaderTrait<E, ModelEx = E::ModelEx>,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
impl<E> PersistentRepository<E>where
E: EntityTrait,
E::ModelEx: BaseEntity + Send + Sync + Clone + Serialize,
E::Model: Into<E::ModelEx> + Send + Sync,
<E::ModelEx as BaseEntity>::LoaderType: EntityLoaderTrait<E, ModelEx = E::ModelEx>,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
Sourcepub fn new(db: DatabaseConnection) -> Self
pub fn new(db: DatabaseConnection) -> Self
Creates a repository holding the given connection.
Sourcepub fn db(&self) -> &DatabaseConnection
pub fn db(&self) -> &DatabaseConnection
Returns the underlying connection.
Sourcepub fn initialize(db: &DatabaseConnection) -> Arc<Self> ⓘ
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.
Sourcepub fn find() -> Arc<Self> ⓘ
pub fn find() -> Arc<Self> ⓘ
Returns the registered repository for E.
Panics if PersistentRepository::initialize was not called first.
Sourcepub fn try_find() -> Option<Arc<Self>>
pub fn try_find() -> Option<Arc<Self>>
Returns the registered repository for E, or None when uninitialized.
Sourcepub fn new_ephemeral(db: DatabaseConnection) -> Self
pub fn new_ephemeral(db: DatabaseConnection) -> Self
Creates a repository without touching the shared registry.
Sourcepub fn start(&self) -> QueryData<E>where
E::ModelEx: BaseEntity,
pub fn start(&self) -> QueryData<E>where
E::ModelEx: BaseEntity,
Starts a fresh QueryData over E::find().
Sourcepub fn start_delete(&self) -> DeleteQueryData<E>
pub fn start_delete(&self) -> DeleteQueryData<E>
Starts a fresh DeleteQueryData over E::delete_many().
Sourcepub fn is_owner<U>(&self, model: &E::ModelEx, user: &U) -> boolwhere
U: BaseEntity,
pub fn is_owner<U>(&self, model: &E::ModelEx, user: &U) -> boolwhere
U: BaseEntity,
Returns true when the model and user share the same id.
Sourcepub fn is_owner_auditable<U>(&self, model: &E::ModelEx, user: &U) -> bool
pub fn is_owner_auditable<U>(&self, model: &E::ModelEx, user: &U) -> bool
Returns true when the model’s creator id matches the user id.
Sourcepub fn is_accessible<U>(&self, model: &E::ModelEx, user: &U) -> boolwhere
U: BaseEntity,
pub fn is_accessible<U>(&self, model: &E::ModelEx, user: &U) -> boolwhere
U: BaseEntity,
Returns true when the user may access the model; currently same as PersistentRepository::is_owner.
Sourcepub fn is_accessible_auditable<U>(&self, model: &E::ModelEx, user: &U) -> bool
pub fn is_accessible_auditable<U>(&self, model: &E::ModelEx, user: &U) -> bool
Returns true when the user created or last updated the model.
Sourcepub async fn transaction<F, T, EType>(&self, f: F) -> Result<T, EType>
pub async fn transaction<F, T, EType>(&self, f: F) -> Result<T, EType>
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.
Sourcepub async fn transaction_with_opts<U, F, T, EType>(
&self,
opts: RepositoryOptions<U>,
f: F,
) -> Result<T, EType>
pub async fn transaction_with_opts<U, F, T, EType>( &self, opts: RepositoryOptions<U>, f: F, ) -> Result<T, EType>
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.
Sourcepub async fn get_count<U>(
&self,
filter: Option<QueryData<E>>,
opts: Option<RepositoryOptions<U>>,
) -> Result<u64, DbErr>
pub async fn get_count<U>( &self, filter: Option<QueryData<E>>, opts: Option<RepositoryOptions<U>>, ) -> Result<u64, DbErr>
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.
Sourcepub async fn get_count_txn(
&self,
filter: Option<QueryData<E>>,
txn: &DatabaseTransaction,
) -> Result<u64, DbErr>where
E::ModelEx: BaseEntity,
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.
Sourcepub async fn count(&self, filter: Option<QueryData<E>>) -> Result<u64, DbErr>where
E::ModelEx: BaseEntity,
pub async fn count(&self, filter: Option<QueryData<E>>) -> Result<u64, DbErr>where
E::ModelEx: BaseEntity,
Counts rows matching the filter with default options.
Sourcepub async fn get_paginated_view<U>(
&self,
filter: Option<QueryData<E>>,
opts: RepositoryOptions<U>,
) -> Result<PageResult<E::ModelEx>, DbErr>
pub async fn get_paginated_view<U>( &self, filter: Option<QueryData<E>>, opts: RepositoryOptions<U>, ) -> Result<PageResult<E::ModelEx>, DbErr>
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.
Sourcepub async fn paginated(
&self,
select: Select<E>,
opts: RepositoryOptions<NoUser>,
) -> Result<PageResult<E::ModelEx>, DbErr>
pub async fn paginated( &self, select: Select<E>, opts: RepositoryOptions<NoUser>, ) -> Result<PageResult<E::ModelEx>, DbErr>
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.
Sourcepub async fn get_all<U>(
&self,
filter: Option<QueryData<E>>,
opts: Option<RepositoryOptions<U>>,
) -> Result<Vec<E::ModelEx>, DbErr>
pub async fn get_all<U>( &self, filter: Option<QueryData<E>>, opts: Option<RepositoryOptions<U>>, ) -> Result<Vec<E::ModelEx>, DbErr>
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.
Sourcepub async fn get_many<U>(
&self,
filter: Option<QueryData<E>>,
opts: RepositoryOptions<U>,
) -> Result<Vec<E::ModelEx>, DbErr>
pub async fn get_many<U>( &self, filter: Option<QueryData<E>>, opts: RepositoryOptions<U>, ) -> Result<Vec<E::ModelEx>, DbErr>
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.
Sourcepub async fn find_many<U>(
&self,
filter: Option<QueryData<E>>,
opts: RepositoryOptions<U>,
) -> Result<Vec<E::ModelEx>, DbErr>
pub async fn find_many<U>( &self, filter: Option<QueryData<E>>, opts: RepositoryOptions<U>, ) -> Result<Vec<E::ModelEx>, DbErr>
Alias for PersistentRepository::get_many.
Sourcepub async fn find_all(
&self,
filter: Option<QueryData<E>>,
) -> Result<Vec<E::ModelEx>, DbErr>where
E::ModelEx: BaseEntity,
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.
Sourcepub async fn get_distinct_rows<U>(
&self,
filter: Option<QueryData<E>>,
opts: RepositoryOptions<U>,
) -> Result<Vec<E::ModelEx>, DbErr>
pub async fn get_distinct_rows<U>( &self, filter: Option<QueryData<E>>, opts: RepositoryOptions<U>, ) -> Result<Vec<E::ModelEx>, DbErr>
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.
Sourcepub async fn get_one<U>(
&self,
filter: Option<QueryData<E>>,
opts: Option<RepositoryOptions<U>>,
) -> Result<Option<E::ModelEx>, DbErr>
pub async fn get_one<U>( &self, filter: Option<QueryData<E>>, opts: Option<RepositoryOptions<U>>, ) -> Result<Option<E::ModelEx>, DbErr>
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.
Sourcepub async fn find_one(
&self,
filter: Option<QueryData<E>>,
) -> Result<Option<E::ModelEx>, DbErr>where
E::ModelEx: BaseEntity,
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.
Sourcepub async fn get_by_id(&self, id: i64) -> Result<Option<E::ModelEx>, DbErr>where
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::ModelEx: BaseEntity,
<E::ModelEx as BaseEntity>::LoaderType: EntityLoaderTrait<E, ModelEx = E::ModelEx>,
pub async fn get_by_id(&self, id: i64) -> Result<Option<E::ModelEx>, DbErr>where
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::ModelEx: BaseEntity,
<E::ModelEx as BaseEntity>::LoaderType: EntityLoaderTrait<E, ModelEx = E::ModelEx>,
Finds a row by primary key converted from id; returns None when absent.
Sourcepub async fn find_by_id(&self, id: i64) -> Result<Option<E::ModelEx>, DbErr>where
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::ModelEx: BaseEntity,
pub async fn find_by_id(&self, id: i64) -> Result<Option<E::ModelEx>, DbErr>where
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::ModelEx: BaseEntity,
Alias for PersistentRepository::get_by_id.
Sourcepub async fn get_by_id_with_opts<U>(
&self,
id: i64,
opts: RepositoryOptions<U>,
) -> Result<Option<E::ModelEx>, DbErr>where
U: BaseEntity + Clone + Send + Sync + 'static,
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::ModelEx: BaseEntity,
pub async fn get_by_id_with_opts<U>(
&self,
id: i64,
opts: RepositoryOptions<U>,
) -> Result<Option<E::ModelEx>, DbErr>where
U: BaseEntity + Clone + Send + Sync + 'static,
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::ModelEx: BaseEntity,
Finds a row by id; runs inside opts.txn when one is attached.
U is the RepositoryOptions identity type.
Sourcepub async fn create_one<A, U>(
&self,
active: A,
opts: Option<RepositoryOptions<U>>,
) -> Result<E::ModelEx, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::Model: IntoActiveModel<A>,
E::ModelEx: BaseEntity,
U: BaseEntity + Clone + Send + Sync + 'static,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
pub async fn create_one<A, U>(
&self,
active: A,
opts: Option<RepositoryOptions<U>>,
) -> Result<E::ModelEx, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::Model: IntoActiveModel<A>,
E::ModelEx: BaseEntity,
U: BaseEntity + Clone + Send + Sync + 'static,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
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.
Sourcepub async fn create_one_simple<A>(&self, active: A) -> Result<E::ModelEx, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::Model: IntoActiveModel<A>,
E::ModelEx: BaseEntity,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
pub async fn create_one_simple<A>(&self, active: A) -> Result<E::ModelEx, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::Model: IntoActiveModel<A>,
E::ModelEx: BaseEntity,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
Inserts one row with default options.
Sourcepub async fn create_one_with_opts<A, U>(
&self,
active: A,
opts: RepositoryOptions<U>,
) -> Result<E::ModelEx, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::Model: IntoActiveModel<A>,
E::ModelEx: BaseEntity,
U: BaseEntity + Clone + Send + Sync + 'static,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
pub async fn create_one_with_opts<A, U>(
&self,
active: A,
opts: RepositoryOptions<U>,
) -> Result<E::ModelEx, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::Model: IntoActiveModel<A>,
E::ModelEx: BaseEntity,
U: BaseEntity + Clone + Send + Sync + 'static,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
Inserts one row with the given options.
A is the SeaORM active model for E; U is the RepositoryOptions identity type.
Sourcepub async fn create_many<A, U>(
&self,
actives: Vec<A>,
opts: Option<RepositoryOptions<U>>,
) -> Result<Vec<E::ModelEx>, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::ModelEx: BaseEntity,
E::Model: IntoActiveModel<A>,
U: BaseEntity + Clone + Send + Sync + 'static,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
pub async fn create_many<A, U>(
&self,
actives: Vec<A>,
opts: Option<RepositoryOptions<U>>,
) -> Result<Vec<E::ModelEx>, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::ModelEx: BaseEntity,
E::Model: IntoActiveModel<A>,
U: BaseEntity + Clone + Send + Sync + 'static,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
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.
Sourcepub async fn create_many_simple<A>(
&self,
actives: Vec<A>,
) -> Result<Vec<E::ModelEx>, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::ModelEx: BaseEntity,
E::Model: IntoActiveModel<A>,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
pub async fn create_many_simple<A>(
&self,
actives: Vec<A>,
) -> Result<Vec<E::ModelEx>, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::ModelEx: BaseEntity,
E::Model: IntoActiveModel<A>,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
Inserts many rows with default options.
Sourcepub async fn create_many_with_opts<A, U>(
&self,
actives: Vec<A>,
opts: RepositoryOptions<U>,
) -> Result<Vec<E::ModelEx>, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::ModelEx: BaseEntity,
E::Model: IntoActiveModel<A>,
U: BaseEntity + Clone + Send + Sync + 'static,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
pub async fn create_many_with_opts<A, U>(
&self,
actives: Vec<A>,
opts: RepositoryOptions<U>,
) -> Result<Vec<E::ModelEx>, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::ModelEx: BaseEntity,
E::Model: IntoActiveModel<A>,
U: BaseEntity + Clone + Send + Sync + 'static,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
Inserts many rows with the given options.
Sourcepub async fn upsert_one<A>(&self, active: A) -> Result<E::ModelEx, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::Model: IntoActiveModel<A> + Clone,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
pub async fn upsert_one<A>(&self, active: A) -> Result<E::ModelEx, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::Model: IntoActiveModel<A> + Clone,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
Inserts one row with fresh base fields; does not check for an existing id.
Sourcepub async fn upsert_many<A>(
&self,
actives: Vec<A>,
) -> Result<Vec<E::ModelEx>, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::ModelEx: BaseEntity,
E::Model: IntoActiveModel<A> + Clone,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
pub async fn upsert_many<A>(
&self,
actives: Vec<A>,
) -> Result<Vec<E::ModelEx>, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::ModelEx: BaseEntity,
E::Model: IntoActiveModel<A> + Clone,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
Inserts each row via PersistentRepository::upsert_one, one at a time.
Sourcepub async fn upsert_one_with_id<A, F>(
&self,
active: A,
id_fn: F,
) -> Result<E::ModelEx, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::Model: IntoActiveModel<A> + Clone + BaseEntity,
F: Fn(&A) -> Option<i64> + Send,
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
pub async fn upsert_one_with_id<A, F>(
&self,
active: A,
id_fn: F,
) -> Result<E::ModelEx, DbErr>where
A: ActiveModelTrait<Entity = E> + Send + ActiveModelBehavior,
E::Model: IntoActiveModel<A> + Clone + BaseEntity,
F: Fn(&A) -> Option<i64> + Send,
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
Updates the row when id_fn yields an existing id, else inserts it.
Updates refresh updatedAt; inserts fill the full base field set.
Sourcepub 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,
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.
Sourcepub 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,
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,
Alias for PersistentRepository::update_many.
Sourcepub 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,
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.
Sourcepub async fn update_by_id<F>(
&self,
id: i64,
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::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
pub async fn update_by_id<F>(
&self,
id: i64,
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::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
Loads a row by id, applies effector to the model, and saves it when changed.
Returns None when the id does not exist.
Sourcepub async fn update_by_id_with_active<F>(
&self,
id: i64,
effector: F,
) -> Result<Option<E::ModelEx>, DbErr>where
F: FnMut(&mut E::ActiveModel) -> bool + Send,
E::ModelEx: Into<E::Model> + BaseEntity,
E::Model: IntoActiveModel<E::ActiveModel> + Clone,
E::ActiveModel: ActiveModelTrait<Entity = E> + Send,
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
pub async fn update_by_id_with_active<F>(
&self,
id: i64,
effector: F,
) -> Result<Option<E::ModelEx>, DbErr>where
F: FnMut(&mut E::ActiveModel) -> bool + Send,
E::ModelEx: Into<E::Model> + BaseEntity,
E::Model: IntoActiveModel<E::ActiveModel> + Clone,
E::ActiveModel: ActiveModelTrait<Entity = E> + Send,
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
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.
Sourcepub 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,
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.
Sourcepub async fn update_by_id_with_opts<F, U>(
&self,
id: i64,
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> + BaseEntity,
E::Model: IntoActiveModel<E::ActiveModel> + Clone,
E::ActiveModel: ActiveModelTrait<Entity = E> + Send,
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
pub async fn update_by_id_with_opts<F, U>(
&self,
id: i64,
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> + BaseEntity,
E::Model: IntoActiveModel<E::ActiveModel> + Clone,
E::ActiveModel: ActiveModelTrait<Entity = E> + Send,
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::Column: ColumnTrait + FromStr,
<E::Column as FromStr>::Err: Debug,
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.
Sourcepub async fn delete_many(
&self,
filter: Option<DeleteQueryData<E>>,
) -> Result<u64, DbErr>
pub async fn delete_many( &self, filter: Option<DeleteQueryData<E>>, ) -> Result<u64, DbErr>
Deletes rows matching the filter and returns the affected row count.
Sourcepub async fn delete_many_with_opts<U>(
&self,
filter: Option<DeleteQueryData<E>>,
opts: Option<RepositoryOptions<U>>,
) -> Result<u64, DbErr>
pub async fn delete_many_with_opts<U>( &self, filter: Option<DeleteQueryData<E>>, opts: Option<RepositoryOptions<U>>, ) -> Result<u64, DbErr>
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.
Sourcepub async fn remove_many(
&self,
filter: Option<DeleteQueryData<E>>,
) -> Result<u64, DbErr>
pub async fn remove_many( &self, filter: Option<DeleteQueryData<E>>, ) -> Result<u64, DbErr>
Alias for PersistentRepository::delete_many.
Sourcepub async fn remove_many_with_opts<U>(
&self,
filter: Option<DeleteQueryData<E>>,
opts: Option<RepositoryOptions<U>>,
) -> Result<u64, DbErr>
pub async fn remove_many_with_opts<U>( &self, filter: Option<DeleteQueryData<E>>, opts: Option<RepositoryOptions<U>>, ) -> Result<u64, DbErr>
Alias for PersistentRepository::delete_many_with_opts.
Sourcepub async fn delete_one(
&self,
filter: Option<DeleteQueryData<E>>,
) -> Result<bool, DbErr>
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.
Sourcepub async fn delete_one_with_opts<U>(
&self,
filter: Option<DeleteQueryData<E>>,
opts: Option<RepositoryOptions<U>>,
) -> Result<bool, DbErr>
pub async fn delete_one_with_opts<U>( &self, filter: Option<DeleteQueryData<E>>, opts: Option<RepositoryOptions<U>>, ) -> Result<bool, DbErr>
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.
Sourcepub async fn delete_by_id(&self, id: i64) -> Result<Option<E::ModelEx>, DbErr>where
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::ModelEx: BaseEntity,
pub async fn delete_by_id(&self, id: i64) -> Result<Option<E::ModelEx>, DbErr>where
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::ModelEx: BaseEntity,
Deletes the row with the given id and returns the deleted model, or None when absent.
Sourcepub async fn delete_by_id_with_opts<U>(
&self,
id: i64,
opts: Option<RepositoryOptions<U>>,
) -> Result<Option<E::ModelEx>, DbErr>where
U: BaseEntity + Clone + Send + Sync + 'static,
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::ModelEx: BaseEntity,
pub async fn delete_by_id_with_opts<U>(
&self,
id: i64,
opts: Option<RepositoryOptions<U>>,
) -> Result<Option<E::ModelEx>, DbErr>where
U: BaseEntity + Clone + Send + Sync + 'static,
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::ModelEx: BaseEntity,
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.
Sourcepub async fn delete_by_id_txn(
&self,
id: i64,
txn: &DatabaseTransaction,
) -> Result<Option<E::ModelEx>, DbErr>where
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::ModelEx: BaseEntity,
pub async fn delete_by_id_txn(
&self,
id: i64,
txn: &DatabaseTransaction,
) -> Result<Option<E::ModelEx>, DbErr>where
E::PrimaryKey: PrimaryKeyTrait,
<E::PrimaryKey as PrimaryKeyTrait>::ValueType: From<i64>,
E::ModelEx: BaseEntity,
Deletes the row with the given id inside the transaction, returning the deleted model if found.
Sourcepub async fn delete_many_txn(
&self,
filter: Option<DeleteQueryData<E>>,
txn: &DatabaseTransaction,
) -> Result<u64, DbErr>
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.
Sourcepub async fn delete_many_txn_with_opts<U>(
&self,
filter: Option<DeleteQueryData<E>>,
opts: Option<RepositoryOptions<U>>,
txn: &DatabaseTransaction,
) -> Result<u64, DbErr>
pub async fn delete_many_txn_with_opts<U>( &self, filter: Option<DeleteQueryData<E>>, opts: Option<RepositoryOptions<U>>, txn: &DatabaseTransaction, ) -> Result<u64, DbErr>
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>where
E: EntityTrait + Clone,
E::ModelEx: BaseEntity + Send + Sync,
<E::ModelEx as BaseEntity>::LoaderType: EntityLoaderTrait<E, ModelEx = E::ModelEx>,
impl<E> Clone for PersistentRepository<E>where
E: EntityTrait + Clone,
E::ModelEx: BaseEntity + Send + Sync,
<E::ModelEx as BaseEntity>::LoaderType: EntityLoaderTrait<E, ModelEx = E::ModelEx>,
Auto Trait Implementations§
impl<E> !RefUnwindSafe for PersistentRepository<E>
impl<E> !UnwindSafe for PersistentRepository<E>
impl<E> Freeze for PersistentRepository<E>
impl<E> Send for PersistentRepository<E>
impl<E> Sync for PersistentRepository<E>
impl<E> Unpin for PersistentRepository<E>
impl<E> UnsafeUnpin for PersistentRepository<E>
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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,
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