somnia 0.8.0

Type-safe SurrealDB ORM for Rust: typed query builder, #[derive(SurrealRecord)], schema generation, and Diesel-style migrations.
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
//! Proves `#[derive(SurrealRecord)]` makes the Rust type the source of truth for
//! the SurrealDB schema: `up()` emits the same `DEFINE TABLE`/`DEFINE FIELD` DDL
//! as the hand-written `migrations/027_create_asset_enrichment_schema.surql`, and
//! `down()` is its inverse.

#[cfg(test)]
mod tests {
    use somnia::{
        DefineAnalyzer, DefineEvent, DefineFunction, DefineParam, For, IfExpr, SurrealRecord,
        SurrealSchema, Thing, Transaction,
    };

    // Mirrors the `asset_version` table from migration 027 — the Rust type is now
    // the single source of truth for that schema.
    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, SurrealRecord)]
    #[table("asset_version")]
    #[allow(dead_code)]
    struct AssetVersion {
        #[field(thing)]
        id: Thing<AssetVersion>,
        #[field(record = "tenant")]
        tenant: Option<serde_json::Value>,
        #[field(record = "asset")]
        asset: serde_json::Value,
        #[field(default = "1")]
        version_number: i64,
        label: Option<String>,
        storage_account: String,
        container: String,
        name: String,
        url: String,
        #[field(default = "0")]
        size: i64,
        hash: Option<String>,
        content_type: Option<String>,
        #[field(record = "user")]
        created_by: Option<serde_json::Value>,
        change_notes: Option<String>,
        #[field(default = "false")]
        is_current: bool,
        #[field(ty = "datetime", default = "time::now()")]
        created_at: String,
    }

    #[test]
    fn up_matches_migration_027() {
        let expected = "\
DEFINE TABLE IF NOT EXISTS asset_version SCHEMAFULL PERMISSIONS FULL;
DEFINE FIELD IF NOT EXISTS tenant ON TABLE asset_version TYPE option<record<tenant>>;
DEFINE FIELD IF NOT EXISTS asset ON TABLE asset_version TYPE record<asset>;
DEFINE FIELD IF NOT EXISTS version_number ON TABLE asset_version TYPE int DEFAULT 1;
DEFINE FIELD IF NOT EXISTS label ON TABLE asset_version TYPE option<string>;
DEFINE FIELD IF NOT EXISTS storage_account ON TABLE asset_version TYPE string;
DEFINE FIELD IF NOT EXISTS container ON TABLE asset_version TYPE string;
DEFINE FIELD IF NOT EXISTS name ON TABLE asset_version TYPE string;
DEFINE FIELD IF NOT EXISTS url ON TABLE asset_version TYPE string;
DEFINE FIELD IF NOT EXISTS size ON TABLE asset_version TYPE int DEFAULT 0;
DEFINE FIELD IF NOT EXISTS hash ON TABLE asset_version TYPE option<string>;
DEFINE FIELD IF NOT EXISTS content_type ON TABLE asset_version TYPE option<string>;
DEFINE FIELD IF NOT EXISTS created_by ON TABLE asset_version TYPE option<record<user>>;
DEFINE FIELD IF NOT EXISTS change_notes ON TABLE asset_version TYPE option<string>;
DEFINE FIELD IF NOT EXISTS is_current ON TABLE asset_version TYPE bool DEFAULT false;
DEFINE FIELD IF NOT EXISTS created_at ON TABLE asset_version TYPE datetime DEFAULT time::now();";

        assert_eq!(AssetVersion::up(), expected);
    }

    #[test]
    fn down_drops_the_table() {
        assert_eq!(
            AssetVersion::down(),
            "REMOVE TABLE IF EXISTS asset_version;"
        );
    }

    #[test]
    fn schemaless_and_custom_permissions() {
        #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, SurrealRecord)]
        #[table("scratch", schemaless, permissions = "NONE")]
        #[allow(dead_code)]
        struct Scratch {
            #[field(thing)]
            id: Thing<Scratch>,
            blob: serde_json::Value,
        }
        assert_eq!(
            Scratch::define_table(),
            "DEFINE TABLE IF NOT EXISTS scratch SCHEMALESS PERMISSIONS NONE;"
        );
        // Fields are still defined even on a schemaless table.
        assert_eq!(
            Scratch::define_fields(),
            &["DEFINE FIELD IF NOT EXISTS blob ON TABLE scratch TYPE object;"]
        );
    }

    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, SurrealRecord)]
    #[table("member")]
    #[index(name = "member_email_unique", fields = "email", unique)]
    #[index(name = "member_name_idx", fields = "first_name, last_name")]
    #[allow(dead_code)]
    struct Member {
        #[field(thing)]
        id: Thing<Member>,
        email: String,
        first_name: String,
        last_name: String,
    }

    #[test]
    fn index_attrs_emit_define_index_ddl() {
        assert_eq!(
            Member::define_indexes(),
            &[
                "DEFINE INDEX IF NOT EXISTS member_email_unique ON TABLE member FIELDS email UNIQUE;",
                "DEFINE INDEX IF NOT EXISTS member_name_idx ON TABLE member FIELDS first_name, last_name;",
            ]
        );
        // up() appends indexes after the field definitions.
        let up = Member::up();
        let fields_end = up.find("DEFINE INDEX").unwrap();
        assert!(up[..fields_end]
            .contains("DEFINE FIELD IF NOT EXISTS email ON TABLE member TYPE string;"));
        assert!(up.ends_with("FIELDS first_name, last_name;"));
    }

    #[test]
    fn indexes_apply_and_enforce_on_live_surreal() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(async {
            let db = surrealdb::engine::any::connect("mem://").await.unwrap();
            db.use_ns("t").use_db("t").await.unwrap();
            db.query(Member::up()).await.unwrap().check().unwrap();

            // First insert with a given email succeeds.
            db.query("CREATE member SET email = 'a@x.com', first_name = 'A', last_name = 'B';")
                .await
                .unwrap()
                .check()
                .unwrap();
            // A second insert with the same email must violate the UNIQUE index.
            let dup = db
                .query("CREATE member SET email = 'a@x.com', first_name = 'C', last_name = 'D';")
                .await
                .unwrap()
                .check();
            assert!(
                dup.is_err(),
                "UNIQUE index should reject the duplicate email"
            );
        });
    }

    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, SurrealRecord)]
    #[table("account")]
    #[allow(dead_code)]
    struct Account {
        #[field(thing)]
        id: Thing<Account>,
        #[field(assert = "$value >= 0")]
        balance: i64,
        #[field(readonly)]
        created_by: String,
        #[field(permissions = "FOR select NONE")]
        secret: String,
    }

    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, SurrealRecord)]
    #[table("blob_holder")]
    #[allow(dead_code)]
    struct BlobHolder {
        #[field(thing)]
        id: Thing<BlobHolder>,
        #[field(flexible)]
        meta: serde_json::Value,
    }

    #[test]
    fn flexible_field_renders_flexible_keyword() {
        assert_eq!(
            BlobHolder::define_fields(),
            &["DEFINE FIELD IF NOT EXISTS meta ON TABLE blob_holder FLEXIBLE TYPE object;"]
        );
    }

    #[test]
    fn field_assert_readonly_permissions_ddl() {
        assert_eq!(
            Account::define_fields(),
            &[
                "DEFINE FIELD IF NOT EXISTS balance ON TABLE account TYPE int ASSERT $value >= 0;",
                "DEFINE FIELD IF NOT EXISTS created_by ON TABLE account TYPE string READONLY;",
                "DEFINE FIELD IF NOT EXISTS secret ON TABLE account TYPE string PERMISSIONS FOR select NONE;",
            ]
        );
    }

    #[test]
    fn field_attrs_apply_on_live_surreal() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(async {
            let db = surrealdb::engine::any::connect("mem://").await.unwrap();
            db.use_ns("t").use_db("t").await.unwrap();
            db.query(Account::up()).await.unwrap().check().unwrap();
            // A valid row applies.
            db.query("CREATE account SET balance = 10, created_by = 'me', secret = 's';")
                .await
                .unwrap()
                .check()
                .unwrap();
            // ASSERT rejects a negative balance.
            let bad = db
                .query("CREATE account SET balance = -1, created_by = 'me', secret = 's';")
                .await
                .unwrap()
                .check();
            assert!(bad.is_err(), "ASSERT $value >= 0 should reject -1");
        });
    }

    #[test]
    fn standalone_ddl_builders_apply_on_live_surreal() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(async {
            let db = surrealdb::engine::any::connect("mem://").await.unwrap();
            db.use_ns("t").use_db("t").await.unwrap();
            db.query("DEFINE TABLE post SCHEMALESS;")
                .await
                .unwrap()
                .check()
                .unwrap();

            for ddl in [
                DefineAnalyzer::new("ascii")
                    .tokenizers(["class"])
                    .filters(["lowercase", "ascii"])
                    .to_surrealql(),
                DefineParam::new("rate", "0.5").to_surrealql(),
                DefineFunction::new("greet")
                    .arg("name", "string")
                    .returns("string")
                    .body("RETURN 'hi ' + $name;")
                    .to_surrealql(),
                DefineEvent::new("on_post", "post")
                    .when("$event = 'CREATE'")
                    .then("{ CREATE log SET at = time::now() }")
                    .to_surrealql(),
            ] {
                db.query(&ddl).await.unwrap().check().unwrap();
            }

            // The function is callable.
            let mut res = db
                .query("RETURN fn::greet('world');")
                .await
                .unwrap()
                .check()
                .unwrap();
            let out: Option<String> = res.take(0).unwrap();
            assert_eq!(out.as_deref(), Some("hi world"));

            // The param resolves to its defined value.
            let mut res = db.query("RETURN $rate;").await.unwrap().check().unwrap();
            let rate: Option<f64> = res.take(0).unwrap();
            assert_eq!(rate, Some(0.5));
        });
    }

    #[test]
    fn control_flow_runs_on_live_surreal() {
        use somnia::{DynExpr, Raw};
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(async {
            let db = surrealdb::engine::any::connect("mem://").await.unwrap();
            db.use_ns("t").use_db("t").await.unwrap();

            // FOR loop creates three rows.
            let for_sql = For::new("n", Raw("[1, 2, 3]".into()))
                .push("CREATE counter SET v = $n")
                .to_surrealql();
            db.query(&for_sql).await.unwrap().check().unwrap();
            let mut res = db
                .query("SELECT count() FROM counter GROUP ALL;")
                .await
                .unwrap()
                .check()
                .unwrap();
            let rows: Vec<serde_json::Value> = res.take(0).unwrap();
            assert_eq!(rows[0]["count"].as_i64(), Some(3));

            // IF expression evaluates the matching branch.
            let mut if_buf = String::from("RETURN ");
            IfExpr::new(Raw("5 > 3".into()), Raw("'yes'".into()))
                .else_(Raw("'no'".into()))
                .render_dyn(&mut if_buf);
            let mut res = db.query(&if_buf).await.unwrap().check().unwrap();
            let out: Option<String> = res.take(0).unwrap();
            assert_eq!(out.as_deref(), Some("yes"));
        });
    }

    #[test]
    fn transaction_is_atomic_on_live_surreal() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(async {
            let db = surrealdb::engine::any::connect("mem://").await.unwrap();
            db.use_ns("t").use_db("t").await.unwrap();
            db.query(Member::up()).await.unwrap().check().unwrap();

            // Two inserts in one transaction; the second reuses the first's email,
            // violating the UNIQUE index. The whole transaction must roll back.
            let tx = Transaction::new()
                .push("CREATE member SET email = 'dup@x.com', first_name = 'A', last_name = 'B'")
                .push("CREATE member SET email = 'dup@x.com', first_name = 'C', last_name = 'D'");
            let res = db.query(tx.to_surrealql()).await.unwrap().check();
            assert!(res.is_err(), "transaction should fail on the duplicate");

            // Atomicity: the first insert must NOT have persisted.
            let mut count = db
                .query("SELECT count() FROM member GROUP ALL;")
                .await
                .unwrap()
                .check()
                .unwrap();
            let rows: Vec<serde_json::Value> = count.take(0).unwrap();
            let n = rows
                .first()
                .and_then(|r| r.get("count"))
                .and_then(|c| c.as_i64())
                .unwrap_or(0);
            assert_eq!(n, 0, "rolled-back transaction must leave no rows");
        });
    }

    // Exercises the recursive type mapper: typed arrays, arrays of records,
    // nested Option, and duration.
    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, SurrealRecord)]
    #[table("catalog")]
    #[allow(dead_code)]
    struct Catalog {
        #[field(thing)]
        id: Thing<Catalog>,
        tags: Vec<String>,
        scores: Vec<i64>,
        owners: Vec<Thing<Member>>,
        nicknames: Option<Vec<String>>,
        ttl: std::time::Duration,
    }

    #[test]
    fn richer_type_mapping_emits_precise_field_types() {
        assert_eq!(
            Catalog::define_fields(),
            &[
                "DEFINE FIELD IF NOT EXISTS tags ON TABLE catalog TYPE array<string>;",
                "DEFINE FIELD IF NOT EXISTS scores ON TABLE catalog TYPE array<int>;",
                "DEFINE FIELD IF NOT EXISTS owners ON TABLE catalog TYPE array<record>;",
                "DEFINE FIELD IF NOT EXISTS nicknames ON TABLE catalog TYPE option<array<string>>;",
                "DEFINE FIELD IF NOT EXISTS ttl ON TABLE catalog TYPE duration;",
            ]
        );
    }

    #[test]
    fn richer_types_apply_on_live_surreal() {
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(async {
            let db = surrealdb::engine::any::connect("mem://").await.unwrap();
            db.use_ns("t").use_db("t").await.unwrap();
            db.query(Catalog::up()).await.unwrap().check().unwrap();
            // A write satisfying every typed field must succeed under SCHEMAFULL.
            db.query(
                "CREATE catalog SET tags = ['a', 'b'], scores = [1, 2], \
                 owners = [member:x], nicknames = NONE, ttl = 1s500ms;",
            )
            .await
            .unwrap()
            .check()
            .unwrap();
        });
    }

    #[test]
    fn up_down_round_trips_against_live_surreal() {
        // The generated DDL must actually apply (and reverse) on SurrealDB 3.x.
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(async {
            let db = surrealdb::engine::any::connect("mem://").await.unwrap();
            db.use_ns("t").use_db("t").await.unwrap();

            // up: define schema, then a write that satisfies SCHEMAFULL must succeed.
            db.query(AssetVersion::up()).await.unwrap().check().unwrap();
            db.query(
                "CREATE asset_version SET asset = asset:a1, version_number = 2, \
                 storage_account = 'sa', container = 'c', name = 'n', url = 'u', \
                 is_current = true;",
            )
            .await
            .unwrap()
            .check()
            .unwrap();

            // down: drop the table. Selecting from the now-removed SCHEMAFULL
            // table errors with "table does not exist" — proving the reversal.
            db.query(AssetVersion::down())
                .await
                .unwrap()
                .check()
                .unwrap();
            let res = db.query("SELECT * FROM asset_version;").await.unwrap();
            assert!(res.check().is_err(), "table should not exist after down()");

            // up() is idempotent / reversible: re-applying it succeeds again.
            db.query(AssetVersion::up()).await.unwrap().check().unwrap();
        });
    }
}