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