1
1
use std::time::SystemTime;
2

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

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

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

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

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

            
40
1
    println!(
41
1
        "Inserted message '{:?}' with id {}",
42
1
        message_doc.contents, message_doc.header.id
43
1
    );
44
1

            
45
1
    Ok(())
46
1
}
47

            
48
1
#[test]
49
1
fn runs() {
50
1
    main().unwrap()
51
1
}