1
use std::time::SystemTime;
2

            
3
use bonsaidb::core::schema::{Collection, SerializedCollection};
4
use bonsaidb::local::config::{Builder, StorageConfiguration};
5
use bonsaidb::local::Database;
6
use serde::{Deserialize, Serialize};
7

            
8
11
#[derive(Debug, Serialize, Deserialize, Collection)]
9
#[collection(name = "messages")]
10
struct Message {
11
    pub timestamp: SystemTime,
12
    pub contents: String,
13
}
14

            
15
1
fn main() -> Result<(), bonsaidb::core::Error> {
16
1
    let db = Database::open::<Message>(StorageConfiguration::new("basic.bonsaidb"))?;
17

            
18
    // Insert a new `Message` into the database. `Message` is a `Collection`
19
    // implementor, which makes them act in a similar fashion to tables in other
20
    // databases. BonsaiDb stores each "row" as a `Document`. This document
21
    // will have a unique ID, some other metadata, and your stored value. In
22
    // this case, `Message` implements `Serialize` and `Deserialize`, so we can
23
    // use convenience methods that return a `CollectionDocument`, moving all
24
    // needs of serialization behind the scenes.
25
1
    let document = Message {
26
1
        contents: String::from("Hello, World!"),
27
1
        timestamp: SystemTime::now(),
28
1
    }
29
1
    .push_into(&db)?;
30

            
31
    // Retrieve the message using the id returned from the previous call. both
32
    // `document` and `message_doc` should be identical.
33
1
    let message_doc =
34
1
        Message::get(&document.header.id, &db)?.expect("couldn't retrieve stored item");
35
1

            
36
1
    println!(
37
1
        "Inserted message '{:?}' with id {}",
38
1
        message_doc.contents, message_doc.header.id
39
1
    );
40
1

            
41
1
    Ok(())
42
1
}
43

            
44
1
#[test]
45
1
fn runs() {
46
1
    main().unwrap()
47
1
}