Db

Struct Db 

Source
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

Source

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.

Source

pub async fn register_object(&self, value: &Value) -> Result<i32, Error>

Registers a single JSON object in the database.

§Arguments
  • value - The JSON value to register.
§Returns

A Result containing the ID (i32) or a tokio_postgres::Error.

Source

pub async fn register_batch_objects( &self, values: &[Value], ) -> Result<Vec<i32>, Error>

Registers a batch of JSON objects in the database.

§Arguments
  • values - A slice of JSON values to register.
§Returns

A Result containing a vector of IDs or a tokio_postgres::Error.

Source

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.

Source

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.

Source

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.

Source

pub fn queries_executed(&self) -> u64

Returns the total number of database queries executed.

§Returns

The total number of queries executed since instance creation.

Source

pub fn query_errors(&self) -> u64

Returns the total number of database query errors.

§Returns

The total number of failed queries since instance creation.

Auto Trait Implementations§

§

impl !Freeze for Db

§

impl !RefUnwindSafe for Db

§

impl Send for Db

§

impl Sync for Db

§

impl Unpin for Db

§

impl !UnwindSafe for Db

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more