heliosdb-nano 3.23.2

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
Documentation
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! Supabase Realtime Compatible API
//!
//! WebSocket-based realtime subscription API compatible with Supabase Realtime.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Realtime message types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "event", content = "payload")]
pub enum RealtimeMessage {
    /// Phoenix-style phx_join
    #[serde(rename = "phx_join")]
    Join(JoinPayload),

    /// Phoenix-style phx_leave
    #[serde(rename = "phx_leave")]
    Leave,

    /// Phoenix-style phx_reply
    #[serde(rename = "phx_reply")]
    Reply(ReplyPayload),

    /// Heartbeat (ping)
    #[serde(rename = "heartbeat")]
    Heartbeat,

    /// Presence state
    #[serde(rename = "presence_state")]
    PresenceState(PresenceStatePayload),

    /// Presence diff
    #[serde(rename = "presence_diff")]
    PresenceDiff(PresenceDiffPayload),

    /// Broadcast
    #[serde(rename = "broadcast")]
    Broadcast(BroadcastPayload),

    /// Postgres changes
    #[serde(rename = "postgres_changes")]
    PostgresChanges(PostgresChangesPayload),

    /// System event
    #[serde(rename = "system")]
    System(SystemPayload),
}

/// Join channel payload
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JoinPayload {
    pub config: ChannelConfig,
}

/// Channel configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChannelConfig {
    /// Broadcast configuration
    pub broadcast: Option<BroadcastConfig>,
    /// Presence configuration
    pub presence: Option<PresenceConfig>,
    /// Postgres changes configuration
    pub postgres_changes: Option<Vec<PostgresChangeConfig>>,
}

/// Broadcast configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BroadcastConfig {
    /// Receive own broadcasts
    #[serde(rename = "self")]
    pub self_broadcast: Option<bool>,
    /// Acknowledge broadcasts
    pub ack: Option<bool>,
}

/// Presence configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PresenceConfig {
    /// Presence key (usually user ID)
    pub key: Option<String>,
}

/// Postgres changes subscription config
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PostgresChangeConfig {
    /// Event type: INSERT, UPDATE, DELETE, *
    pub event: String,
    /// Schema name
    pub schema: String,
    /// Table name
    pub table: Option<String>,
    /// Filter expression
    pub filter: Option<String>,
}

/// Reply payload
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplyPayload {
    pub status: String,
    pub response: serde_json::Value,
}

/// Presence state payload
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PresenceStatePayload {
    #[serde(flatten)]
    pub presences: HashMap<String, Vec<PresenceMeta>>,
}

/// Presence metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PresenceMeta {
    pub phx_ref: String,
    #[serde(flatten)]
    pub user_meta: serde_json::Value,
}

/// Presence diff payload
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PresenceDiffPayload {
    pub joins: HashMap<String, Vec<PresenceMeta>>,
    pub leaves: HashMap<String, Vec<PresenceMeta>>,
}

/// Broadcast payload
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BroadcastPayload {
    pub event: String,
    #[serde(rename = "type")]
    pub msg_type: Option<String>,
    pub payload: serde_json::Value,
}

/// Postgres changes payload
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PostgresChangesPayload {
    pub data: PostgresChangeData,
}

/// Postgres change data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PostgresChangeData {
    /// Schema name
    pub schema: String,
    /// Table name
    pub table: String,
    /// Commit timestamp
    pub commit_timestamp: String,
    /// Event type: INSERT, UPDATE, DELETE
    #[serde(rename = "eventType")]
    pub event_type: String,
    /// New row data
    pub new: Option<serde_json::Value>,
    /// Old row data
    pub old: Option<serde_json::Value>,
    /// Errors
    pub errors: Option<Vec<String>>,
}

/// System event payload
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemPayload {
    pub channel: Option<String>,
    pub extension: Option<String>,
    pub message: Option<String>,
    pub status: Option<String>,
}

/// Incoming WebSocket message
#[derive(Debug, Clone, Deserialize)]
pub struct IncomingMessage {
    pub topic: String,
    pub event: String,
    pub payload: serde_json::Value,
    #[serde(rename = "ref")]
    pub msg_ref: Option<String>,
}

/// Outgoing WebSocket message
#[derive(Debug, Clone, Serialize)]
pub struct OutgoingMessage {
    pub topic: String,
    pub event: String,
    pub payload: serde_json::Value,
    #[serde(rename = "ref")]
    pub msg_ref: Option<String>,
}

impl OutgoingMessage {
    pub fn reply(topic: &str, msg_ref: Option<&str>, status: &str, response: serde_json::Value) -> Self {
        Self {
            topic: topic.to_string(),
            event: "phx_reply".to_string(),
            payload: serde_json::json!({
                "status": status,
                "response": response
            }),
            msg_ref: msg_ref.map(|s| s.to_string()),
        }
    }

    pub fn postgres_changes(topic: &str, change: PostgresChangeData) -> Self {
        Self {
            topic: topic.to_string(),
            event: "postgres_changes".to_string(),
            payload: serde_json::json!({
                "data": change
            }),
            msg_ref: None,
        }
    }

    pub fn broadcast(topic: &str, event: &str, payload: serde_json::Value) -> Self {
        Self {
            topic: topic.to_string(),
            event: "broadcast".to_string(),
            payload: serde_json::json!({
                "event": event,
                "payload": payload
            }),
            msg_ref: None,
        }
    }

    pub fn presence_state(topic: &str, state: HashMap<String, Vec<PresenceMeta>>) -> Self {
        Self {
            topic: topic.to_string(),
            event: "presence_state".to_string(),
            payload: serde_json::to_value(state).unwrap_or_default(),
            msg_ref: None,
        }
    }

    pub fn presence_diff(topic: &str, joins: HashMap<String, Vec<PresenceMeta>>, leaves: HashMap<String, Vec<PresenceMeta>>) -> Self {
        Self {
            topic: topic.to_string(),
            event: "presence_diff".to_string(),
            payload: serde_json::json!({
                "joins": joins,
                "leaves": leaves
            }),
            msg_ref: None,
        }
    }

    pub fn system(topic: &str, message: &str, status: &str) -> Self {
        Self {
            topic: topic.to_string(),
            event: "system".to_string(),
            payload: serde_json::json!({
                "message": message,
                "status": status
            }),
            msg_ref: None,
        }
    }

    pub fn heartbeat(msg_ref: Option<&str>) -> Self {
        Self {
            topic: "phoenix".to_string(),
            event: "phx_reply".to_string(),
            payload: serde_json::json!({
                "status": "ok",
                "response": {}
            }),
            msg_ref: msg_ref.map(|s| s.to_string()),
        }
    }
}

/// Channel subscription
#[derive(Debug, Clone)]
pub struct Subscription {
    pub id: String,
    pub topic: String,
    pub config: ChannelConfig,
    pub created_at: u64,
}

/// Realtime server state
pub struct RealtimeServer {
    subscriptions: HashMap<String, Vec<Subscription>>, // connection_id -> subscriptions
    presence: HashMap<String, HashMap<String, Vec<PresenceMeta>>>, // topic -> presence state
}

impl RealtimeServer {
    pub fn new() -> Self {
        Self {
            subscriptions: HashMap::new(),
            presence: HashMap::new(),
        }
    }

    /// Handle incoming message
    pub fn handle_message(
        &mut self,
        connection_id: &str,
        message: IncomingMessage,
    ) -> Vec<OutgoingMessage> {
        let mut responses = Vec::new();

        match message.event.as_str() {
            "phx_join" => {
                let config: ChannelConfig = serde_json::from_value(
                    message.payload.get("config").cloned().unwrap_or_default()
                ).unwrap_or(ChannelConfig {
                    broadcast: None,
                    presence: None,
                    postgres_changes: None,
                });

                // Create subscription
                let sub = Subscription {
                    id: generate_ref(),
                    topic: message.topic.clone(),
                    config: config.clone(),
                    created_at: current_timestamp(),
                };

                self.subscriptions
                    .entry(connection_id.to_string())
                    .or_default()
                    .push(sub);

                // Reply with OK
                responses.push(OutgoingMessage::reply(
                    &message.topic,
                    message.msg_ref.as_deref(),
                    "ok",
                    serde_json::json!({
                        "postgres_changes": config.postgres_changes.map(|pcs| {
                            pcs.iter().map(|pc| {
                                serde_json::json!({
                                    "id": generate_ref(),
                                    "event": pc.event,
                                    "schema": pc.schema,
                                    "table": pc.table,
                                    "filter": pc.filter
                                })
                            }).collect::<Vec<_>>()
                        })
                    }),
                ));

                // Send presence state if presence is configured
                if config.presence.is_some() {
                    let state = self.presence.get(&message.topic).cloned().unwrap_or_default();
                    responses.push(OutgoingMessage::presence_state(&message.topic, state));
                }
            }

            "phx_leave" => {
                // Remove subscription
                if let Some(subs) = self.subscriptions.get_mut(connection_id) {
                    subs.retain(|s| s.topic != message.topic);
                }

                responses.push(OutgoingMessage::reply(
                    &message.topic,
                    message.msg_ref.as_deref(),
                    "ok",
                    serde_json::json!({}),
                ));
            }

            "heartbeat" => {
                responses.push(OutgoingMessage::heartbeat(message.msg_ref.as_deref()));
            }

            "broadcast" => {
                // Forward broadcast to all subscribers
                if let Some(payload) = message.payload.get("payload") {
                    let event = message.payload.get("event")
                        .and_then(|e| e.as_str())
                        .unwrap_or("broadcast");

                    responses.push(OutgoingMessage::broadcast(
                        &message.topic,
                        event,
                        payload.clone(),
                    ));
                }
            }

            "presence" => {
                // Handle presence update
                if let Some(key) = message.payload.get("key").and_then(|k| k.as_str()) {
                    let meta = PresenceMeta {
                        phx_ref: generate_ref(),
                        user_meta: message.payload.get("meta").cloned().unwrap_or_default(),
                    };

                    let mut joins = HashMap::new();
                    joins.insert(key.to_string(), vec![meta]);

                    self.presence
                        .entry(message.topic.clone())
                        .or_default()
                        .insert(key.to_string(), joins.get(key).cloned().unwrap_or_default());

                    responses.push(OutgoingMessage::presence_diff(
                        &message.topic,
                        joins,
                        HashMap::new(),
                    ));
                }
            }

            _ => {
                // Unknown event - send error reply
                responses.push(OutgoingMessage::reply(
                    &message.topic,
                    message.msg_ref.as_deref(),
                    "error",
                    serde_json::json!({
                        "reason": format!("Unknown event: {}", message.event)
                    }),
                ));
            }
        }

        responses
    }

    /// Notify postgres change to subscribers
    pub fn notify_postgres_change(&self, change: &PostgresChangeData) -> Vec<(String, OutgoingMessage)> {
        let mut notifications = Vec::new();

        for (conn_id, subs) in &self.subscriptions {
            for sub in subs {
                if let Some(ref pcs) = sub.config.postgres_changes {
                    for pc in pcs {
                        // Check if change matches subscription filter
                        if pc.schema == change.schema
                            && (pc.event == "*" || pc.event.to_uppercase() == change.event_type)
                            && (pc.table.is_none() || pc.table.as_deref() == Some(&change.table))
                        {
                            notifications.push((
                                conn_id.clone(),
                                OutgoingMessage::postgres_changes(&sub.topic, change.clone()),
                            ));
                        }
                    }
                }
            }
        }

        notifications
    }

    /// Remove connection and clean up presence
    pub fn disconnect(&mut self, connection_id: &str) -> Vec<OutgoingMessage> {
        let mut responses = Vec::new();

        if let Some(subs) = self.subscriptions.remove(connection_id) {
            for sub in subs {
                // Clean up presence
                if let Some(presence) = self.presence.get_mut(&sub.topic) {
                    // Would identify user from connection and remove
                    if !presence.is_empty() {
                        let leaves: HashMap<String, Vec<PresenceMeta>> = presence.drain().collect();
                        responses.push(OutgoingMessage::presence_diff(&sub.topic, HashMap::new(), leaves));
                    }
                }
            }
        }

        responses
    }

    /// Get all connections subscribed to a topic
    pub fn get_topic_subscribers(&self, topic: &str) -> Vec<&str> {
        self.subscriptions
            .iter()
            .filter(|(_, subs)| subs.iter().any(|s| s.topic == topic))
            .map(|(conn_id, _)| conn_id.as_str())
            .collect()
    }
}

impl Default for RealtimeServer {
    fn default() -> Self {
        Self::new()
    }
}

// Helper functions

fn generate_ref() -> String {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};
    use std::time::{SystemTime, UNIX_EPOCH};

    let mut hasher = DefaultHasher::new();
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos()
        .hash(&mut hasher);

    format!("{}", hasher.finish())
}

fn current_timestamp() -> u64 {
    use std::time::{SystemTime, UNIX_EPOCH};
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}