Skip to main content

DataSourcePort

Trait DataSourcePort 

Source
pub trait DataSourcePort: Send + Sync {
    // Required methods
    fn query<'life0, 'async_trait>(
        &'life0 self,
        params: QueryParams,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Value>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn healthcheck<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn source_name(&self) -> &str;
}
Expand description

Port: query a database and return rows as JSON values.

Implementations connect to PostgreSQL, MySQL, SQLite, MongoDB, or any other datastore and return results as Vec<Value>.

§Example

use stygian_graph::ports::data_source::{DataSourcePort, QueryParams};
use stygian_graph::domain::error::Result;
use async_trait::async_trait;
use serde_json::{json, Value};

struct MockDb;

#[async_trait]
impl DataSourcePort for MockDb {
    async fn query(&self, params: QueryParams) -> Result<Vec<Value>> {
        Ok(vec![json!({"id": 1, "name": "test"})])
    }

    async fn healthcheck(&self) -> Result<()> {
        Ok(())
    }

    fn source_name(&self) -> &str {
        "mock-db"
    }
}

Required Methods§

Source

fn query<'life0, 'async_trait>( &'life0 self, params: QueryParams, ) -> Pin<Box<dyn Future<Output = Result<Vec<Value>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Execute a query and return results as JSON rows.

§Arguments
  • params - Query statement, bind parameters, and optional limit
§Returns
  • Ok(Vec<Value>) - Result rows serialised as JSON objects
  • Err(StygianError) - Query or connection error
§Example
let params = QueryParams {
    query: "SELECT 1 AS n".into(),
    parameters: vec![],
    limit: None,
};
let rows = db.query(params).await.unwrap();
Source

fn healthcheck<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Check that the underlying connection is alive.

§Example
db.healthcheck().await.unwrap();
Source

fn source_name(&self) -> &str

Human-readable name of this data source (e.g. "postgres", "sqlite").

§Example
println!("Connected to: {}", db.source_name());

Implementors§