helios_persistence/backends/postgres/mod.rs
1//! PostgreSQL backend implementation.
2//!
3//! This module provides a complete PostgreSQL implementation of all storage traits.
4//! It supports connection pooling via deadpool-postgres, JSONB storage for resources,
5//! native TIMESTAMPTZ for timestamps, and PostgreSQL full-text search.
6//!
7//! # Features
8//!
9//! - Connection pooling with deadpool-postgres
10//! - Full CRUD operations with tenant isolation
11//! - Version history tracking
12//! - Search support (string, token, date, reference, quantity, composite)
13//! - Full-text search using tsvector/tsquery
14//! - Transaction support with configurable isolation levels
15//! - Pessimistic locking with SELECT ... FOR UPDATE
16//!
17//! # Example
18//!
19//! ```no_run
20//! use helios_persistence::backends::postgres::{PostgresBackend, PostgresConfig};
21//! use helios_persistence::tenant::{TenantContext, TenantId, TenantPermissions};
22//!
23//! # async fn main_example() -> Result<(), Box<dyn std::error::Error>> {
24//! // Create a PostgreSQL backend
25//! let config = PostgresConfig::default();
26//! let backend = PostgresBackend::new(config).await?;
27//!
28//! // Initialize the schema
29//! backend.init_schema().await?;
30//!
31//! // Create a tenant context
32//! let tenant = TenantContext::new(
33//! TenantId::new("acme"),
34//! TenantPermissions::full_access(),
35//! );
36//!
37//! // Now you can use the backend for CRUD operations
38//! # Ok(())
39//! # }
40//! ```
41//!
42//! # Schema
43//!
44//! The PostgreSQL backend uses the following core schema:
45//!
46//! ```sql
47//! -- Main resource table
48//! CREATE TABLE IF NOT EXISTS resources (
49//! tenant_id TEXT NOT NULL,
50//! resource_type TEXT NOT NULL,
51//! id TEXT NOT NULL,
52//! version_id TEXT NOT NULL,
53//! data JSONB NOT NULL,
54//! last_updated TIMESTAMPTZ NOT NULL,
55//! is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
56//! deleted_at TIMESTAMPTZ,
57//! fhir_version TEXT NOT NULL DEFAULT '4.0',
58//! PRIMARY KEY (tenant_id, resource_type, id)
59//! );
60//!
61//! -- Version history table
62//! CREATE TABLE IF NOT EXISTS resource_history (
63//! tenant_id TEXT NOT NULL,
64//! resource_type TEXT NOT NULL,
65//! id TEXT NOT NULL,
66//! version_id TEXT NOT NULL,
67//! data JSONB NOT NULL,
68//! last_updated TIMESTAMPTZ NOT NULL,
69//! is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
70//! fhir_version TEXT NOT NULL DEFAULT '4.0',
71//! PRIMARY KEY (tenant_id, resource_type, id, version_id)
72//! );
73//! ```
74
75mod backend;
76mod bulk_export;
77mod bulk_submit;
78pub(crate) mod schema;
79pub mod search;
80mod search_impl;
81mod storage;
82mod transaction;
83
84pub use backend::{PostgresBackend, PostgresConfig};