Intermediate: Update
Updating Entries
Updates on entries are another basic primitive that Holochain offers via the HDK to hApp developers. It consists of marking an old entry as updated, and creating a new one with updated content.
It's important to understand this: the DHT is an append only store, in which you can't really modify data. The only thing you can do is add new data that refers to old data in it.
So, when we update an entry, we are not modifying the contents of the original entry, we are creating a new entry and marking the old entry as updated by the new one.
This means that if we for example do a get
with the hash of the original entry, we are going to get the contens of the original entry, even if that entry has been updated.
So, let's see what updating an entry looks like:
use hdk::prelude::*;
#[hdk_entry(id = "post")]
struct Post(String);
entry_defs![Post::entry_def()];
#[hdk_extern]
fn create_post(post: Post) -> ExternResult<HeaderHash> {
create_entry(post)
}
#[hdk_extern]
fn update_post(old_header_address: HeaderHash, new_post: Post) -> ExternResult<HeaderHash> {
update_entry(old_header_address, new_post)
}
Do you see something interesting in the update_post
function? We are not really updating an entry, we are updating its header!
Here you can start to see the importance of Elements
, Headers
and HeaderHashes
: it's not enough to know the entry hash that we want to update, we need to know which header created that entry in order to update it.
Try it!
Here is a very simple interactive simulation of creating an update chain:
- Click in one of the nodes.
- Create an entry with arbritary content in
Call Zome Fns
. - Select its header in the
Entry Graph
, and see its contents inEntry Contents
.- Copy the hash of the header by clicking on the "Copy" button inside that
Entry Contents
while having the header selected.
- Copy the hash of the header by clicking on the "Copy" button inside that
- In
Call Zome Fns
, select theupdate_entry
function. - In the
original_header_address
field, paste the header that you copied, and innew_content
pass some arbritatry content as well. - See the
Entry Graph
that is created from these actions.
You can play around with update chains for a while. You can also try to update the same entry multiple times! What happens then?