turso-orm 0.1.3

An async ORM dedicated to the Turso database, with a typed entity API
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
//! End-to-end tests of the entity API against a real in-memory Turso database.
//!
//! The suite exercises the derive macros and the query builders together:
//! schema generation, single-row CRUD through active models, filtering,
//! ordering, pagination, counting and streaming, custom projections with
//! `FromQueryResult`, relations in every form (`find_related`,
//! `find_also_related`, joins and the batch loaders), composite keys and
//! transactions with savepoints. Each test opens its own database so that
//! they can run in parallel.
//!
//! ```text
//! cargo test -p turso-orm --test entity
//! ```

use futures_util::TryStreamExt;
use turso_orm::entity::LoaderTrait;
use turso_orm::prelude::*;
use turso_orm::query::UpdateResult;

/// The `user` entity: an auto-increment key, a unique column and one field of
/// every feature-gated type.
mod user {
    use turso_orm::prelude::*;

    /// A row of the `user` table.
    #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
    #[turso(table_name = "user")]
    pub(crate) struct Model {
        #[turso(primary_key)]
        pub id: i32,
        #[turso(unique)]
        pub email: String,
        pub name: Option<String>,
        pub active: bool,
        pub score: f64,
        pub created_at: DateTime,
        pub token: Uuid,
        pub meta: Option<Json>,
    }

    /// The relations of `user`; the columns are inferred from the reverse
    /// `belongs_to` on `post`.
    #[derive(Copy, Clone, Debug, DeriveRelation)]
    pub(crate) enum Relation {
        #[turso(has_many = "super::post::Entity")]
        Post,
    }

    impl ActiveModelBehavior for ActiveModel {}
}

/// The `post` entity: owns the foreign key to `user` with `ON DELETE CASCADE`
/// and an indexed column.
mod post {
    use turso_orm::prelude::*;

    /// A row of the `post` table.
    #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
    #[turso(table_name = "post")]
    pub(crate) struct Model {
        #[turso(primary_key)]
        pub id: i64,
        #[turso(indexed)]
        pub user_id: i32,
        pub title: String,
        pub views: i32,
    }

    /// The relations of `post`.
    #[derive(Copy, Clone, Debug, DeriveRelation)]
    pub(crate) enum Relation {
        #[turso(
            belongs_to = "super::user::Entity",
            from = "Column::UserId",
            to = "super::user::Column::Id",
            on_delete = "Cascade"
        )]
        User,
    }

    impl ActiveModelBehavior for ActiveModel {}
}

/// The `tag` entity: a composite primary key with a text component, so no
/// auto increment.
mod tag {
    use turso_orm::prelude::*;

    /// A row of the `tag` table.
    #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
    #[turso(table_name = "tag")]
    pub(crate) struct Model {
        #[turso(primary_key)]
        pub post_id: i64,
        #[turso(primary_key)]
        pub label: String,
    }

    /// The relations of `tag`; there are none.
    #[derive(Copy, Clone, Debug, DeriveRelation)]
    pub(crate) enum Relation {}

    impl ActiveModelBehavior for ActiveModel {}
}

/// Opens a fresh in-memory database with the three tables and their indexes.
async fn setup() -> Database {
    let db = Database::connect(ConnectOptions::in_memory().max_connections(4))
        .await
        .expect("open");
    let schema = Schema::new();
    for stmt in [
        schema.create_table_from_entity(user::Entity).to_statement(),
        schema.create_table_from_entity(post::Entity).to_statement(),
        schema.create_table_from_entity(tag::Entity).to_statement(),
    ] {
        db.execute(stmt).await.expect("create table");
    }
    for idx in schema.create_index_from_entity(post::Entity) {
        db.execute(idx.to_statement()).await.expect("create index");
    }
    db
}

/// Builds a user active model with every non-key attribute `Set`, except `name`.
fn new_user(email: &str) -> user::ActiveModel {
    user::ActiveModel {
        email: Set(email.to_owned()),
        active: Set(true),
        score: Set(1.5),
        created_at: Set(chrono::NaiveDate::from_ymd_opt(2024, 1, 2)
            .expect("date")
            .and_hms_opt(3, 4, 5)
            .expect("time")),
        token: Set(Uuid::new_v4()),
        meta: Set(Some(serde_json::json!({"k": 1}))),
        ..Default::default()
    }
}

/// Schema generation renders inline and composite primary keys, foreign keys and `STRICT`.
#[test]
fn schema_ddl() {
    let sql = Schema::new()
        .create_table_from_entity(post::Entity)
        .to_string_inlined();
    assert_eq!(
        sql,
        "CREATE TABLE \"post\" (\"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \"user_id\" INTEGER NOT NULL, \
         \"title\" TEXT NOT NULL, \"views\" INTEGER NOT NULL, \
         FOREIGN KEY (\"user_id\") REFERENCES \"user\" (\"id\") ON DELETE CASCADE)"
    );
    let sql = Schema::new()
        .strict(true)
        .create_table_from_entity(tag::Entity)
        .to_string_inlined();
    assert_eq!(
        sql,
        "CREATE TABLE \"tag\" (\"post_id\" INTEGER NOT NULL, \"label\" TEXT NOT NULL, PRIMARY KEY (\"post_id\", \"label\")) STRICT"
    );
}

/// Insert, find by id, update, save and delete round-trip a row through every column type.
#[tokio::test]
async fn crud() {
    let db = setup().await;
    let am = new_user("a@x.io");
    let token = am.token.clone().into_value().expect("set");
    let inserted = am.insert(&db).await.expect("insert");
    assert_eq!(inserted.id, 1);
    assert_eq!(inserted.email, "a@x.io");
    assert_eq!(inserted.token, token);
    assert_eq!(inserted.meta, Some(serde_json::json!({"k": 1})));
    assert_eq!(inserted.created_at.to_string(), "2024-01-02 03:04:05");

    let found = user::Entity::find_by_id(1)
        .one(&db)
        .await
        .expect("find")
        .expect("exists");
    assert_eq!(found, inserted);

    // A model converted into an active model is all `Unchanged`, so it is
    // an update candidate with nothing to write yet.
    let mut am: user::ActiveModel = found.clone().into();
    assert!(am.is_update());
    assert!(!am.is_changed());
    am.name = Set(Some("Alice".into()));
    am.active = Set(false);
    let updated = am.update(&db).await.expect("update");
    assert_eq!(updated.name.as_deref(), Some("Alice"));
    assert!(!updated.active);

    // `save` with an `Unchanged` key updates and hands back the full row.
    let saved = user::ActiveModel {
        id: Unchanged(1),
        score: Set(9.0),
        ..Default::default()
    }
    .save(&db)
    .await
    .expect("save");
    assert_eq!(saved.score, Unchanged(9.0));
    assert_eq!(saved.name, Unchanged(Some("Alice".into())));

    let res = user::Entity::delete_by_id(1)
        .exec(&db)
        .await
        .expect("delete");
    assert_eq!(res.rows_affected, 1);
    assert!(
        user::Entity::find_by_id(1)
            .one(&db)
            .await
            .expect("find")
            .is_none()
    );
}

/// Filters, ordering, counting, pagination, streaming, `IN` lists and bulk updates and deletes behave as SQL does.
#[tokio::test]
async fn queries_filters_order_pagination_count_stream() {
    let db = setup().await;
    for i in 0..10 {
        let mut u = new_user(&format!("u{i}@x.io"));
        u.score = Set(f64::from(i));
        u.active = Set(i % 2 == 0);
        u.insert(&db).await.expect("insert");
    }
    let actives = user::Entity::find()
        .filter(user::Column::Active.eq(true))
        .filter(user::Column::Score.gte(4))
        .order_by_desc(user::Column::Score)
        .all(&db)
        .await
        .expect("all");
    assert_eq!(
        actives
            .iter()
            .map(|u| u.score.to_string())
            .collect::<Vec<_>>(),
        ["8", "6", "4"]
    );

    let cond = Condition::any()
        .add(user::Column::Email.starts_with("u1"))
        .add(user::Column::Email.ends_with("9@x.io"));
    assert_eq!(
        user::Entity::find()
            .filter(cond)
            .count(&db)
            .await
            .expect("count"),
        2
    );

    let paginator = user::Entity::find()
        .order_by_asc(user::Column::Id)
        .paginate(&db, 4);
    assert_eq!(paginator.num_items().await.expect("items"), 10);
    assert_eq!(paginator.num_pages().await.expect("pages"), 3);
    let page = paginator.fetch_page(2).await.expect("page");
    assert_eq!(page.iter().map(|u| u.id).collect::<Vec<_>>(), [9, 10]);

    let streamed: Vec<i32> = user::Entity::find()
        .order_by_asc(user::Column::Id)
        .limit(3)
        .stream(&db)
        .await
        .expect("stream")
        .map_ok(|u| u.id)
        .try_collect()
        .await
        .expect("collect");
    assert_eq!(streamed, [1, 2, 3]);

    let in_list = user::Entity::find()
        .filter(user::Column::Id.is_in([2, 3, 99]))
        .all(&db)
        .await
        .expect("in");
    assert_eq!(in_list.len(), 2);

    // Scores 0, 1 and 2 are below 3; of those, 0 and 2 were active.
    let UpdateResult { rows_affected } = user::Entity::update_many()
        .col(user::Column::Active, false)
        .filter(user::Column::Score.lt(3))
        .exec(&db)
        .await
        .expect("update many");
    assert_eq!(rows_affected, 3);
    let deleted = user::Entity::delete_many()
        .filter(user::Column::Active.eq(false))
        .exec(&db)
        .await
        .expect("delete many");
    assert_eq!(deleted.rows_affected, 7);
}

/// A custom projection decoded by name, with one field renamed through `column_name`.
#[derive(Debug, PartialEq, turso_orm::FromQueryResult)]
struct Stats {
    total: i64,
    #[turso(column_name = "max_score")]
    best: f64,
}

/// A multi-row insert returns every stored row, a bare select list decodes into a custom struct, and a unique violation is classified.
#[tokio::test]
async fn custom_select_into_model_and_insert_many() {
    let db = setup().await;
    let models: Vec<user::ActiveModel> = (0..3)
        .map(|i| {
            let mut u = new_user(&format!("m{i}@x.io"));
            u.score = Set(f64::from(i * 10));
            u
        })
        .collect();
    let inserted = user::Entity::insert_many(models)
        .exec_with_returning(&db)
        .await
        .expect("insert many");
    assert_eq!(inserted.len(), 3);

    let stats = user::Entity::find()
        .select_only()
        .expr_as(Func::count_star(), "total")
        .expr_as(user::Column::Score.max(), "max_score")
        .into_model::<Stats>()
        .one(&db)
        .await
        .expect("stats")
        .expect("row");
    assert_eq!(
        stats,
        Stats {
            total: 3,
            best: 20.0
        }
    );

    let err = new_user("m0@x.io")
        .insert(&db)
        .await
        .expect_err("duplicate email");
    assert!(err.is_unique_violation());
}

/// Relations resolve in both directions through `find_related`, `find_also_related`, joins and the loaders, and the generated foreign key cascades.
#[tokio::test]
async fn relations_and_loaders() {
    let db = setup().await;
    let alice = new_user("alice@x.io").insert(&db).await.expect("alice");
    let bob = new_user("bob@x.io").insert(&db).await.expect("bob");
    for (owner, title) in [(alice.id, "a1"), (alice.id, "a2"), (bob.id, "b1")] {
        post::ActiveModel {
            user_id: Set(owner),
            title: Set(title.into()),
            views: Set(0),
            ..Default::default()
        }
        .insert(&db)
        .await
        .expect("post");
    }

    // Forward direction: a user's posts.
    let alice_posts = alice
        .find_related(post::Entity)
        .order_by_asc(post::Column::Id)
        .all(&db)
        .await
        .expect("related");
    assert_eq!(
        alice_posts
            .iter()
            .map(|p| p.title.as_str())
            .collect::<Vec<_>>(),
        ["a1", "a2"]
    );

    // Reverse direction: a post's owner.
    let first = post::Entity::find_by_id(1)
        .one(&db)
        .await
        .expect("p")
        .expect("exists");
    let owner = first
        .find_related(user::Entity)
        .one(&db)
        .await
        .expect("owner")
        .expect("exists");
    assert_eq!(owner.id, alice.id);

    // Both sides in one query through the `A_` / `B_` aliased select list.
    let pairs = post::Entity::find()
        .find_also_related(user::Entity)
        .order_by(post::Column::Id, Order::Asc)
        .all(&db)
        .await
        .expect("also related");
    assert_eq!(pairs.len(), 3);
    assert_eq!(
        pairs[2].1.as_ref().map(|u| u.email.as_str()),
        Some("bob@x.io")
    );

    // Batch loaders keep the result aligned with the input order.
    let users = user::Entity::find()
        .order_by_asc(user::Column::Id)
        .all(&db)
        .await
        .expect("users");
    let posts = users.load_many(post::Entity, &db).await.expect("load many");
    assert_eq!(posts.iter().map(Vec::len).collect::<Vec<_>>(), [2, 1]);

    let all_posts = post::Entity::find()
        .order_by_asc(post::Column::Id)
        .all(&db)
        .await
        .expect("posts");
    let owners = all_posts
        .load_one(user::Entity, &db)
        .await
        .expect("load one");
    assert_eq!(
        owners
            .iter()
            .map(|o| o.as_ref().map(|u| u.id))
            .collect::<Vec<_>>(),
        [Some(1), Some(1), Some(2)]
    );

    // A join lets a filter on the related table narrow the main one; the
    // qualified column references keep `user.id` and `post.id` apart.
    let joined = post::Entity::find()
        .inner_join(user::Entity)
        .filter(user::Column::Email.eq("bob@x.io"))
        .all(&db)
        .await
        .expect("join");
    assert_eq!(joined.len(), 1);

    // Deleting a user cascades to its posts through the generated foreign key.
    user::Entity::delete_by_id(alice.id)
        .exec(&db)
        .await
        .expect("delete alice");
    assert_eq!(post::Entity::find().count(&db).await.expect("count"), 1);
}

/// Composite keys round-trip as tuples, a rolled-back savepoint leaves no row, and the closure form of a transaction commits its result.
#[tokio::test]
async fn composite_keys_and_transactions() {
    let db = setup().await;
    let txn = db.begin().await.expect("begin");
    tag::ActiveModel {
        post_id: Set(7),
        label: Set("rust".into()),
    }
    .insert(&txn)
    .await
    .expect("insert tag");
    let found = tag::Entity::find_by_id((7, "rust".to_owned()))
        .one(&txn)
        .await
        .expect("find")
        .expect("exists");
    assert_eq!(found.label, "rust");
    // A nested transaction is a savepoint; rolling it back drops only its
    // own insert.
    {
        let sp = txn.begin().await.expect("savepoint");
        tag::ActiveModel {
            post_id: Set(8),
            label: Set("ghost".into()),
        }
        .insert(&sp)
        .await
        .expect("insert");
        sp.rollback().await.expect("rollback");
    }
    assert_eq!(tag::Entity::find().count(&txn).await.expect("count"), 1);
    txn.commit().await.expect("commit");

    let n = db
        .transaction(|txn| {
            Box::pin(async move {
                tag::Entity::delete_by_id((7, "rust".to_owned()))
                    .exec(txn)
                    .await?;
                tag::Entity::find().count(txn).await
            })
        })
        .await
        .expect("closure");
    assert_eq!(n, 0);
}