Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Links, paths, and anchors #453

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d1bbb0a
add glossary entry for addressable content
pdaoust May 24, 2024
a8870fd
new links content
pdaoust Jun 6, 2024
34847bc
add links page to nav
pdaoust Jun 6, 2024
c404706
modifications to entries page to make the format fit better with links
pdaoust Jun 6, 2024
047e9ac
add pubkey to dictionary
pdaoust Jun 6, 2024
9b29a8c
fix broken links
pdaoust Jun 6, 2024
7a81f50
small text edit on links page
pdaoust Jun 7, 2024
7123b93
small content edits
pdaoust Sep 27, 2024
41470c5
refine warnings about depending on hashes
pdaoust Sep 27, 2024
5397386
fix code samples for anchors
pdaoust Sep 27, 2024
abcfc67
fix erroneous link tag size
pdaoust Sep 27, 2024
7296c0f
add identifiers to nav
pdaoust Sep 30, 2024
866ba85
small edits to links build guide
pdaoust Nov 12, 2024
b12d049
large changes to identifiers build guide
pdaoust Nov 12, 2024
8064b6c
add words to dictionary
pdaoust Nov 12, 2024
e9820c9
add links to community path libraries
pdaoust Nov 13, 2024
40cc54a
improve/consistent-ify navigation in build guide
pdaoust Nov 13, 2024
adebfaf
Merge branch 'main' into feat/guide/links
pdaoust Nov 22, 2024
9f79f3e
fix broken link
pdaoust Nov 22, 2024
a885aaf
fix language re: 'dead' and 'entry creation' actions
pdaoust Nov 22, 2024
6441164
nailing down some challenging language re: action hashes and graph DHT
pdaoust Nov 28, 2024
4803bb1
suppress spelling messages on hashes
pdaoust Nov 28, 2024
cd3451c
fix spelling mistake
pdaoust Nov 28, 2024
9792f1c
table styling
pdaoust Nov 28, 2024
3e6a315
final sweep for incorrect or very confusing things
pdaoust Nov 28, 2024
e3be354
fix broken link
pdaoust Nov 28, 2024
f6d07d4
experiment: remove cspell directives from code blocks
pdaoust Nov 28, 2024
b4366e8
change count_links bandwidth note
pdaoust Dec 16, 2024
fd08910
gitignore a local hApp folder for testing build guide code
pdaoust Dec 19, 2024
c1dda73
add Director entry type
pdaoust Dec 19, 2024
00b92d3
prose/code improvements
pdaoust Dec 19, 2024
e13d99b
unbreak/simplify code in build guide, update for 0.4
pdaoust Dec 20, 2024
cf17a98
Merge branch 'feat/guide/links' of github.com:holochain/docs-pages in…
pdaoust Dec 20, 2024
95a8f1c
Apply suggestions from code review
pdaoust Jan 2, 2025
322b58e
fix inconsistent indentation in code
pdaoust Jan 2, 2025
f2378c8
more nicer timestamp construction
pdaoust Jan 2, 2025
e7d6cc6
Merge branch 'feat/guide/links' of github.com:holochain/docs-pages in…
pdaoust Jan 2, 2025
8fa57d5
private data is encrypted
pdaoust Jan 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell/custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Kleppmann
NixOS
nixpkgs
pkgs
pubkey
QUIC
rustc
rustflags
Expand Down
1 change: 1 addition & 0 deletions src/pages/_data/navigation/mainNav.json5
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
{ title: "Build", url: "/build/", children: [
{ title: "Working with Data", url: "/build/working-with-data/", children: [
{ title: "Entries", url: "/build/entries/" },
{ title: "Links, Paths, and Anchors", url: "/build/links-paths-and-anchors/" },
]},
]
},
Expand Down
40 changes: 22 additions & 18 deletions src/pages/build/entries.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ An entry is always paired with an **entry creation action** that tells you who a

The pairing of an entry and the action that created it is called a **record**, which is the basic unit of data in a Holochain application.

## Scaffold an entry type and CRUD API

The Holochain dev tool command `hc scaffold entry-type <entry_type>` generates the code for a simple entry type and a CRUD API. It presents an interface that lets you define a struct and its fields, then asks you to choose whether to implement update and delete functions for it along with the default create and read functions.

## Define an entry type

Each entry has a **type**, which your application code uses to make sense of the entry's bytes. Our [HDI library](https://docs.rs/hdi/latest/hdi/) gives you macros to automatically define, serialize, and deserialize entry types to and from any Rust struct or enum that [`serde`](https://docs.rs/serde/latest/serde/) can handle.
Each entry has a **type**. This lets your application make sense of what would otherwise be a blob of arbitrary bytes. Our [HDI library](https://docs.rs/hdi/latest/hdi/) gives you macros to automatically define, serialize, and deserialize typed entries to and from any Rust struct or enum that [`serde`](https://docs.rs/serde/latest/serde/) can handle.

Entry types are defined in an [**integrity zome**](/resources/glossary/#integrity-zome). To define an [`EntryType`](https://docs.rs/hdi/latest/hdi/prelude/enum.EntryType.html), use the [`hdi::prelude::hdk_entry_helper`](https://docs.rs/hdi/latest/hdi/prelude/attr.hdk_entry_helper.html) macro on your Rust type:

Expand All @@ -27,7 +31,7 @@ use hdi::prelude::*;
#[hdk_entry_helper]
pub struct Movie {
title: String,
director: String,
director_hash: EntryHash,
imdb_id: Option<String>,
release_date: Timestamp,
box_office_revenue: u128,
Expand All @@ -42,13 +46,18 @@ In order to dispatch validation to the proper integrity zome, Holochain needs to
use hdi::prelude::*;

#[hdk_entry_defs]
#[unit_enum(UnitEntryTypes)]
enum EntryTypes {
Movie(Movie),
// other types...
}
```

### Configuring an entry type
This also gives you an enum that you can use later when you're storing app data. This is important because, under the hood, an entry type consists of two bytes --- an integrity zome index and an entry def index. These are required whenever you want to write an entry. Instead of having to remember those values every time you store something, your coordinator zome can just import and use this enum, which already knows how to convert each entry type to the right IDs.

The code sample above also uses a macro called `unit_enum`, which will generate an enum of all the entry types' names _without_ places to store values.

### Configure an entry type

Each variant in the enum should hold the Rust type that corresponds to it, and is implicitly marked with an `entry_def` proc macro which, if you specify it explicitly, lets you configure the given entry type further:

Expand All @@ -59,6 +68,7 @@ Each variant in the enum should hold the Rust type that corresponds to it, and i
use hdi::prelude::*;

#[hdk_entry_defs]
#[unit_enum(UnitEntryTypes)]
enum EntryTypes {
#[entry_def(required_validations = 7, )]
Movie(Movie),
Expand All @@ -72,13 +82,11 @@ enum EntryTypes {
}
```

This also gives you an enum that you can use later when you're storing app data. This is important because, under the hood, an entry type consists of two bytes -- an integrity zome index and an entry def index. These are required whenever you want to write an entry. Instead of having to remember those values every time you store something, your coordinator zome can just import and use this enum, which already knows how to convert each entry type to the right IDs.

## Create an entry

Most of the time you'll want to define your create, read, update, and delete (CRUD) functions in a [**coordinator zome**](/resources/glossary/#coordinator-zome) rather than the integrity zome that defines it. This is because a coordinator zome is easier to update in the wild than an integrity zome.

Create an entry by calling [`hdk::prelude::create_entry`](https://docs.rs/hdk/latest/hdk/prelude/fn.create_entry.html). If you used `hdk_entry_helper` and `hdk_entry_defs` macro in your integrity zome (see [Define an entry type](#define-an-entry-type)), you can use the entry types enum you defined, and the entry will be serialized and have the correct integrity zome and entry type indexes added to it.
Create an entry by calling [`hdk::prelude::create_entry`](https://docs.rs/hdk/latest/hdk/entry/fn.create_entry.html). If you used `hdk_entry_helper` and `hdk_entry_defs` macro in your integrity zome (see [Define an entry type](#define-an-entry-type)), you can use the entry types enum you defined, and the entry will be serialized and have the correct integrity zome and entry type indexes added to it.

```rust
use hdk::prelude::*;
Expand All @@ -88,7 +96,7 @@ use movie_integrity::*;

let movie = Movie {
title: "The Good, the Bad, and the Ugly",
director: "Sergio Leone"
director_hash: EntryHash::from_raw_36(vec![ /* hash of 'Sergio Leone' entry */ ]),
mattyg marked this conversation as resolved.
Show resolved Hide resolved
imdb_id: Some("tt0060196"),
release_date: Timestamp::from(Date::Utc("1966-12-23")),
box_office_revenue: 389_000_000,
Expand Down Expand Up @@ -374,26 +382,22 @@ match maybe_details {
}
```

## Scaffolding an entry type and CRUD API

The Holochain dev tool command `hc scaffold entry-type <entry_type>` generates the code for a simple entry type and a CRUD API. It presents an interface that lets you define a struct and its fields, then asks you to choose whether to implement update and delete functions for it along with the default create and read functions.

## Community CRUD libraries

If the scaffolder doesn't support your desired functionality, or is too low-level, there are some community-maintained libraries that offer opinionated and high-level ways to work with entries. Some of them also offer permissions management.
There are some community-maintained libraries that offer opinionated and high-level ways to work with entries. Some of them also offer permissions management.

* [rust-hc-crud-caps](https://github.com/spartan-holochain-counsel/rust-hc-crud-caps)
* [hdk_crud](https://github.com/lightningrodlabs/hdk_crud)
* [hc-cooperative-content](https://github.com/mjbrisebois/hc-cooperative-content)

## Reference

* [hdi::prelude::hdk_entry_helper](https://docs.rs/hdi/latest/hdi/attr.hdk_entry_helper.html)
* [hdi::prelude::hdk_entry_defs](https://docs.rs/hdi/latest/hdi/prelude/attr.hdk_entry_defs.html)
* [hdi::prelude::entry_def](https://docs.rs/hdi/latest/hdi/prelude/entry_def/index.html)
* [hdk::prelude::create_entry](https://docs.rs/hdk/latest/hdk/entry/fn.create_entry.html)
* [hdk::prelude::update_entry](https://docs.rs/hdk/latest/hdk/entry/fn.update_entry.html)
* [hdi::prelude::delete_entry](https://docs.rs/hdk/latest/hdk/entry/fn.delete_entry.html)
* [`hdi::prelude::hdk_entry_helper`](https://docs.rs/hdi/latest/hdi/attr.hdk_entry_helper.html)
* [`hdi::prelude::hdk_entry_defs`](https://docs.rs/hdi/latest/hdi/prelude/attr.hdk_entry_defs.html)
* [`hdi::prelude::entry_def`](https://docs.rs/hdi/latest/hdi/prelude/entry_def/index.html)
* [`hdk::prelude::create_entry`](https://docs.rs/hdk/latest/hdk/entry/fn.create_entry.html)
* [`hdk::prelude::update_entry`](https://docs.rs/hdk/latest/hdk/entry/fn.update_entry.html)
* [`hdi::prelude::delete_entry`](https://docs.rs/hdk/latest/hdk/entry/fn.delete_entry.html)

## Further reading

Expand Down
1 change: 1 addition & 0 deletions src/pages/build/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ This Build Guide organizes everything you need to know about developing Holochai

* [Overview](/build/working-with-data/) --- general concepts related to working with data in Holochain
* [Entries](/build/entries/) --- creating, reading, updating, and deleting
* [Links, Paths, and Anchors](/build/links-paths-and-anchors/) --- creating and deleting
:::
Loading