Intermediate: Anchors
What is an anchor?
You can think of anchors as an entry that every user knows the hash of. As the name implies, it acts as an 'anchor' with which we can link other entries to in order to create queries for things every user will need to access, for example, every user or post in the system.
Try it!
Here you can try out a system where every post that is created gets linked to an anchor for posts.
entry_defs![
Anchor::entry_def(),
...
];
Exercise
For this exercise we will be implementing the above example.
create_post
: creates a post and attaches it to an anchor for posts.
- Define an anchor with
anchor
type =ALL_POSTS
and text =ALL_POSTS
. This will already internally create the anchor for you. - Create the post entry, and calculate its hash.
- Create a link from the anchor entry to the post entry.
get_all_posts
queries the links of the posts anchor and returns all the posts.
- Calculate the anchor entry hash.
- Get the links from the anchor entry to all the posts.
- For every target in these links, do a
get
to retrieve the posts.
- Check if you are still inside the nix-shell.
Your terminal should look similar to this
[nix-shell:~/path-to-workspace/developer-exercises/path-to-exercise]$
- Implement
create_post
,get_all_posts
. - Compile and test your code:
cd tests && npm test
. - Don't stop until the tests run green.