Expand description
Persistence Layer - Reusable Repository Implementations
This module provides generic, reusable repository implementations that can be used across all modules in the framework. Instead of writing custom repositories for each entity, modules can use these generic implementations.
§Available Implementations
InMemoryRepository- Thread-safe in-memory storage (testing, prototyping)PostgresRepository- PostgreSQL-backed storage (production, requires “postgres” feature)
§Usage Pattern
§In-Memory (testing/prototyping)
ⓘ
use backbone_core::persistence::{InMemoryRepository, PersistentEntity, CrudRepository};
#[derive(Clone, Debug, Serialize, Deserialize)]
struct User {
id: String,
name: String,
created_at: Option<DateTime<Utc>>,
updated_at: Option<DateTime<Utc>>,
deleted_at: Option<DateTime<Utc>>,
}
impl PersistentEntity for User { /* ... */ }
let repo = InMemoryRepository::<User>::new();
let user = repo.create(user).await?;§PostgreSQL (production)
ⓘ
use backbone_core::persistence::{PostgresRepository, PostgresEntity, PersistentEntity};
use sqlx::FromRow;
#[derive(Clone, Debug, Serialize, Deserialize, FromRow)]
struct User {
id: String,
name: String,
created_at: Option<DateTime<Utc>>,
updated_at: Option<DateTime<Utc>>,
deleted_at: Option<DateTime<Utc>>,
}
impl PersistentEntity for User { /* ... */ }
impl PostgresEntity for User {
fn table_name() -> &'static str { "users" }
fn select_columns() -> &'static [&'static str] {
&["id", "name", "created_at", "updated_at", "deleted_at"]
}
fn insert_columns() -> &'static [&'static str] {
&["id", "name", "created_at", "updated_at"]
}
fn bind_for_insert<'q>(entity: &Self, query: Query<'q, ...>) -> Query<'q, ...> {
query.bind(&entity.id).bind(&entity.name)
.bind(entity.created_at).bind(entity.updated_at)
}
}
let pool = PgPool::connect("...").await?;
let repo = PostgresRepository::<User>::new(pool);
let user = repo.create(user).await?;Re-exports§
pub use traits::CrudRepository;pub use traits::PartialUpdatable;pub use traits::PersistentEntity;pub use traits::RepositoryError;pub use traits::SearchableRepository;pub use traits::Versioned;pub use memory::InMemoryRepository;pub use adapter::AdapterError;pub use adapter::CrudServiceAdapter;pub use adapter::SearchableCrudServiceAdapter;pub use adapter::SimpleCrudServiceAdapter;