Skip to main content

cinderblock_core/data_layer/
mod.rs

1use crate::Resource;
2
3pub mod in_memory;
4
5/// Persistence backend for a specific resource type.
6///
7/// The trait is parameterized on `R` so that different data layers can
8/// impose different bounds on the resources they support. For example,
9/// `InMemoryDataLayer` has a blanket impl for all `R: Resource`, while
10/// a SQLite data layer can additionally require `R: SqlResource`.
11pub trait DataLayer<R: Resource>: std::fmt::Debug + Send + Sync + 'static {
12    /// Persist a new resource and return the stored version.
13    ///
14    /// The returned resource may differ from the input when the data layer
15    /// provides server-generated values (e.g. autoincrement primary keys).
16    fn create(&self, resource: R) -> impl Future<Output = crate::Result<R>> + Send;
17
18    fn read(&self, primary_key: &R::PrimaryKey) -> impl Future<Output = crate::Result<R>> + Send;
19
20    fn update(&self, resource: R) -> impl Future<Output = crate::Result<()>> + Send;
21
22    /// Remove a resource by primary key, returning the deleted resource.
23    fn destroy(&self, primary_key: &R::PrimaryKey)
24    -> impl Future<Output = crate::Result<R>> + Send;
25}