Skip to main content

Module backends

Module backends 

Source
Expand description

Concrete storage backend implementations for the incremental update system.

This module provides database-specific implementations of the StorageBackend trait:

  • Postgres (postgres-backend feature): Full SQL backend for CLI deployment with connection pooling, prepared statements, and batch operations.
  • D1 (d1-backend feature): Cloudflare D1 backend for edge deployment via the Cloudflare REST API.
  • InMemory: Simple in-memory backend for testing (always available).

§Backend Factory Pattern

The create_backend factory function provides runtime backend selection based on deployment environment and feature flags:

use thread_flow::incremental::backends::{BackendType, BackendConfig, create_backend};

// CLI deployment with Postgres
let backend = create_backend(
    BackendType::Postgres,
    BackendConfig::Postgres {
        database_url: "postgresql://localhost/thread".to_string(),
    },
).await?;

// Edge deployment with D1
let backend = create_backend(
    BackendType::D1,
    BackendConfig::D1 {
        account_id: "your-account-id".to_string(),
        database_id: "your-db-id".to_string(),
        api_token: "your-token".to_string(),
    },
).await?;

// Testing with in-memory storage (always available)
let backend = create_backend(
    BackendType::InMemory,
    BackendConfig::InMemory,
).await?;

§Feature Gating

Backend availability depends on cargo features:

Attempting to use a disabled backend returns IncrementalError::UnsupportedBackend.

§Deployment Scenarios

§CLI Deployment (Postgres)

[dependencies]
thread-flow = { version = "*", features = ["postgres-backend"] }
use thread_flow::incremental::backends::{BackendType, BackendConfig, create_backend};

let backend = create_backend(
    BackendType::Postgres,
    BackendConfig::Postgres {
        database_url: std::env::var("DATABASE_URL")?,
    },
).await?;

§Edge Deployment (D1)

[dependencies]
thread-flow = { version = "*", features = ["d1-backend", "worker"] }
use thread_flow::incremental::backends::{BackendType, BackendConfig, create_backend};

let backend = create_backend(
    BackendType::D1,
    BackendConfig::D1 {
        account_id: std::env::var("CF_ACCOUNT_ID")?,
        database_id: std::env::var("CF_DATABASE_ID")?,
        api_token: std::env::var("CF_API_TOKEN")?,
    },
).await?;

§Testing (InMemory)

use thread_flow::incremental::backends::{BackendType, BackendConfig, create_backend};

let backend = create_backend(
    BackendType::InMemory,
    BackendConfig::InMemory,
).await?;

Re-exports§

pub use postgres::PostgresIncrementalBackend;

Modules§

postgres
PostgreSQL storage backend for the incremental update system.

Enums§

BackendConfig
Configuration for backend initialization.
BackendType
Backend type selector for runtime backend selection.
IncrementalError
Errors that can occur during backend initialization and operation.

Functions§

create_backend
Creates a storage backend based on the specified type and configuration.