-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathprimary-keys.rs
More file actions
91 lines (81 loc) · 2.99 KB
/
Copy pathprimary-keys.rs
File metadata and controls
91 lines (81 loc) · 2.99 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use bonsaidb::core::key::{Key, NextValueError};
use bonsaidb::core::schema::{Collection, Schema, SerializedCollection};
use bonsaidb::local::config::{Builder, StorageConfiguration};
use bonsaidb::local::Database;
use serde::{Deserialize, Serialize};
#[derive(Debug, Schema)]
#[schema(name = "primary-keys", collections = [UserProfile, AssociatedProfileData])]
struct ExampleSchema;
// ANCHOR: derive_with_natural_id
#[derive(Debug, Serialize, Deserialize, Collection, Eq, PartialEq)]
#[collection(name = "user-profiles", primary_key = u32)]
struct UserProfile {
#[natural_id]
pub external_id: u32,
pub name: String,
}
// ANCHOR_END: derive_with_natural_id
// ANCHOR: derive
#[derive(Debug, Serialize, Deserialize, Collection, Eq, PartialEq)]
#[collection(name = "multi-key", primary_key = AssociatedProfileKey)]
struct AssociatedProfileData {
value: String,
}
#[derive(Key, Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
struct AssociatedProfileKey {
pub user_id: u32,
pub data_id: u64,
}
// ANCHOR_END: derive
fn main() -> Result<(), bonsaidb::core::Error> {
drop(std::fs::remove_dir_all("primary-keys.bonsaidb"));
let db = Database::open::<ExampleSchema>(StorageConfiguration::new("primary-keys.bonsaidb"))?;
// It's not uncommon to need to store data in a database that has an
// "external" identifier. Some examples could be externally authenticated
// user profiles, social networking site posts, or for normalizing a single
// type's fields across multiple Collections.
//
// The UserProfile type in this example has a `u32` primary key, and it
// defines a closure that returns the field `external_id`. When pushing a
// value into a collection with a natural id, it will automatically become
// the primary key:
// ANCHOR: natural_id_query
let user = UserProfile {
external_id: 42,
name: String::from("ecton"),
}
.push_into(&db)?;
let retrieved_from_database = UserProfile::get(&42, &db)?.expect("document not found");
assert_eq!(user, retrieved_from_database);
// ANCHOR_END: natural_id_query
// It's possible to use any type that implements the Key trait as a primary
// key, including tuples:
// ANCHOR: query
let key = AssociatedProfileKey {
user_id: user.header.id,
data_id: 64,
};
let inserted = AssociatedProfileData {
value: String::from("hello"),
}
.insert_into(&key, &db)?;
let retrieved = AssociatedProfileData::get(&key, &db)?.expect("document not found");
assert_eq!(inserted, retrieved);
// ANCHOR_END: query
// Not all primary key types support automatic key assignment -- the Key
// trait implementation controls that behavior:
assert!(matches!(
AssociatedProfileData {
value: String::from("error"),
}
.push_into(&db)
.unwrap_err()
.error,
bonsaidb::core::Error::DocumentPush(_, NextValueError::Unsupported)
));
Ok(())
}
#[test]
fn runs() {
main().unwrap()
}