-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
basic-local-multidb.rs
74 lines (64 loc) · 1.82 KB
/
basic-local-multidb.rs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::time::SystemTime;
use bonsaidb::{
core::{
connection::{Connection, StorageConnection},
schema::{
Collection, CollectionName, DefaultSerialization, Schematic, SerializedCollection,
},
Error,
},
local::{
config::{Builder, StorageConfiguration},
Storage,
},
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct Message {
pub timestamp: SystemTime,
pub contents: String,
}
impl Collection for Message {
fn collection_name() -> CollectionName {
CollectionName::private("messages")
}
fn define_views(_schema: &mut Schematic) -> Result<(), Error> {
Ok(())
}
}
impl DefaultSerialization for Message {}
#[tokio::main]
async fn main() -> Result<(), bonsaidb::core::Error> {
let storage =
Storage::open(StorageConfiguration::new("basic.bonsaidb").with_schema::<Message>()?)
.await?;
// Before you can create a database, you must register the schema you're
// wanting to use.
storage.create_database::<Message>("messages", true).await?;
let messages = storage.database::<Message>("messages").await?;
storage
.create_database::<Message>("private-messages", true)
.await?;
let private_messages = storage.database::<Message>("private-messages").await?;
insert_a_message(&messages, "Hello, World!").await?;
insert_a_message(&private_messages, "Hey!").await?;
Ok(())
}
// ANCHOR: reusable-code
async fn insert_a_message<C: Connection>(
connection: &C,
value: &str,
) -> Result<(), bonsaidb::core::Error> {
Message {
contents: String::from(value),
timestamp: SystemTime::now(),
}
.push_into(connection)
.await?;
Ok(())
}
// ANCHOR_END: reusable-code
#[test]
fn runs() {
main().unwrap()
}