Dodo
Dodo (pronounced doe doe
) is a very basic persistence library designed to be a quick and easy
way to create a persistent storage. It uses Serde under the hood to perform serialization of
the persisted data.
Getting Started
First, create a struct implementing the Entity, Serialize and Deserialize traits, which
can be done using derives. For the Entity
derive to work, it must contain a field named id
of type Option<Uuid>
.
# use dodo::prelude::*;
# use serde::{Deserialize, Serialize};
# use uuid::Uuid;
#[derive(Entity, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Person {
id: Option<Uuid>,
name: String,
age: u64,
}
Then, create a storage for your data. You can implement your own, or use the default Directory storage.
# use dodo::prelude::*;
#
# fn main() -> Result<(), Box<dyn std::error::Error>> {
# let tempdir = tempfile::tempdir()?;
# let path = tempdir.path();
let directory = Directory::new(path)?;
# Ok(())
# }
Now, you must choose a serializer. You can also implement your own, or use the default JsonSerializer. When this is done, it is highly recommanded to create a type definition for your repository using the chosen storage and serializer.
# use dodo::prelude::*;
# use serde::{Deserialize, Serialize};
# use uuid::Uuid;
#
# #[derive(Entity, Serialize, Deserialize)]
# #[serde(rename_all = "camelCase")]
# struct Person {
# id: Option<Uuid>,
# name: String,
# age: u64,
# }
#
type PersonRepository = Repository<Person, Directory, JsonSerializer>;
Finally, create a repository and start using it.
# use dodo::prelude::*;
# use serde::{Deserialize, Serialize};
# use uuid::Uuid;
#
# #[derive(Debug, Entity, Serialize, Deserialize)]
# #[serde(rename_all = "camelCase")]
# struct Person {
# id: Option<Uuid>,
# name: String,
# age: u64,
# }
#
# type PersonRepository = Repository<Person, Directory, JsonSerializer>;
#
# fn main() -> Result<(), Box<dyn std::error::Error>> {
# let tempdir = tempfile::tempdir()?;
# let path = tempdir.path();
# let directory = Directory::new(path)?;
let mut repository = PersonRepository::new(directory);
let mut person = Person { id: None, name: "John Smith".into(), age: 42 };
repository.insert(&mut person)?;
# Ok(())
# }
Example
This short example demonstrate how to use Dodo to read and write entities to the disk.
use std::error::Error;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use dodo::prelude::*;
#[derive(Debug, Entity, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Person {
id: Option<Uuid>,
name: String,
age: u64,
}
type PersonRepository = Repository<Person, Directory, JsonSerializer>;
fn main() -> Result<(), Box<dyn Error>> {
let path = tempfile::tempdir()?;
let mut person1 = Person { id: None, name: "John Smith".into(), age: 18 };
let mut person2 = Person { id: None, name: "Mary Smith".into(), age: 42 };
let mut repository = PersonRepository::new(Directory::new(&path)?);
repository.insert(&mut person1)?;
repository.insert(&mut person2)?;
println!("{:?}", repository.find_all()?.filter(|person| person.age > 20).collect()?);
Ok(())
}