p2panda_store/traits.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use std::error::Error;
4
5/// Traits to implement database transaction provider.
6///
7/// To guard against sharing transactions unknowingly across unrelated database queries, a concept
8/// of a "permit" was introduced which does not protect from misuse but helps to make "holding" a
9/// transaction explicit.
10pub trait Transaction {
11 type Error: Error;
12
13 type Permit;
14
15 /// Begins a transaction.
16 fn begin(&self) -> impl Future<Output = Result<Self::Permit, Self::Error>>;
17
18 /// Rolls back the transaction and with that all uncommitted changes.
19 fn rollback(&self, permit: Self::Permit) -> impl Future<Output = Result<(), Self::Error>>;
20
21 /// Commits the transaction.
22 fn commit(&self, permit: Self::Permit) -> impl Future<Output = Result<(), Self::Error>>;
23}