summaryrefslogtreecommitdiff
path: root/server/src/play.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/play.rs')
-rw-r--r--server/src/play.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/server/src/play.rs b/server/src/play.rs
index 8fe1872..242bdb8 100644
--- a/server/src/play.rs
+++ b/server/src/play.rs
@@ -1,4 +1,6 @@
use async_trait::async_trait;
+use sqlx::{PgPool, query};
+use uuid::Uuid;
use crate::error::BridgeError;
@@ -11,6 +13,43 @@ pub trait Journal {
async fn replay(&mut self, seq: i64) -> Result<Vec<serde_json::Value>, BridgeError>;
}
+pub struct DbJournal {
+ db: PgPool,
+ id: Uuid,
+}
+
+impl DbJournal {
+ pub fn new(db: PgPool, id: Uuid) -> Self {
+ Self { db, id }
+ }
+}
+
+#[async_trait]
+impl Journal for DbJournal {
+ async fn append(&mut self,seq:i64,payload:serde_json::Value) -> Result<(),BridgeError> {
+ query!(
+ r#"
+ insert into object_journal (id, seq, payload)
+ values ($1, $2, $3)
+ "#,
+ self.id, seq, payload,
+ ).execute(&self.db).await?;
+ Ok(())
+ }
+
+ async fn replay(&mut self,seq:i64) -> Result<Vec<serde_json::Value>,BridgeError> {
+ let results = query!(
+ r#"
+ select payload from object_journal
+ where id = $1 and seq >= $2
+ order by seq
+ "#,
+ self.id, seq
+ ).fetch_all(&self.db).await?;
+ Ok(results.into_iter().map(|v| v.payload).collect())
+ }
+}
+
pub struct Table {}
#[cfg(test)]