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 #[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 #[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 #[derive(Default, CandidType, Serialize, Deserialize, Clone)]
135 pub struct DelDoc {
136 pub version: Option<Version>,
137 }
138
139 #[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}