core_crypto_keystore/traits/entity.rs
1use rusqlite::Connection;
2
3use crate::{
4 CryptoKeystoreResult,
5 traits::{
6 KeyType,
7 primary_key::{BorrowPrimaryKey, PrimaryKey},
8 },
9};
10
11/// Something which can be stored in our database.
12///
13/// It has a primary key, which uniquely identifies it.
14pub trait Entity: PrimaryKey + Sized {
15 /// The table name for this entity
16 const COLLECTION_NAME: &'static str;
17
18 /// Get an entity by its primary key.
19 ///
20 /// For entites whose primary key has a distinct borrowed type, it is best to implement this as a direct
21 /// passthrough:
22 ///
23 /// ```rust,ignore
24 /// fn get(conn: &Connection, key: &Self::PrimaryKey) -> CoreCryptoKeystoreResult<Option<Self>> {
25 /// Self::get_borrowed(conn, key).await
26 /// }
27 /// ```
28 fn get(conn: &Connection, key: &Self::PrimaryKey) -> CryptoKeystoreResult<Option<Self>>;
29
30 /// Count the number of entities of this type in the database.
31 fn count(conn: &Connection) -> CryptoKeystoreResult<u32>;
32
33 /// Retrieve all entities of this type from the database.
34 fn load_all(conn: &Connection) -> CryptoKeystoreResult<Vec<Self>>;
35}
36
37pub trait EntityGetBorrowed: Entity + BorrowPrimaryKey {
38 /// Get an entity by a borrowed form of its primary key.
39 fn get_borrowed(conn: &Connection, key: &Self::BorrowedPrimaryKey) -> CryptoKeystoreResult<Option<Self>>
40 where
41 for<'pk> &'pk Self::BorrowedPrimaryKey: KeyType;
42}