Crates.io | hc_crud_ceps |
lib.rs | hc_crud_ceps |
version | 0.80.0 |
source | src |
created_at | 2021-09-27 20:43:07.582834 |
updated_at | 2023-07-20 16:12:48.769882 |
description | A CRUD library for Holochain zomes that implement the CEPS pattern (Chained, Entry, Permalink, State-based) |
homepage | |
repository | https://github.com/mjbrisebois/rust-hc-crud-ceps |
max_upload_size | |
id | 457164 |
size | 122,415 |
A CRUD library for Holochain zomes that implement the CEPS pattern (Chained, Entry, Permalink, State-based)
For information on which versions of this package work for each Holochain release, see docs/Holochain_Version_Map.md
Example of adding to Cargo.toml
[dependencies]
hc_crud_ceps = "0.3.0"
Example of common imports
use hc_crud::{
now,
create_entity, get_entity, get_entities, update_entity, delete_entity,
Entity, EntryModel, EntityType,
};
These imports and structs are assumed for all examples
use hdk::prelude::*;
use hc_crud::{
now,
create_entity, get_entity, get_entities, update_entity, delete_entity,
Entity, EntryModel, EntityType,
};
#[hdk_entry_helper]
#[derive(Clone)]
pub struct PostEntry {
pub title: String,
pub message: String,
pub published_at: Option<u64>,
pub last_updated: Option<u64>,
}
#[hdk_entry_defs]
#[unit_enum(UnitEntryTypes)]
pub enum EntryTypes {
#[entry_def]
Post(PostEntry),
}
impl EntryModel<EntryTypes> for PostEntry {
fn name() -> &'static str { "Post" }
fn get_type(&self) -> EntityType {
EntityType::new( "post", "entry" )
}
fn to_input(&self) -> EntryTypes {
EntryTypes::Post(self.clone())
}
}
Example
let input = PostEntry {
title: String::from("Greeting"),
message: String::from("Hello world!"),
published_at: Some(1633108520744),
last_updated: None,
};
let post_entity = create_entity( &input )?;
Example
let post_entity = get_entity( &entity.id )?;
Example
let post_entity = update_entity( &entity.address, |mut previous: PostEntry, _| {
previous.message = String::from("Hello, world!");
previous.last_updated = Some( now()? );
Ok(previous)
})?;
Example
delete_entity::<PostEntry,EntryTypes>( &entity.id )?;
Create a 1-to-many relationship for post entries to have comment entries.
The following examples use this additional struct
#[hdk_entry_helper]
#[derive(Clone)]
pub struct CommentEntry {
pub message: String,
pub published_at: Option<u64>,
pub last_updated: Option<u64>,
}
impl EntryModel<EntryTypes> for CommentEntry {
fn name() -> &'static str { "Comment" }
fn get_type(&self) -> EntityType {
EntityType::new( "comment", "entry" )
}
fn to_input(&self) -> EntryTypes {
EntryTypes::Comment(self.clone())
}
}
Add CommentEntry
to EntryTypes
enum
#[hdk_entry_defs]
#[unit_enum(UnitEntryTypes)]
pub enum EntryTypes {
#[entry_def]
Post(PostEntry),
+ #[entry_def]
+ Comment(CommentEntry),
}
Create a CommentEntry
and link it to the PostEntry
#[hdk_link_types]
pub enum LinkTypes {
Post,
Comment,
}
let input = CommentEntry {
message: String::from("Where is the sun?"),
published_at: Some( now()? ),
last_updated: None,
};
let comment_entity = create_entity( &input )?;
comment_entity.link_from( &post_entity.id, LinkTypes::Comment, None )?;
Get a Collection
for a specific base and tag
let collection : Vec<Entity<CommentEntry>> = get_entities( &post_entity.id, LinkTypes::Comment, None )?;
See CONTRIBUTING.md