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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! Data model definitions for database storage.
//!
//! This module defines the core data structures used for storing and retrieving
//! information from the LMDB database. The primary model is [`LocalDbModel`],
//! which provides a flexible structure for storing arbitrary JSON data with
//! unique identifiers and content hashing.
use ;
use Value as JsonValue;
/// A flexible data model for storing structured information in the database.
///
/// `LocalDbModel` serves as the primary data container for all database operations.
/// It consists of a unique identifier, a content hash for integrity verification,
/// and arbitrary JSON data for application-specific information.
///
/// # Structure
///
/// - **id**: Unique identifier used as the database key
/// - **hash**: Content hash for data integrity and change detection
/// - **data**: Arbitrary JSON data containing the actual application data
///
/// # Examples
///
/// ## Creating a new model
///
/// ```rust
/// use offline_first_core::local_db_model::LocalDbModel;
/// use serde_json::json;
///
/// let model = LocalDbModel {
/// id: "user_12345".to_string(),
/// hash: "sha256_content_hash".to_string(),
/// data: json!({
/// "name": "John Doe",
/// "email": "john@example.com",
/// "age": 30,
/// "preferences": {
/// "theme": "dark",
/// "notifications": true
/// }
/// }),
/// };
/// ```
///
/// ## With complex nested data
///
/// ```rust
/// use offline_first_core::local_db_model::LocalDbModel;
/// use serde_json::json;
///
/// let model = LocalDbModel {
/// id: "document_001".to_string(),
/// hash: "doc_hash_abc123".to_string(),
/// data: json!({
/// "title": "Project Documentation",
/// "sections": [
/// {
/// "name": "Introduction",
/// "content": "This document describes..."
/// },
/// {
/// "name": "Architecture",
/// "content": "The system is designed..."
/// }
/// ],
/// "metadata": {
/// "created_at": "2024-01-15T10:30:00Z",
/// "author": "engineering_team",
/// "version": "1.0.0"
/// }
/// }),
/// };
/// ```
///
/// # Serialization
///
/// The model implements [`Serialize`] and [`Deserialize`] traits from serde,
/// enabling seamless JSON conversion for database storage and FFI operations.
///
/// ```rust
/// use offline_first_core::local_db_model::LocalDbModel;
/// use serde_json::json;
///
/// let model = LocalDbModel {
/// id: "test".to_string(),
/// hash: "test_hash".to_string(),
/// data: json!({"key": "value"}),
/// };
///
/// // Serialize to JSON string
/// let json_string = serde_json::to_string(&model)?;
/// println!("Serialized: {}", json_string);
///
/// // Deserialize from JSON string
/// let deserialized: LocalDbModel = serde_json::from_str(&json_string)?;
/// assert_eq!(model.id, deserialized.id);
/// # Ok::<(), serde_json::Error>(())
/// ```
///
/// # Clone Support
///
/// The model implements [`Clone`] for easy duplication when needed for
/// updates or transformations.
///
/// ```rust
/// use offline_first_core::local_db_model::LocalDbModel;
/// use serde_json::json;
///
/// let original = LocalDbModel {
/// id: "original".to_string(),
/// hash: "hash123".to_string(),
/// data: json!({"status": "active"}),
/// };
///
/// let mut updated = original.clone();
/// updated.hash = "new_hash456".to_string();
/// updated.data = json!({"status": "updated"});
/// ```
///
/// # Database Integration
///
/// This model is designed to work seamlessly with the database operations
/// provided by [`AppDbState`]:
///
/// ```no_run
/// use offline_first_core::{local_db_state::AppDbState, local_db_model::LocalDbModel};
/// use serde_json::json;
///
/// let db = AppDbState::init("my_app".to_string())?;
///
/// let model = LocalDbModel {
/// id: "settings_001".to_string(),
/// hash: "settings_hash".to_string(),
/// data: json!({
/// "theme": "dark",
/// "language": "en",
/// "auto_save": true
/// }),
/// };
///
/// // Store the model
/// db.push(model.clone())?;
///
/// // Retrieve it back
/// let retrieved = db.get_by_id("settings_001")?;
/// assert!(retrieved.is_some());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Field Constraints
///
/// ## ID Field
/// - Must be unique within the database
/// - Cannot be empty (LMDB limitation)
/// - Should be descriptive and meaningful to your application
/// - Common patterns: "user_id", "document_id", "config_name"
///
/// ## Hash Field
/// - Typically used for content integrity verification
/// - Can be any string, commonly SHA-256 hashes
/// - Useful for detecting changes in data
/// - Optional but recommended for data integrity
///
/// ## Data Field
/// - Accepts any valid JSON value
/// - Can contain objects, arrays, strings, numbers, booleans, or null
/// - Size limitations apply based on LMDB configuration
/// - Nested structures are fully supported