Repository navigation
Expand file tree
/
Copy pathbasic-local.rs
More file actions
47 lines (39 loc) · 1.57 KB
/
Copy pathbasic-local.rs
File metadata and controls
47 lines (39 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::time::SystemTime;
use bonsaidb::core::schema::{Collection, SerializedCollection};
use bonsaidb::local::config::{Builder, StorageConfiguration};
use bonsaidb::local::Database;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Collection)]
#[collection(name = "messages")]
struct Message {
pub timestamp: SystemTime,
pub contents: String,
}
fn main() -> Result<(), bonsaidb::core::Error> {
let db = Database::open::<Message>(StorageConfiguration::new("basic.bonsaidb"))?;
// Insert a new `Message` into the database. `Message` is a `Collection`
// implementor, which makes them act in a similar fashion to tables in other
// databases. BonsaiDb stores each "row" as a `Document`. This document
// will have a unique ID, some other metadata, and your stored value. In
// this case, `Message` implements `Serialize` and `Deserialize`, so we can
// use convenience methods that return a `CollectionDocument`, moving all
// needs of serialization behind the scenes.
let document = Message {
contents: String::from("Hello, World!"),
timestamp: SystemTime::now(),
}
.push_into(&db)?;
// Retrieve the message using the id returned from the previous call. both
// `document` and `message_doc` should be identical.
let message_doc =
Message::get(&document.header.id, &db)?.expect("couldn't retrieve stored item");
println!(
"Inserted message '{:?}' with id {}",
message_doc.contents, message_doc.header.id
);
Ok(())
}
#[test]
fn runs() {
main().unwrap()
}