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 rate_limit;
17mod scope;
18mod sequence;
19mod system;
20mod tenant;
21mod user;
22mod utilities;
23
24#[derive(OperationOutcomeError, Debug)]
25pub enum StoreError {
26 #[error(code = "duplicate", diagnostic = "Resource already exists.")]
27 Duplicate,
28 #[error(code = "not-found", diagnostic = "Resource not found.")]
29 NotFound,
30 #[error(code = "invalid", diagnostic = "SQL Error occured.")]
31 SQLXError(#[from] sqlx::Error),
32 #[error(code = "exception", diagnostic = "Failed to create transaction.")]
33 TransactionError,
34 #[error(code = "invalid", diagnostic = "Cannot commit non transaction.")]
35 NotTransaction,
36 #[error(code = "invalid", diagnostic = "Failed to commit the transaction.")]
37 FailedCommitTransaction,
38}
39
40#[derive(Debug, Clone)]
42pub enum PGConnection {
43 Pool(sqlx::Pool<Postgres>, Cache<VersionId, Resource>),
44 Transaction(
45 Arc<Mutex<sqlx::Transaction<'static, Postgres>>>,
46 Cache<VersionId, Resource>,
47 ),
48}
49
50static TOTAL_CACHE_SIZE: u64 = 1000 * 10;
51
52impl PGConnection {
53 pub fn pool(pool: sqlx::Pool<Postgres>) -> Self {
54 PGConnection::Pool(pool, Cache::new(TOTAL_CACHE_SIZE))
55 }
56
57 pub fn cache(&self) -> &Cache<VersionId, Resource> {
58 match self {
59 PGConnection::Pool(_, cache) => cache,
60 PGConnection::Transaction(_, cache) => cache,
61 }
62 }
63}
64
65impl Repository for PGConnection {}