junobuild_satellite/db/
types.rs

1pub mod state {
2    use crate::db::types::config::DbConfig;
3    use crate::{DelDoc, SetDoc};
4    use candid::CandidType;
5    use ic_stable_structures::StableBTreeMap;
6    use junobuild_collections::types::core::CollectionKey;
7    use junobuild_collections::types::rules::Rules;
8    use junobuild_shared::rate::types::RateTokenStore;
9    use junobuild_shared::types::core::{Blob, Key};
10    use junobuild_shared::types::memory::Memory;
11    use junobuild_shared::types::state::{Timestamp, UserId, Version};
12    use serde::{Deserialize, Serialize};
13    use std::collections::{BTreeMap, HashMap};
14
15    pub type Collection = BTreeMap<Key, Doc>;
16    pub type DbHeap = HashMap<CollectionKey, Collection>;
17
18    pub type DbStable = StableBTreeMap<StableKey, Doc, Memory>;
19
20    #[derive(CandidType, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
21    pub struct StableKey {
22        pub collection: CollectionKey,
23        pub key: Key,
24    }
25
26    #[derive(CandidType, Serialize, Deserialize, Clone)]
27    pub struct DbHeapState {
28        pub db: DbHeap,
29        pub rules: Rules,
30        pub config: Option<DbConfig>,
31    }
32
33    #[derive(Default, Clone)]
34    pub struct DbRuntimeState {
35        pub rate_tokens: RateTokenStore,
36    }
37
38    /// Represents a document in a collection's store.
39    ///
40    /// This struct defines the structure of a document stored in a collection. It includes the following fields:
41    /// - `owner`: The `UserId` representing the owner of the document.
42    /// - `data`: A `Blob` containing the document's data.
43    /// - `description`: An optional `String` providing additional document description, limited to 1024 characters.
44    /// - `created_at`: A `u64` timestamp for the document's creation.
45    /// - `updated_at`: A `u64` timestamp for the document's last update.
46    /// - `version`: A `u64` number for the document's version. The field is optional for backwards compatibility but, will be populated to 1 on the first create or update.
47    ///
48    /// This struct is used to store and manage documents within a collection's store.
49    #[derive(CandidType, Serialize, Deserialize, Clone)]
50    pub struct Doc {
51        pub owner: UserId,
52        pub data: Blob,
53        pub description: Option<String>,
54        pub created_at: Timestamp,
55        pub updated_at: Timestamp,
56        pub version: Option<Version>,
57    }
58
59    #[derive(CandidType, Serialize, Deserialize, Clone)]
60    pub struct DocContext<T> {
61        pub collection: CollectionKey,
62        pub key: Key,
63        pub data: T,
64    }
65
66    #[derive(CandidType, Serialize, Deserialize, Clone)]
67    pub struct DocUpsert {
68        pub before: Option<Doc>,
69        pub after: Doc,
70    }
71
72    #[derive(CandidType, Serialize, Deserialize, Clone)]
73    pub struct DocAssertSet {
74        pub current: Option<Doc>,
75        pub proposed: SetDoc,
76    }
77
78    #[derive(CandidType, Serialize, Deserialize, Clone)]
79    pub struct DocAssertDelete {
80        pub current: Option<Doc>,
81        pub proposed: DelDoc,
82    }
83}
84
85pub mod config {
86    use candid::{CandidType, Deserialize};
87    use junobuild_shared::types::config::ConfigMaxMemorySize;
88    use junobuild_shared::types::state::{Timestamp, Version};
89    use serde::Serialize;
90
91    pub type DbConfigMaxMemorySize = ConfigMaxMemorySize;
92
93    #[derive(Default, CandidType, Serialize, Deserialize, Clone)]
94    pub struct DbConfig {
95        pub max_memory_size: Option<DbConfigMaxMemorySize>,
96        pub version: Option<Version>,
97        pub created_at: Option<Timestamp>,
98        pub updated_at: Option<Timestamp>,
99    }
100}
101
102pub mod interface {
103    use crate::db::types::config::DbConfigMaxMemorySize;
104    use candid::CandidType;
105    use junobuild_shared::types::core::Blob;
106    use junobuild_shared::types::state::Version;
107    use serde::{Deserialize, Serialize};
108
109    /// Parameters for setting a document.
110    ///
111    /// This struct, `SetDoc`, is used to specify the parameters for setting or updating a document in a collection's store.
112    /// It includes the following fields:
113    /// - `data`: A `Blob` containing the new data for the document.
114    /// - `description`: An optional `String` providing additional description for the document. This field is optional.
115    /// - `version`: An optional `u64` version representing the last version of the document to ensure
116    ///   update consistency. This field is optional - i.e. first time a document is saved, it can be left empty but following updates require the current version to be passed.
117    ///
118    /// `SetDoc` is used to provide parameters for setting or updating a document in the collection's store.
119    #[derive(Default, CandidType, Serialize, Deserialize, Clone)]
120    pub struct SetDoc {
121        pub data: Blob,
122        pub description: Option<String>,
123        pub version: Option<Version>,
124    }
125
126    /// Parameters for deleting a document.
127    ///
128    /// This struct, `DelDoc`, is used to specify the parameters for deleting a document from a collection's store.
129    /// It includes the following field:
130    /// - `version`: An optional `u64` version representing the last version of the document to ensure
131    ///   deletion consistency. This field is optional.
132    ///
133    /// `DelDoc` is used to provide deletion parameters when removing a document.
134    #[derive(Default, CandidType, Serialize, Deserialize, Clone)]
135    pub struct DelDoc {
136        pub version: Option<Version>,
137    }
138
139    /// Parameters for setting the datastore configuration.
140    ///
141    /// This struct includes the following fields:
142    /// - `max_memory_size`: An optional `DbConfigMaxMemorySize` representing the maximum memory size allowed for the datastore.
143    /// - `version`: An optional `Version` used for version control to ensure update consistency. If specified, it must match the current configuration version to apply the update.
144    ///
145    /// `SetDbConfig` ensures that configuration updates are applied in a consistent and controlled manner.
146    #[derive(Default, CandidType, Serialize, Deserialize, Clone)]
147    pub struct SetDbConfig {
148        pub max_memory_size: Option<DbConfigMaxMemorySize>,
149        pub version: Option<Version>,
150    }
151}
152
153pub mod store {
154    pub struct AssertSetDocOptions {
155        pub with_assert_rate: bool,
156    }
157}