Skip to content

Commit

Permalink
Expose option for setting ID function (#202)
Browse files Browse the repository at this point in the history
* Add ID format to `PgStore`

* Allow `PgStoreBuilder` to set ID format

* Support UUID version 7 directly
  • Loading branch information
tt authored Oct 30, 2024
1 parent 6cc4609 commit ce4fdf8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tokio = { version = "1.6", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
# Uuid generation
uuid = { version = "1.1", features = ["serde", "v4"] }
uuid = { version = "1.6", features = ["serde", "v4", "v7"] }
# Time esrs-core
chrono = { version = "0.4", features = ["serde"] }
# Build async trait
Expand Down
19 changes: 19 additions & 0 deletions src/store/postgres/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ use crate::Aggregate;
use super::persistable::Persistable;
use super::{PgStore, Schema};

/// The `UuidFormat` enum defines the UUID format preference:
///
/// - `V4`: Uses the random UUID version 4 as defined by RFC 9562 section 5.4.
/// - `V7`: Uses the time-ordered UUID version 7 as defined by RFC 9562 section 5.7.
pub enum UuidFormat {
V4,
V7,
}

/// Struct used to build a brand new [`PgStore`].
pub struct PgStoreBuilder<A, Schema = <A as Aggregate>::Event>
where
Expand All @@ -24,6 +33,7 @@ where
event_handlers: Vec<Box<dyn EventHandler<A> + Send>>,
transactional_event_handlers: Vec<Box<dyn TransactionalEventHandler<A, PgStoreError, PgConnection> + Send>>,
event_buses: Vec<Box<dyn EventBus<A> + Send>>,
event_id_format: UuidFormat,
run_migrations: bool,
_schema: PhantomData<Schema>,
}
Expand All @@ -40,6 +50,7 @@ where
event_handlers: vec![],
transactional_event_handlers: vec![],
event_buses: vec![],
event_id_format: UuidFormat::V4,
run_migrations: true,
_schema: PhantomData,
}
Expand Down Expand Up @@ -112,10 +123,17 @@ where
event_handlers: self.event_handlers,
transactional_event_handlers: self.transactional_event_handlers,
event_buses: self.event_buses,
event_id_format: self.event_id_format,
_schema: PhantomData,
}
}

/// Set the UUID format of event IDs.
pub fn with_event_id_format(mut self, event_id_format: UuidFormat) -> Self {
self.event_id_format = event_id_format;
self
}

/// This function runs all the needed [`Migrations`], atomically setting up the database if
/// `run_migrations` isn't explicitly set to false. [`Migrations`] should be run only at application
/// startup due to avoid performance issues.
Expand All @@ -137,6 +155,7 @@ where
event_handlers: RwLock::new(self.event_handlers),
transactional_event_handlers: self.transactional_event_handlers,
event_buses: self.event_buses,
event_id_format: self.event_id_format,
}),
_schema: self._schema,
})
Expand Down
7 changes: 6 additions & 1 deletion src/store/postgres/event_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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::postgres::UuidFormat;
use crate::store::{EventStore, EventStoreLockGuard, StoreEvent, UnlockOnDrop};
use crate::types::SequenceNumber;
use crate::{Aggregate, AggregateState};
Expand Down Expand Up @@ -58,6 +59,7 @@ where
pub(super) transactional_event_handlers:
Vec<Box<dyn TransactionalEventHandler<A, PgStoreError, PgConnection> + Send>>,
pub(super) event_buses: Vec<Box<dyn EventBus<A> + Send>>,
pub(super) event_id_format: UuidFormat,
}

impl<A, S> PgStore<A, S>
Expand Down Expand Up @@ -95,7 +97,10 @@ where
sequence_number: SequenceNumber,
executor: impl Executor<'_, Database = Postgres>,
) -> Result<StoreEvent<A::Event>, PgStoreError> {
let id: Uuid = Uuid::new_v4();
let id: Uuid = match self.inner.event_id_format {
UuidFormat::V4 => Uuid::new_v4(),
UuidFormat::V7 => Uuid::now_v7(),
};

#[cfg(feature = "upcasting")]
let version: Option<i32> = S::current_version();
Expand Down

0 comments on commit ce4fdf8

Please sign in to comment.