Skip to content

Commit

Permalink
feat: return transaction for workspace database operation (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
khorshuheng authored Dec 5, 2024
1 parent e114e85 commit d862b40
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions collab-database/src/workspace_database/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl WorkspaceDatabase {
/// Create a new [DatabaseMeta] for the given database id and view id
/// use [Self::update_database] to attach more views to the existing database.
///
pub fn add_database(&mut self, database_id: &str, view_ids: Vec<String>) {
pub fn add_database(&mut self, database_id: &str, view_ids: Vec<String>) -> TransactionMut {
let mut txn = self.collab.transact_mut();
let linked_views: HashSet<String> = view_ids.into_iter().collect();
let record = DatabaseMeta {
Expand All @@ -65,6 +65,7 @@ impl WorkspaceDatabase {
linked_views: linked_views.into_iter().collect(),
};
self.body.push_back(&mut txn, record);
txn
}

pub fn batch_add_database(
Expand All @@ -85,17 +86,21 @@ impl WorkspaceDatabase {
}

/// Update the database by the given id
pub fn update_database(&mut self, database_id: &str, f: impl FnMut(&mut DatabaseMeta)) {
self
.body
.update_database(&mut self.collab.transact_mut(), database_id, f);
pub fn update_database(
&mut self,
database_id: &str,
f: impl FnMut(&mut DatabaseMeta),
) -> TransactionMut {
let mut txn = self.collab.transact_mut();
self.body.update_database(&mut txn, database_id, f);
txn
}

/// Delete the database by the given id
pub fn delete_database(&mut self, database_id: &str) {
self
.body
.delete_database(&mut self.collab.transact_mut(), database_id);
pub fn delete_database(&mut self, database_id: &str) -> TransactionMut {
let mut txn = self.collab.transact_mut();
self.body.delete_database(&mut txn, database_id);
txn
}

/// Test if the database with the given id exists
Expand Down

0 comments on commit d862b40

Please sign in to comment.