heliosdb-nano 3.30.0

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
511
512
513
514
515
516
517
518
519
520
521
522
523
//! REST API Handlers (PostgREST-compatible)
//!
//! Axum handlers for the `/rest/v1/` endpoints.  These translate HTTP
//! requests with PostgREST-style query parameters into parameterized SQL
//! queries and return JSON results.

use axum::{
    extract::{Path, Query, State},
    http::{HeaderMap, StatusCode},
    Json,
};
use std::collections::HashMap;
use tracing::{info, warn, debug};

use crate::api::{
    models::ApiError,
    server::AppState,
    rest_executor::RestExecutor,
};

/// Reserved query-string keys that are NOT filter columns.
const RESERVED_KEYS: &[&str] = &["select", "order", "limit", "offset", "apikey"];

/// Convert a `crate::Value` to a `serde_json::Value`.
fn value_to_json(val: &crate::Value) -> serde_json::Value {
    serde_json::Value::from(val)
}

/// Collect filter parameters from the query string.
///
/// Everything that is NOT a reserved key (`select`, `order`, `limit`, `offset`)
/// is treated as a `column=operator.value` filter.
fn collect_filters(params: &HashMap<String, String>) -> Vec<(String, String)> {
    params.iter()
        .filter(|(k, _)| !RESERVED_KEYS.contains(&k.as_str()))
        .map(|(k, v)| (k.clone(), v.clone()))
        .collect()
}

// ── RLS helpers ──────────────────────────────────────────────────────────────

/// Extract the user ID from the JWT in the `Authorization: Bearer <token>` header.
///
/// If an `AuthBridge` is configured in state, the token is decoded and
/// `claims.sub` (the user ID) is returned.  Returns `None` when no header
/// is present or the token is invalid.
fn extract_user_from_headers(headers: &HeaderMap, state: &AppState) -> Option<String> {
    let bridge = state.auth_bridge.as_ref()?;
    let auth_header = headers.get("authorization")?.to_str().ok()?;
    let token = auth_header.strip_prefix("Bearer ")?;
    bridge.get_user(token).ok().map(|u| u.id)
}

/// Return `true` if the request carries a `service_role` API key.
///
/// The service-role key bypasses RLS.  We look for it in the `apikey`
/// header (Supabase convention) or the `x-api-key` header.
fn is_service_role(headers: &HeaderMap, state: &AppState) -> bool {
    let bridge = match &state.auth_bridge {
        Some(b) => b,
        None => return false,
    };

    let key = headers
        .get("apikey")
        .or_else(|| headers.get("x-api-key"))
        .and_then(|v| v.to_str().ok());

    if let Some(key) = key {
        // If the key can be decoded as a valid JWT with role=service_role, bypass.
        if let Ok(user) = bridge.get_user(key) {
            return user.role == "service_role";
        }
    }
    false
}

// ── Handlers ─────────────────────────────────────────────────────────────────

/// `GET /rest/v1/:table` — Select rows from a table.
///
/// Query parameters:
/// - `select` — comma-separated column list (default `*`)
/// - `order`  — e.g. `created_at.desc,name.asc`
/// - `limit`  — maximum rows
/// - `offset` — skip N rows
/// - Any other key is a filter: `column=operator.value`
///
/// RLS: when a valid JWT is present and the table has an `owner_id`/`user_id`
/// column, only rows belonging to the authenticated user are returned.
/// A `service_role` API key bypasses RLS.
pub async fn rest_select(
    State(state): State<AppState>,
    Path(table): Path<String>,
    Query(params): Query<HashMap<String, String>>,
    headers: HeaderMap,
) -> Result<Json<Vec<serde_json::Value>>, ApiError> {
    info!(table = %table, "REST SELECT");

    let executor = RestExecutor::new(state.db.clone());
    let select = params.get("select").map(|s| s.as_str()).unwrap_or("*");
    let order = params.get("order").map(|s| s.as_str());
    let limit: Option<usize> = params.get("limit").and_then(|s| s.parse().ok());
    let offset: Option<usize> = params.get("offset").and_then(|s| s.parse().ok());
    let filters = collect_filters(&params);

    // ── RLS ──────────────────────────────────────────────────────────
    let bypass_rls = is_service_role(&headers, &state);
    let user_id = if bypass_rls {
        None
    } else {
        extract_user_from_headers(&headers, &state)
    };
    debug!(table = %table, ?user_id, bypass_rls, "RLS context");

    let (tuples, columns) = if user_id.is_some() && !bypass_rls {
        executor
            .select_with_rls(&table, select, &filters, order, limit, offset, user_id.as_deref())
            .map_err(|e| {
                warn!(table = %table, error = %e, "REST SELECT (RLS) failed");
                ApiError::from(e)
            })?
    } else {
        executor
            .select(&table, select, &filters, order, limit, offset)
            .map_err(|e| {
                warn!(table = %table, error = %e, "REST SELECT failed");
                ApiError::from(e)
            })?
    };

    let rows: Vec<serde_json::Value> = tuples.iter().map(|tuple| {
        let mut obj = serde_json::Map::new();
        for (i, col) in columns.iter().enumerate() {
            if let Some(val) = tuple.values.get(i) {
                obj.insert(col.clone(), value_to_json(val));
            }
        }
        serde_json::Value::Object(obj)
    }).collect();

    Ok(Json(rows))
}

/// `POST /rest/v1/:table` — Insert rows into a table.
///
/// Accepts a JSON object (single row) or a JSON array of objects (batch).
/// After a successful insert the change notifier is informed so that
/// active WebSocket subscribers receive a realtime event.
pub async fn rest_insert(
    State(state): State<AppState>,
    Path(table): Path<String>,
    Json(body): Json<serde_json::Value>,
) -> Result<(StatusCode, Json<serde_json::Value>), ApiError> {
    info!(table = %table, "REST INSERT");

    let executor = RestExecutor::new(state.db.clone());

    // Normalise: single object → array of one
    let rows: Vec<serde_json::Value> = match body.clone() {
        serde_json::Value::Array(arr) => arr,
        obj @ serde_json::Value::Object(_) => vec![obj],
        _ => return Err(ApiError::bad_request(
            "Request body must be a JSON object or array of objects"
        )),
    };

    let (affected, _, _) = executor.insert(&table, &rows).map_err(|e| {
        warn!(table = %table, error = %e, "REST INSERT failed");
        ApiError::from(e)
    })?;

    // ── Notify subscribers ───────────────────────────────────────────
    if affected > 0 {
        if let Some(notifier) = &state.change_notifier {
            for row in &rows {
                notifier.notify(&table, "INSERT", Some(row.clone()), None);
            }
        }
    }

    let response = serde_json::json!({
        "message": format!("{affected} row(s) inserted"),
        "count": affected,
    });

    Ok((StatusCode::CREATED, Json(response)))
}

/// `PATCH /rest/v1/:table` — Update rows matching filter criteria.
///
/// The request body is a JSON object with the columns/values to set.
/// Filter criteria come from query-string parameters.
/// RLS: restricts updates to rows owned by the authenticated user (unless
/// service_role).
pub async fn rest_update(
    State(state): State<AppState>,
    Path(table): Path<String>,
    Query(params): Query<HashMap<String, String>>,
    headers: HeaderMap,
    Json(body): Json<serde_json::Value>,
) -> Result<Json<serde_json::Value>, ApiError> {
    info!(table = %table, "REST UPDATE");

    let executor = RestExecutor::new(state.db.clone());
    let filters = collect_filters(&params);

    // ── RLS ──────────────────────────────────────────────────────────
    let bypass_rls = is_service_role(&headers, &state);
    let user_id = if bypass_rls {
        None
    } else {
        extract_user_from_headers(&headers, &state)
    };

    let affected = if user_id.is_some() && !bypass_rls {
        executor.update_with_rls(&table, &body, &filters, user_id.as_deref()).map_err(|e| {
            warn!(table = %table, error = %e, "REST UPDATE (RLS) failed");
            ApiError::from(e)
        })?
    } else {
        executor.update(&table, &body, &filters).map_err(|e| {
            warn!(table = %table, error = %e, "REST UPDATE failed");
            ApiError::from(e)
        })?
    };

    // ── Notify subscribers ───────────────────────────────────────────
    if affected > 0 {
        if let Some(notifier) = &state.change_notifier {
            notifier.notify(&table, "UPDATE", Some(body), None);
        }
    }

    Ok(Json(serde_json::json!({
        "message": format!("{affected} row(s) updated"),
        "count": affected,
    })))
}

/// `DELETE /rest/v1/:table` — Delete rows matching filter criteria.
///
/// Filter criteria come from query-string parameters.
/// RLS: restricts deletes to rows owned by the authenticated user (unless
/// service_role).
pub async fn rest_delete(
    State(state): State<AppState>,
    Path(table): Path<String>,
    Query(params): Query<HashMap<String, String>>,
    headers: HeaderMap,
) -> Result<Json<serde_json::Value>, ApiError> {
    info!(table = %table, "REST DELETE");

    let executor = RestExecutor::new(state.db.clone());
    let filters = collect_filters(&params);

    // ── RLS ──────────────────────────────────────────────────────────
    let bypass_rls = is_service_role(&headers, &state);
    let user_id = if bypass_rls {
        None
    } else {
        extract_user_from_headers(&headers, &state)
    };

    let affected = if user_id.is_some() && !bypass_rls {
        executor.delete_with_rls(&table, &filters, user_id.as_deref()).map_err(|e| {
            warn!(table = %table, error = %e, "REST DELETE (RLS) failed");
            ApiError::from(e)
        })?
    } else {
        executor.delete(&table, &filters).map_err(|e| {
            warn!(table = %table, error = %e, "REST DELETE failed");
            ApiError::from(e)
        })?
    };

    // ── Notify subscribers ───────────────────────────────────────────
    if affected > 0 {
        if let Some(notifier) = &state.change_notifier {
            notifier.notify(&table, "DELETE", None, None);
        }
    }

    Ok(Json(serde_json::json!({
        "message": format!("{affected} row(s) deleted"),
        "count": affected,
    })))
}

/// `POST /rest/v1/rpc/:function` — Execute a stored function / RPC.
///
/// This is a stub that returns 501 Not Implemented for now.
#[allow(dead_code)]
pub async fn rest_rpc(
    State(_state): State<AppState>,
    Path(function): Path<String>,
    Json(_body): Json<serde_json::Value>,
) -> Result<Json<serde_json::Value>, ApiError> {
    info!(function = %function, "REST RPC");

    Err(ApiError::new(
        StatusCode::NOT_IMPLEMENTED,
        "NotImplemented",
        format!("RPC function '{}' is not yet supported", function),
    ))
}

// ── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::EmbeddedDatabase;
    use crate::compute::QueryRegistry;
    use std::sync::Arc;

    fn test_state() -> AppState {
        let db = Arc::new(EmbeddedDatabase::new_in_memory().unwrap());
        let query_registry = Arc::new(QueryRegistry::new());
        AppState { db, query_registry, auth_bridge: None, oauth_registry: None, change_notifier: None }
    }

    fn test_state_with_table() -> AppState {
        let state = test_state();
        state.db.execute("CREATE TABLE users (id INT, name TEXT, age INT)").unwrap();
        state.db.execute("INSERT INTO users VALUES (1, 'Alice', 30)").unwrap();
        state.db.execute("INSERT INTO users VALUES (2, 'Bob', 25)").unwrap();
        state.db.execute("INSERT INTO users VALUES (3, 'Carol', 35)").unwrap();
        state
    }

    fn empty_headers() -> HeaderMap {
        HeaderMap::new()
    }

    #[tokio::test]
    async fn test_rest_select_all() {
        let state = test_state_with_table();
        let params = HashMap::new();

        let result = rest_select(
            State(state),
            Path("users".to_string()),
            Query(params),
            empty_headers(),
        ).await;

        assert!(result.is_ok());
        let rows = result.unwrap().0;
        assert_eq!(rows.len(), 3);
    }

    #[tokio::test]
    async fn test_rest_select_with_filter() {
        let state = test_state_with_table();
        let mut params = HashMap::new();
        params.insert("name".to_string(), "eq.Alice".to_string());

        let result = rest_select(
            State(state),
            Path("users".to_string()),
            Query(params),
            empty_headers(),
        ).await;

        assert!(result.is_ok());
        let rows = result.unwrap().0;
        assert_eq!(rows.len(), 1);
    }

    #[tokio::test]
    async fn test_rest_select_with_limit() {
        let state = test_state_with_table();
        let mut params = HashMap::new();
        params.insert("limit".to_string(), "2".to_string());

        let result = rest_select(
            State(state),
            Path("users".to_string()),
            Query(params),
            empty_headers(),
        ).await;

        assert!(result.is_ok());
        let rows = result.unwrap().0;
        assert_eq!(rows.len(), 2);
    }

    #[tokio::test]
    async fn test_rest_insert_single() {
        let state = test_state();
        state.db.execute("CREATE TABLE items (id INT, label TEXT)").unwrap();

        let body = serde_json::json!({"id": 1, "label": "test"});

        let result = rest_insert(
            State(state),
            Path("items".to_string()),
            Json(body),
        ).await;

        assert!(result.is_ok());
        let (status, json) = result.unwrap();
        assert_eq!(status, StatusCode::CREATED);
        assert_eq!(json.0["count"], 1);
    }

    #[tokio::test]
    async fn test_rest_insert_batch() {
        let state = test_state();
        state.db.execute("CREATE TABLE items (id INT, label TEXT)").unwrap();

        let body = serde_json::json!([
            {"id": 1, "label": "a"},
            {"id": 2, "label": "b"},
        ]);

        let result = rest_insert(
            State(state),
            Path("items".to_string()),
            Json(body),
        ).await;

        assert!(result.is_ok());
        let (_, json) = result.unwrap();
        assert_eq!(json.0["count"], 2);
    }

    #[tokio::test]
    async fn test_rest_update() {
        let state = test_state_with_table();
        let mut params = HashMap::new();
        params.insert("id".to_string(), "eq.1".to_string());

        let body = serde_json::json!({"name": "Alicia"});

        let result = rest_update(
            State(state),
            Path("users".to_string()),
            Query(params),
            empty_headers(),
            Json(body),
        ).await;

        assert!(result.is_ok());
        let json = result.unwrap().0;
        assert_eq!(json["count"], 1);
    }

    #[tokio::test]
    async fn test_rest_delete() {
        let state = test_state_with_table();
        let mut params = HashMap::new();
        params.insert("id".to_string(), "eq.2".to_string());

        let result = rest_delete(
            State(state.clone()),
            Path("users".to_string()),
            Query(params),
            empty_headers(),
        ).await;

        assert!(result.is_ok());
        let json = result.unwrap().0;
        assert_eq!(json["count"], 1);

        // Verify row count
        let _remaining = state.db.query("SELECT * FROM users", &[]);
    }

    #[tokio::test]
    async fn test_rest_select_nonexistent_table() {
        let state = test_state();
        let params = HashMap::new();

        let result = rest_select(
            State(state),
            Path("nonexistent".to_string()),
            Query(params),
            empty_headers(),
        ).await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_rest_insert_invalid_body() {
        let state = test_state();
        state.db.execute("CREATE TABLE t (id INT)").unwrap();

        let body = serde_json::json!("not an object");

        let result = rest_insert(
            State(state),
            Path("t".to_string()),
            Json(body),
        ).await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_rest_select_rls_no_auth_bridge_returns_all() {
        // Without an auth bridge, RLS is effectively disabled.
        let state = test_state_with_table();
        state.db.execute("ALTER TABLE users ADD COLUMN owner_id TEXT").unwrap();
        state.db.execute("UPDATE users SET owner_id = 'u1' WHERE id = 1").unwrap();
        state.db.execute("UPDATE users SET owner_id = 'u2' WHERE id = 2").unwrap();
        state.db.execute("UPDATE users SET owner_id = 'u1' WHERE id = 3").unwrap();

        let result = rest_select(
            State(state),
            Path("users".to_string()),
            Query(HashMap::new()),
            empty_headers(),
        ).await;

        assert!(result.is_ok());
        // No auth bridge = no RLS filtering = all 3 rows.
        assert_eq!(result.unwrap().0.len(), 3);
    }
}