Skip to content

brokkr-broker::dal

Rust

Data Access Layer (DAL) Module

This module provides an abstraction layer for database operations in the Brokkr Broker. It includes structures and methods for interacting with various data entities such as agents, stacks, deployment objects, and more.

Main Structures

  • DAL: The main Data Access Layer struct that provides access to all sub-DALs.
  • FilterType: An enum used for specifying filter types in queries.

Usage

To use the DAL in your code:

use brokkr_broker::dal::DAL;
use brokkr_broker::db::create_shared_connection_pool;
let pool = create_shared_connection_pool("database_url", "app_name", 5);
let dal = DAL::new(pool);
// Now you can use dal to access various data operations
let agents = dal.agents().list().expect("Failed to list agents");

pub

Derives: Clone

The main Data Access Layer struct.

This struct serves as the central point for database operations, managing a connection pool and providing access to specific DAL implementations for different entities.

Name Type Description
pool ConnectionPool A connection pool for PostgreSQL database connections with schema support.
auth_cache Option<Cache<String, AuthPayload>> In-memory cache for PAK authentication results, keyed by PAK hash.

pub

fn new(pool: ConnectionPool) -> Self

Creates a new DAL instance with the given connection pool.

Parameters:

Name Type Description
pool ConnectionPool A connection pool for PostgreSQL database connections with schema support.

Returns:

A new DAL instance.

Source
pub fn new(pool: ConnectionPool) -> Self {
DAL {
pool,
auth_cache: None,
}
}

pub

fn new_with_auth_cache(
pool: ConnectionPool,
auth_cache_ttl_seconds: u64,
) -> Self

Creates a new DAL instance with an auth cache.

Parameters:

Name Type Description
pool ConnectionPool A connection pool for PostgreSQL database connections with schema support.
auth_cache_ttl_seconds u64 TTL for cached auth results. 0 disables caching.
Source
pub fn new_with_auth_cache(pool: ConnectionPool, auth_cache_ttl_seconds: u64) -> Self {
let auth_cache = if auth_cache_ttl_seconds > 0 {
Some(
Cache::builder()
.time_to_live(Duration::from_secs(auth_cache_ttl_seconds))
.max_capacity(10_000)
.build(),
)
} else {
None
};
DAL { pool, auth_cache }
}

pub

fn conn(
&self,
) -> Result<
diesel::r2d2::PooledConnection<
diesel::r2d2::ConnectionManager<diesel::PgConnection>,
>,
diesel::result::Error,
>

Borrows a pooled database connection (with the schema search_path set).

Returns the r2d2 pool error mapped to a diesel::result::Error so DAL methods can ? it on their existing Result<_, diesel::result::Error> surface instead of panicking. There is no From<r2d2::Error> for the diesel error, so it is wrapped as an UnableToSendCommand database error. Pool exhaustion / DB outage thus becomes a normal 500 result (via ApiError::from_diesel) rather than an unwind caught by the outer CatchPanicLayer.

Source
pub fn conn(
&self,
) -> Result<
diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>,
diesel::result::Error,
> {
self.pool.get().map_err(|e| {
diesel::result::Error::DatabaseError(
diesel::result::DatabaseErrorKind::UnableToSendCommand,
Box::new(format!("failed to get DB connection from pool: {e}")),
)
})
}

pub

fn invalidate_auth_cache(&self, pak_hash: &str)

Invalidates a specific entry in the auth cache by PAK hash.

Source
pub fn invalidate_auth_cache(&self, pak_hash: &str) {
if let Some(cache) = &self.auth_cache {
cache.invalidate(pak_hash);
}
}

pub

fn invalidate_all_auth_cache(&self)

Invalidates all entries in the auth cache.

Source
pub fn invalidate_all_auth_cache(&self) {
if let Some(cache) = &self.auth_cache {
cache.invalidate_all();
}
}

pub

fn agents(&self) -> AgentsDAL<'_>

Provides access to the Agents Data Access Layer.

Returns:

An instance of AgentsDAL.

Source
pub fn agents(&self) -> AgentsDAL<'_> {
AgentsDAL { dal: self }
}

pub

fn agent_annotations(&self) -> AgentAnnotationsDAL<'_>

Provides access to the Agent Annotations Data Access Layer.

Returns:

An instance of AgentAnontationsDAL.

Source
pub fn agent_annotations(&self) -> AgentAnnotationsDAL<'_> {
AgentAnnotationsDAL { dal: self }
}

pub

fn agent_events(&self) -> AgentEventsDAL<'_>

Provides access to the Agent Events Data Access Layer.

Returns:

An instance of AgentEventsDAL.

Source
pub fn agent_events(&self) -> AgentEventsDAL<'_> {
AgentEventsDAL { dal: self }
}

pub

fn agent_k8s_events(&self) -> AgentK8sEventsDAL<'_>

Provides access to the agent kube-Events telemetry buffer (agent_k8s_events). Bounded by the 6h retention ceiling enforced in ws::eviction.

Source
pub fn agent_k8s_events(&self) -> AgentK8sEventsDAL<'_> {
AgentK8sEventsDAL { dal: self }
}

pub

fn agent_pod_logs(&self) -> AgentPodLogsDAL<'_>

Provides access to the agent pod-logs telemetry buffer (agent_pod_logs). Bounded by the 6h retention ceiling enforced in ws::eviction.

Source
pub fn agent_pod_logs(&self) -> AgentPodLogsDAL<'_> {
AgentPodLogsDAL { dal: self }
}

pub

fn agent_labels(&self) -> AgentLabelsDAL<'_>

Provides access to the Agent Labels Data Access Layer.

Returns:

An instance of AgentLabelsDAL.

Source
pub fn agent_labels(&self) -> AgentLabelsDAL<'_> {
AgentLabelsDAL { dal: self }
}

pub

fn agent_targets(&self) -> AgentTargetsDAL<'_>

Provides access to the Agent Targets Data Access Layer.

Returns:

An instance of AgentTargetssDAL.

Source
pub fn agent_targets(&self) -> AgentTargetsDAL<'_> {
AgentTargetsDAL { dal: self }
}

pub

fn agent_generator_registrations(
&self,
) -> AgentGeneratorRegistrationsDAL<'_>

Provides access to the Agent Generator Registrations Data Access Layer.

Source
pub fn agent_generator_registrations(&self) -> AgentGeneratorRegistrationsDAL<'_> {
AgentGeneratorRegistrationsDAL { dal: self }
}

pub

fn stack_labels(&self) -> StackLabelsDAL<'_>

Provides access to the Stack Labels Data Access Layer.

Returns:

An instance of StackLabelsDAL.

Source
pub fn stack_labels(&self) -> StackLabelsDAL<'_> {
StackLabelsDAL { dal: self }
}

pub

fn stack_annotations(&self) -> StackAnnotationsDAL<'_>

Provides access to the Stack Annotations Data Access Layer.

Returns:

An instance of StackAnontationsDAL.

Source
pub fn stack_annotations(&self) -> StackAnnotationsDAL<'_> {
StackAnnotationsDAL { dal: self }
}

pub

fn stacks(&self) -> StacksDAL<'_>

Provides access to the Stacks Data Access Layer.

Returns:

An instance of StacksDAL.

Source
pub fn stacks(&self) -> StacksDAL<'_> {
StacksDAL { dal: self }
}

pub

fn deployment_health(&self) -> DeploymentHealthDAL<'_>

Provides access to the Deployment Health Data Access Layer.

Returns:

An instance of DeploymentHealthDAL.

Source
pub fn deployment_health(&self) -> DeploymentHealthDAL<'_> {
DeploymentHealthDAL { dal: self }
}

pub

fn deployment_objects(&self) -> DeploymentObjectsDAL<'_>

Provides access to the Deployment Objects Data Access Layer.

Returns:

An instance of DeploymentObjectsDAL.

Source
pub fn deployment_objects(&self) -> DeploymentObjectsDAL<'_> {
DeploymentObjectsDAL { dal: self }
}

pub

fn generators(&self) -> GeneratorsDAL<'_>

Provides access to the Generators Data Access Layer.

Returns:

An instance of GeneratorsDal.

Source
pub fn generators(&self) -> GeneratorsDAL<'_> {
GeneratorsDAL { dal: self }
}

pub

fn templates(&self) -> TemplatesDAL<'_>

Provides access to the Templates Data Access Layer.

Returns:

An instance of TemplatesDAL.

Source
pub fn templates(&self) -> TemplatesDAL<'_> {
TemplatesDAL { dal: self }
}

pub

fn template_labels(&self) -> TemplateLabelsDAL<'_>

Provides access to the Template Labels Data Access Layer.

Returns:

An instance of TemplateLabelsDAL.

Source
pub fn template_labels(&self) -> TemplateLabelsDAL<'_> {
TemplateLabelsDAL { dal: self }
}

pub

fn template_annotations(&self) -> TemplateAnnotationsDAL<'_>

Provides access to the Template Annotations Data Access Layer.

Returns:

An instance of TemplateAnnotationsDAL.

Source
pub fn template_annotations(&self) -> TemplateAnnotationsDAL<'_> {
TemplateAnnotationsDAL { dal: self }
}

pub

fn template_targets(&self) -> TemplateTargetsDAL<'_>

Provides access to the Template Targets Data Access Layer.

Returns:

An instance of TemplateTargetsDAL.

Source
pub fn template_targets(&self) -> TemplateTargetsDAL<'_> {
TemplateTargetsDAL { dal: self }
}

pub

fn rendered_deployment_objects(
&self,
) -> RenderedDeploymentObjectsDAL<'_>

Provides access to the Rendered Deployment Objects Data Access Layer.

Returns:

An instance of RenderedDeploymentObjectsDAL.

Source
pub fn rendered_deployment_objects(&self) -> RenderedDeploymentObjectsDAL<'_> {
RenderedDeploymentObjectsDAL { dal: self }
}

pub

fn work_orders(&self) -> WorkOrdersDAL<'_>

Provides access to the Work Orders Data Access Layer.

Returns:

An instance of WorkOrdersDAL.

Source
pub fn work_orders(&self) -> WorkOrdersDAL<'_> {
WorkOrdersDAL { dal: self }
}

pub

fn diagnostic_requests(&self) -> DiagnosticRequestsDAL<'_>

Provides access to the Diagnostic Requests Data Access Layer.

Returns:

An instance of DiagnosticRequestsDAL.

Source
pub fn diagnostic_requests(&self) -> DiagnosticRequestsDAL<'_> {
DiagnosticRequestsDAL { dal: self }
}

pub

fn diagnostic_results(&self) -> DiagnosticResultsDAL<'_>

Provides access to the Diagnostic Results Data Access Layer.

Returns:

An instance of DiagnosticResultsDAL.

Source
pub fn diagnostic_results(&self) -> DiagnosticResultsDAL<'_> {
DiagnosticResultsDAL { dal: self }
}

pub

fn webhook_subscriptions(&self) -> WebhookSubscriptionsDAL<'_>

Provides access to the Webhook Subscriptions Data Access Layer.

Returns:

An instance of WebhookSubscriptionsDAL.

Source
pub fn webhook_subscriptions(&self) -> WebhookSubscriptionsDAL<'_> {
WebhookSubscriptionsDAL { dal: self }
}

pub

fn webhook_deliveries(&self) -> WebhookDeliveriesDAL<'_>

Provides access to the Webhook Deliveries Data Access Layer.

Returns:

An instance of WebhookDeliveriesDAL.

Source
pub fn webhook_deliveries(&self) -> WebhookDeliveriesDAL<'_> {
WebhookDeliveriesDAL { dal: self }
}

pub

fn audit_logs(&self) -> AuditLogsDAL<'_>

Provides access to the Audit Logs Data Access Layer.

Returns:

An instance of AuditLogsDAL.

Source
pub fn audit_logs(&self) -> AuditLogsDAL<'_> {
AuditLogsDAL { dal: self }
}

pub

Error types for DAL operations.

  • ConnectionPool - Failed to get a connection from the pool
  • Query - Database query error
  • NotFound - Resource not found

pub

  • And
  • Or