haste_repository/pg/
mod.rs1use haste_fhir_model::r4::generated::resources::Resource;
2use haste_fhir_operation_error::derive::OperationOutcomeError;
3use haste_jwt::VersionId;
4use moka::future::Cache;
5use sqlx::Postgres;
6use std::sync::Arc;
7use tokio::sync::Mutex;
8
9use crate::Repository;
10
11mod authorization_code;
12mod fhir;
13mod membership;
14mod migrate;
15mod project;
16mod scope;
17mod tenant;
18mod user;
19
20#[derive(OperationOutcomeError, Debug)]
21pub enum StoreError {
22 #[error(code = "invalid", diagnostic = "SQL Error occured.")]
23 SQLXError(#[from] sqlx::Error),
24 #[error(code = "exception", diagnostic = "Failed to create transaction.")]
25 TransactionError,
26 #[error(code = "invalid", diagnostic = "Cannot commit non transaction.")]
27 NotTransaction,
28 #[error(code = "invalid", diagnostic = "Failed to commit the transaction.")]
29 FailedCommitTransaction,
30}
31
32#[derive(Debug, Clone)]
34pub enum PGConnection {
35 Pool(sqlx::Pool<Postgres>, Cache<VersionId, Resource>),
36 Transaction(
37 Arc<Mutex<sqlx::Transaction<'static, Postgres>>>,
38 Cache<VersionId, Resource>,
39 ),
40}
41
42static TOTAL_CACHE_SIZE: u64 = 1000 * 10;
43
44impl PGConnection {
45 pub fn pool(pool: sqlx::Pool<Postgres>) -> Self {
46 PGConnection::Pool(pool, Cache::new(TOTAL_CACHE_SIZE))
47 }
48
49 pub fn cache(&self) -> &Cache<VersionId, Resource> {
50 match self {
51 PGConnection::Pool(_, cache) => cache,
52 PGConnection::Transaction(_, cache) => cache,
53 }
54 }
55}
56
57impl Repository for PGConnection {}