A CRUD library for Holochain zomes that implement the CAPS pattern (Chained, Action, Permalink, State-based)
For information on which versions of this package work for each Holochain release, see docs/HolochainVersionMap.md
Example of adding to Cargo.toml
toml
[dependencies]
hc_crud_caps = "0.1.0"
Example of common imports
rust
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 ```rust use hdk::prelude::*; use hccrud::{ now, createentity, getentity, getentities, updateentity, deleteentity, Entity, EntryModel, EntityType, entry_model, };
pub struct PostEntry {
pub title: String,
pub message: String,
pub publishedat: Option
pub enum EntryTypes { #[entry_def] Post(PostEntry), }
entry_model!( EntryTypes::Post( PostEntry ) ); ```
Example ```rust let input = PostEntry { title: String::from("Greeting"), message: String::from("Hello world!"), publishedat: Some(1633108520744), lastupdated: None, };
let postentity = createentity( &input )?; ```
Example
rust
let post_entity = get_entity( &entity.id )?;
Example
rust
let post_entity = update_entity( &entity.address, |mut previous: PostEntry, _| {
previous.message = String::from("Hello, world!");
previous.last_updated = Some( now()? );
Ok(previous)
})?;
Example
rust
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 ```rust
pub struct CommentEntry {
pub message: String,
pub publishedat: Option
Add CommentEntry
to EntryTypes
enum
```diff
#[hdkentrydefs]
#[unitenum(UnitEntryTypes)]
pub enum EntryTypes {
#[entrydef]
Post(PostEntry),
+ #[entry_def]
+ Comment(CommentEntry),
}
+entry_model!( EntryTypes::Comment( CommentEntry ) ); ```
Create a CommentEntry
and link it to the PostEntry
```rust
pub enum LinkTypes { Post, Comment, }
let input = CommentEntry { message: String::from("Where is the sun?"), publishedat: Some( now()? ), lastupdated: None, };
let commententity = createentity( &input )?;
commententity.linkfrom( &post_entity.id, LinkTypes::Comment, None )?; ```
Get a Collection
for a specific base and tag
rust
let collection : Vec<Entity<CommentEntry>> = get_entities( &post_entity.id, LinkTypes::Comment, None )?;
See CONTRIBUTING.md