Skip to content

Commit

Permalink
Move persistable to postgres module
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnabell committed Apr 2, 2024
1 parent 0b165f8 commit 769d131
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/rebuilder/pg_rebuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use uuid::Uuid;
use crate::bus::EventBus;
use crate::handler::{ReplayableEventHandler, TransactionalEventHandler};
use crate::rebuilder::Rebuilder;
use crate::sql::event::Persistable;
use crate::store::postgres::persistable::Persistable;
use crate::store::postgres::{PgStore, PgStoreBuilder, PgStoreError, Schema};
use crate::store::{EventStore, StoreEvent};
use crate::Aggregate;
Expand Down
15 changes: 1 addition & 14 deletions src/sql/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,8 @@ use uuid::Uuid;

use crate::store::postgres::Schema;
use crate::store::StoreEvent;
use crate::store::postgres::persistable::Persistable;
use crate::types::SequenceNumber;
use serde::de::DeserializeOwned;
use serde::Serialize;

#[cfg(not(feature = "upcasting"))]
pub trait Persistable: Serialize + DeserializeOwned {}

#[cfg(not(feature = "upcasting"))]
impl<T> Persistable for T where T: Serialize + DeserializeOwned {}

#[cfg(feature = "upcasting")]
pub trait Persistable: Serialize + DeserializeOwned + crate::event::Upcaster {}

#[cfg(feature = "upcasting")]
impl<T> Persistable for T where T: Serialize + DeserializeOwned + crate::event::Upcaster {}

/// Event representation on the event store
#[derive(sqlx::FromRow, serde::Serialize, serde::Deserialize, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/store/postgres/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use tokio::sync::RwLock;

use crate::bus::EventBus;
use crate::handler::{EventHandler, TransactionalEventHandler};
use crate::sql::event::Persistable;
use crate::sql::migrations::{Migrations, MigrationsHandler};
use crate::sql::statements::{Statements, StatementsHandler};
use crate::store::postgres::{InnerPgStore, PgStoreError};
use crate::Aggregate;

use super::persistable::Persistable;
use super::{PgStore, Schema};

/// Struct used to build a brand new [`PgStore`].
Expand Down
3 changes: 2 additions & 1 deletion src/store/postgres/event_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use uuid::Uuid;

use crate::bus::EventBus;
use crate::handler::{EventHandler, TransactionalEventHandler};
use crate::sql::event::{DbEvent, Persistable};
use crate::sql::event::DbEvent;
use crate::sql::statements::{Statements, StatementsHandler};
use crate::store::postgres::persistable::Persistable;
use crate::store::postgres::PgStoreError;
use crate::store::postgres::Schema;
use crate::store::{EventStore, EventStoreLockGuard, StoreEvent, UnlockOnDrop};
Expand Down
1 change: 1 addition & 0 deletions src/store/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub use schema::*;

mod builder;
mod event_store;
pub mod persistable;
mod schema;

// Trait aliases are experimental. See issue #41517 <https://github.com/rust-lang/rust/issues/41517>
Expand Down
14 changes: 14 additions & 0 deletions src/store/postgres/persistable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use serde::de::DeserializeOwned;
use serde::Serialize;

#[cfg(not(feature = "upcasting"))]
pub trait Persistable: Serialize + DeserializeOwned {}

#[cfg(not(feature = "upcasting"))]
impl<T> Persistable for T where T: Serialize + DeserializeOwned {}

#[cfg(feature = "upcasting")]
pub trait Persistable: Serialize + DeserializeOwned + crate::event::Upcaster {}

#[cfg(feature = "upcasting")]
impl<T> Persistable for T where T: Serialize + DeserializeOwned + crate::event::Upcaster {}
3 changes: 2 additions & 1 deletion src/store/postgres/schema.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::sql::event::Persistable;
use super::persistable::Persistable;


/// To support decoupling between the [`crate::Aggregate::Event`] type and the schema of the DB table
/// in [`super::PgStore`] you can create a schema type that implements [`Persistable`] and [`Schema`]
Expand Down

0 comments on commit 769d131

Please sign in to comment.