Skip to content

Commit

Permalink
Update persistable docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryardley committed Nov 28, 2024
1 parent b974e47 commit 7a07fa6
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions packages/ciphernode/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,27 @@ This worked well especially for objects who's persistable state needs to be deri

Persistable is a struct that connects a repository and some in memory state and ensures that every time the in memory state is mutated that the state is saved to the repository.

```mermaid
graph TD
S[State]
R[Repository]
P[Persistable]
S --> P
R --> P
```

Aside from being less verbose it means we now have a centralized point at which we can implement batching should we need in the future and it means we should have less touch points for persistence in future.

```rust

// Some how we get a repository for a type
let repo:Repository<Vec<String>> = get_repo();

// We can use the sync_load to create a persistable object from the contents of the persistance layer that the repository encapsulates
let persistable:Persistable<Vec<String>> = repo.sync_load().await?;

// If we add a name to the list the list is automatically synced to the database
persistable.mutate(|&mut list| {
list.push("Fred");
list
});

// We can set a new object
persistable.set(vec![String::from("Hello")]);

// We can access properties of the underlying object using `with`
if persistable.with(false, |list| list.len() > 10) {
// do something
}
```

0 comments on commit 7a07fa6

Please sign in to comment.