pub struct Db { /* private fields */ }Expand description
Handles database interactions for registering JSON objects.
This struct manages the connection pool and executes SQL queries to insert or retrieve JSON objects. It uses optimized queries to handle concurrency and minimize round-trips.
Implementations§
Source§impl Db
impl Db
Sourcepub async fn new(
connection_string: &str,
table_name: &str,
id_column: &str,
jsonb_column: &str,
pool_size: u32,
acquire_timeout_secs: Option<u64>,
idle_timeout_secs: Option<u64>,
max_lifetime_secs: Option<u64>,
use_tls: Option<bool>,
) -> Result<Self, JsonRegisterError>
pub async fn new( connection_string: &str, table_name: &str, id_column: &str, jsonb_column: &str, pool_size: u32, acquire_timeout_secs: Option<u64>, idle_timeout_secs: Option<u64>, max_lifetime_secs: Option<u64>, use_tls: Option<bool>, ) -> Result<Self, JsonRegisterError>
Creates a new Db instance.
§Arguments
connection_string- The PostgreSQL connection string.table_name- The name of the table.id_column- The name of the ID column.jsonb_column- The name of the JSONB column.pool_size- The maximum number of connections in the pool.acquire_timeout_secs- Optional timeout for acquiring connections (default: 5s).idle_timeout_secs- Optional timeout for idle connections (default: 600s).max_lifetime_secs- Optional maximum lifetime for connections (default: 1800s).use_tls- Optional flag to enable TLS (default: false for backwards compatibility).
§Returns
A Result containing the new Db instance or a JsonRegisterError.
Sourcepub fn pool_size(&self) -> usize
pub fn pool_size(&self) -> usize
Returns the current size of the connection pool.
This is the total number of connections (both idle and active) currently in the pool. Useful for monitoring pool utilization.
§Returns
The number of connections in the pool.
Sourcepub fn idle_connections(&self) -> usize
pub fn idle_connections(&self) -> usize
Returns the number of idle connections in the pool.
Idle connections are available for immediate use. A low idle count during high load may indicate the pool is undersized.
§Returns
The number of idle connections.
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Checks if the connection pool is closed.
A closed pool cannot create new connections and will error on acquire attempts.
§Returns
true if the pool is closed, false otherwise.
Sourcepub fn queries_executed(&self) -> u64
pub fn queries_executed(&self) -> u64
Returns the total number of database queries executed.
§Returns
The total number of queries executed since instance creation.
Sourcepub fn query_errors(&self) -> u64
pub fn query_errors(&self) -> u64
Returns the total number of database query errors.
§Returns
The total number of failed queries since instance creation.