1use crate::prelude::*;
2
3#[derive(NativeClass)]
5#[inherit(Node)]
6pub struct GDDB {
7 storage: Database<Record>,
8}
9
10#[methods]
11impl GDDB {
12 fn new(_owner: &Node) -> Self {
13 let db: Database<Record> = Database::new("GAME", None, false);
14 Self { storage: db }
15 }
16
17 #[export]
19 pub fn create(&mut self, _owner: &Node, model: String, attributes: Dictionary) -> String {
20 let mut record = Record::new(model);
21 let uuid = record.uuid.clone();
22 record.attributes = attributes.to_json().to_string();
23
24 self.storage.create(record).unwrap();
25
26 uuid
27 }
28
29 #[export]
31 pub fn find(&mut self, _owner: &Node, uuid: String) -> GodotString {
32 let record = self
33 .storage
34 .find(|f| &f.uuid, uuid)
35 .expect("Could not find record");
36
37 let data = Dictionary::new();
38
39 data.insert("uuid", record.uuid.clone());
40 data.insert("model", record.model.clone());
41 data.insert("attributes", record.attributes.clone());
42
43 data.to_json()
44 }
45
46 #[export]
48 pub fn update(&mut self, _owner: &Node, uuid: String, model: String, attributes: String) {
49 let new = Record {
50 uuid,
51 model,
52 attributes,
53 };
54
55 let uuid = new.uuid.clone();
56 let original = self
57 .storage
58 .find(|f| &f.uuid, uuid)
59 .expect("Could not find record to update")
60 .clone();
61
62 self.storage
63 .update(&original, new.clone())
64 .expect("Cannot update record");
65 }
66
67 #[export]
69 pub fn destroy(&mut self, _owner: &Node, uuid: String, model: String, attributes: String) {
70 let record = Record {
71 uuid,
72 model,
73 attributes,
74 };
75
76 self.storage.destroy(&record).expect("Cannot remove record");
77 }
78}