Macro hdk_crud::crud

source ·
macro_rules! crud {
    (
      $crud_type:ident, $entry_types:ident, $entry_type:expr, $link_types:ident, $link_type:expr, $i:ident, $path:expr, $get_peers:ident, $signal_type:ident
    ) => { ... };
}
Expand description

A macro to go quick and easy from having just a Holochain entry definition to having a full create-read-update-delete set of functionality in your Zome, “signals” (events), as well as time based indexing and queries. See example for a comprehensive look at how this works.

use hdk::prelude::*;
use hdk_crud::*;
 
#[hdk_entry_helper]
#[derive(Clone, PartialEq)]
pub struct Example {
    pub number: i32,
}
 
#[hdk_entry_defs]
#[unit_enum(UnitEntryTypes)]
#[derive(Clone)]
pub enum EntryTypes {
    #[entry_def(required_validations = 5)]
    Example(Example),
}
 
#[hdk_link_types]
pub enum LinkTypes {
    All,
}
 
// TestSignal pops out of the crud! macro
#[derive(Debug, Serialize, Deserialize, SerializedBytes)]
#[serde(untagged)]
pub enum SignalTypes {
    Example(ActionSignal<Example>),
}
impl From<ActionSignal<Example>> for SignalTypes {
    fn from(value: ActionSignal<Example>) -> Self {
        SignalTypes::Example(value)
    }
}

pub fn recv_remote_signal(signal: ExternIO) -> ExternResult<()> {
    Ok(emit_signal(&signal)?)
}

pub fn get_peers() -> ExternResult<Vec<AgentPubKey>> {
    Ok(Vec::new())
}

crud!(
  Example,
  EntryTypes,
  EntryTypes::Example,
  LinkTypes,
  LinkTypes::All,
  example,
  "example",
  get_peers,
  SignalTypes
);