use mongodb::bson::doc; use mongodb::options::IndexOptions; use mongodb::{Client, Collection, IndexModel}; use compliance_core::models::*; use crate::error::AgentError; #[derive(Clone, Debug)] pub struct Database { inner: mongodb::Database, } impl Database { pub async fn connect(uri: &str, db_name: &str) -> Result { let client = Client::with_uri_str(uri).await?; let db = client.database(db_name); db.run_command(doc! { "ping": 1 }).await?; tracing::info!("Connected to MongoDB database '{db_name}'"); Ok(Self { inner: db }) } pub async fn ensure_indexes(&self) -> Result<(), AgentError> { // repositories: unique git_url self.repositories() .create_index( IndexModel::builder() .keys(doc! { "git_url": 1 }) .options(IndexOptions::builder().unique(true).build()) .build(), ) .await?; // findings: unique fingerprint self.findings() .create_index( IndexModel::builder() .keys(doc! { "fingerprint": 1 }) .options(IndexOptions::builder().unique(true).build()) .build(), ) .await?; // findings: repo_id + severity compound self.findings() .create_index( IndexModel::builder() .keys(doc! { "repo_id": 1, "severity": 1 }) .build(), ) .await?; // scan_runs: repo_id + started_at descending self.scan_runs() .create_index( IndexModel::builder() .keys(doc! { "repo_id": 1, "started_at": -1 }) .build(), ) .await?; // sbom_entries: compound self.sbom_entries() .create_index( IndexModel::builder() .keys(doc! { "repo_id": 1, "name": 1, "version": 1 }) .build(), ) .await?; // cve_alerts: unique cve_id + repo_id self.cve_alerts() .create_index( IndexModel::builder() .keys(doc! { "cve_id": 1, "repo_id": 1 }) .options(IndexOptions::builder().unique(true).build()) .build(), ) .await?; // tracker_issues: unique finding_id self.tracker_issues() .create_index( IndexModel::builder() .keys(doc! { "finding_id": 1 }) .options(IndexOptions::builder().unique(true).build()) .build(), ) .await?; tracing::info!("Database indexes ensured"); Ok(()) } pub fn repositories(&self) -> Collection { self.inner.collection("repositories") } pub fn findings(&self) -> Collection { self.inner.collection("findings") } pub fn scan_runs(&self) -> Collection { self.inner.collection("scan_runs") } pub fn sbom_entries(&self) -> Collection { self.inner.collection("sbom_entries") } pub fn cve_alerts(&self) -> Collection { self.inner.collection("cve_alerts") } pub fn tracker_issues(&self) -> Collection { self.inner.collection("tracker_issues") } #[allow(dead_code)] pub fn raw_collection(&self, name: &str) -> Collection { self.inner.collection(name) } }