orso 0.0.2

ORSO - A ORm for turSO and SQLite with zero-loss 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
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
use crate::{Database, FilterOperator, Result};
use chrono::{DateTime, Utc};
use serde::{Serialize, de::DeserializeOwned};
use std::collections::HashMap;

#[derive(Debug, Clone, PartialEq)]
pub enum FieldType {
    Text,
    Integer,
    BigInt,
    Numeric,
    Boolean,
    JsonB,
    Timestamp,
}

#[allow(async_fn_in_trait)]
pub trait Orso: Serialize + DeserializeOwned + Send + Sync + Clone {
    fn table_name() -> &'static str;
    fn primary_key_field() -> &'static str {
        "id"
    }
    fn created_at_field() -> Option<&'static str> {
        None
    }
    fn updated_at_field() -> Option<&'static str> {
        None
    }
    fn unique_fields() -> Vec<&'static str> {
        vec![]
    }
    fn has_auto_id() -> bool {
        true
    }
    fn has_timestamps() -> bool {
        true
    }

    fn field_names() -> Vec<&'static str>;
    fn field_types() -> Vec<FieldType>;
    fn field_nullable() -> Vec<bool>;
    fn columns() -> Vec<&'static str>;

    fn get_primary_key(&self) -> Option<String>;
    fn set_primary_key(&mut self, id: String);
    fn get_created_at(&self) -> Option<DateTime<Utc>>;
    fn get_updated_at(&self) -> Option<DateTime<Utc>>;
    fn set_updated_at(&mut self, updated_at: DateTime<Utc>);

    fn migration_sql() -> String;

    fn to_map(&self) -> Result<HashMap<String, crate::Value>>;
    fn from_map(map: HashMap<String, crate::Value>) -> Result<Self>;

    async fn insert(&self, db: &Database) -> Result<()> {
        crate::operations::CrudOperations::insert(self, db).await
    }
    async fn insert_with_table(&self, db: &Database, table_name: &str) -> Result<()> {
        crate::operations::CrudOperations::insert_with_table(self, db, table_name).await
    }

    async fn find_by_id(id: &str, db: &Database) -> Result<Option<Self>> {
        crate::operations::CrudOperations::find_by_id::<Self>(id, db).await
    }

    async fn find_by_id_with_table(
        id: &str,
        db: &Database,
        table_name: &str,
    ) -> Result<Option<Self>> {
        crate::operations::CrudOperations::find_by_id_with_table::<Self>(id, db, table_name).await
    }

    async fn find_all(db: &Database) -> Result<Vec<Self>> {
        crate::operations::CrudOperations::find_all::<Self>(db).await
    }

    async fn find_all_with_table(db: &Database, table_name: &str) -> Result<Vec<Self>> {
        crate::operations::CrudOperations::find_all_with_table::<Self>(db, table_name).await
    }

    async fn find_where(filter: FilterOperator, db: &Database) -> Result<Vec<Self>> {
        crate::operations::CrudOperations::find_where::<Self>(filter, db).await
    }

    async fn find_where_with_table(
        filter: FilterOperator,
        db: &Database,
        table_name: &str,
    ) -> Result<Vec<Self>> {
        crate::operations::CrudOperations::find_where_with_table::<Self>(filter, db, table_name)
            .await
    }

    async fn update(&self, db: &Database) -> Result<()> {
        crate::operations::CrudOperations::update(self, db).await
    }

    async fn update_with_table(&self, db: &Database, table_name: &str) -> Result<()> {
        crate::operations::CrudOperations::update_with_table(self, db, table_name).await
    }

    async fn delete(&self, db: &Database) -> Result<bool> {
        crate::operations::CrudOperations::delete(self, db).await
    }

    async fn delete_with_table(&self, db: &Database, table_name: &str) -> Result<bool> {
        crate::operations::CrudOperations::delete_with_table(self, db, table_name).await
    }

    async fn count(db: &Database) -> Result<u64> {
        crate::operations::CrudOperations::count::<Self>(db).await
    }

    async fn count_with_table(db: &Database, table_name: &str) -> Result<u64> {
        crate::operations::CrudOperations::count_with_table::<Self>(db, table_name).await
    }

    // Advanced CRUD operations
    async fn insert_or_update(&self, db: &Database) -> Result<()> {
        crate::operations::CrudOperations::insert_or_update(self, db).await
    }

    async fn insert_or_update_with_table(&self, db: &Database, table_name: &str) -> Result<()> {
        crate::operations::CrudOperations::insert_or_update_with_table(self, db, table_name).await
    }

    async fn upsert(&self, db: &Database) -> Result<()> {
        crate::operations::CrudOperations::upsert(self, db).await
    }

    async fn upsert_with_table(&self, db: &Database, table_name: &str) -> Result<()> {
        crate::operations::CrudOperations::upsert_with_table(self, db, table_name).await
    }

    // Batch operations (Turso-optimized with execute_batch)
    async fn batch_create(models: &[Self], db: &Database) -> Result<()> {
        crate::operations::CrudOperations::batch_create(models, db).await
    }

    async fn batch_insert_with_table(
        models: &[Self],
        db: &Database,
        table_name: &str,
    ) -> Result<()> {
        crate::operations::CrudOperations::batch_insert_with_table(models, db, table_name).await
    }

    async fn batch_update(models: &[Self], db: &Database) -> Result<()> {
        crate::operations::CrudOperations::batch_update(models, db).await
    }

    async fn batch_update_with_table(
        models: &[Self],
        db: &Database,
        table_name: &str,
    ) -> Result<()> {
        crate::operations::CrudOperations::batch_update_with_table(models, db, table_name).await
    }

    async fn batch_delete(ids: &[&str], db: &Database) -> Result<u64> {
        crate::operations::CrudOperations::batch_delete::<Self>(ids, db).await
    }

    async fn batch_delete_with_table(ids: &[&str], db: &Database, table_name: &str) -> Result<u64> {
        crate::operations::CrudOperations::batch_delete_with_table::<Self>(ids, db, table_name)
            .await
    }

    async fn batch_upsert(models: &[Self], db: &Database) -> Result<()> {
        crate::operations::CrudOperations::batch_upsert(models, db).await
    }

    async fn batch_upsert_with_table(
        models: &[Self],
        db: &Database,
        table_name: &str,
    ) -> Result<()> {
        crate::operations::CrudOperations::batch_upsert_with_table(models, db, table_name).await
    }

    // Find operations
    async fn find_one(filter: FilterOperator, db: &Database) -> Result<Option<Self>> {
        crate::operations::CrudOperations::find_one::<Self>(filter, db).await
    }

    async fn find_one_with_table(
        filter: FilterOperator,
        db: &Database,
        table_name: &str,
    ) -> Result<Option<Self>> {
        crate::operations::CrudOperations::find_one_with_table::<Self>(filter, db, table_name).await
    }

    async fn find_latest<T>(db: &Database) -> Result<Option<T>>
    where
        T: crate::Orso,
    {
        Self::find_latest_with_table(db, T::table_name()).await
    }

    async fn find_latest_with_table<T>(db: &Database, table_name: &str) -> Result<Option<T>>
    where
        T: crate::Orso,
    {
        crate::operations::CrudOperations::find_latest_with_table::<T>(db, table_name).await
    }

    async fn find_latest_filter(filter: FilterOperator, db: &Database) -> Result<Option<Self>> {
        crate::operations::CrudOperations::find_latest_filter::<Self>(filter, db).await
    }

    async fn find_latest_filter_with_table(
        filter: FilterOperator,
        db: &Database,
        table_name: &str,
    ) -> Result<Option<Self>> {
        crate::operations::CrudOperations::find_latest_filter_with_table::<Self>(
            filter, db, table_name,
        )
        .await
    }

    async fn find_first_filter(filter: FilterOperator, db: &Database) -> Result<Option<Self>> {
        crate::operations::CrudOperations::find_first_filter::<Self>(filter, db).await
    }

    async fn find_first_filter_with_table(
        filter: FilterOperator,
        db: &Database,
        table_name: &str,
    ) -> Result<Option<Self>> {
        crate::operations::CrudOperations::find_first_filter_with_table::<Self>(
            filter, db, table_name,
        )
        .await
    }

    async fn exists(db: &Database) -> Result<bool> {
        crate::operations::CrudOperations::exists::<Self>(db).await
    }

    async fn exists_with_table(db: &Database, table_name: &str) -> Result<bool> {
        crate::operations::CrudOperations::exists_with_table::<Self>(db, table_name).await
    }

    async fn exists_filter(filter: FilterOperator, db: &Database) -> Result<bool> {
        crate::operations::CrudOperations::exists_filter::<Self>(filter, db).await
    }

    async fn exists_filter_with_table(
        filter: FilterOperator,
        db: &Database,
        table_name: &str,
    ) -> Result<bool> {
        crate::operations::CrudOperations::exists_filter_with_table::<Self>(filter, db, table_name)
            .await
    }

    async fn find_by_field(field: &str, value: crate::Value, db: &Database) -> Result<Vec<Self>> {
        crate::operations::CrudOperations::find_by_field::<Self>(field, value, db).await
    }

    async fn find_by_field_with_table(
        field: &str,
        value: crate::Value,
        db: &Database,
        table_name: &str,
    ) -> Result<Vec<Self>> {
        crate::operations::CrudOperations::find_by_field_with_table::<Self>(
            field, value, db, table_name,
        )
        .await
    }

    async fn find_latest_by_field(
        field: &str,
        value: crate::Value,
        db: &Database,
    ) -> Result<Option<Self>> {
        crate::operations::CrudOperations::find_latest_by_field::<Self>(field, value, db).await
    }

    async fn find_latest_by_field_with_table(
        field: &str,
        value: crate::Value,
        db: &Database,
        table_name: &str,
    ) -> Result<Option<Self>> {
        crate::operations::CrudOperations::find_latest_by_field_with_table::<Self>(
            field, value, db, table_name,
        )
        .await
    }

    async fn find_first_by_field(
        field: &str,
        value: crate::Value,
        db: &Database,
    ) -> Result<Option<Self>> {
        crate::operations::CrudOperations::find_first_by_field::<Self>(field, value, db).await
    }

    async fn find_first_by_field_with_table(
        field: &str,
        value: crate::Value,
        db: &Database,
        table_name: &str,
    ) -> Result<Option<Self>> {
        crate::operations::CrudOperations::find_first_by_field_with_table::<Self>(
            field, value, db, table_name,
        )
        .await
    }

    async fn find_by_ids(ids: &[&str], db: &Database) -> Result<Vec<Self>> {
        crate::operations::CrudOperations::find_by_ids::<Self>(ids, db).await
    }

    async fn find_by_ids_with_table(
        ids: &[&str],
        db: &Database,
        table_name: &str,
    ) -> Result<Vec<Self>> {
        crate::operations::CrudOperations::find_by_ids_with_table::<Self>(ids, db, table_name).await
    }

    async fn find_by_field_in(
        field: &str,
        values: &[crate::Value],
        db: &Database,
    ) -> Result<Vec<Self>> {
        crate::operations::CrudOperations::find_by_field_in::<Self>(field, values, db).await
    }

    async fn find_by_field_in_with_table(
        field: &str,
        values: &[crate::Value],
        db: &Database,
        table_name: &str,
    ) -> Result<Vec<Self>> {
        crate::operations::CrudOperations::find_by_field_in_with_table::<Self>(
            field, values, db, table_name,
        )
        .await
    }

    async fn find_paginated(
        pagination: &crate::Pagination,
        db: &Database,
    ) -> Result<crate::PaginatedResult<Self>> {
        crate::operations::CrudOperations::find_paginated::<Self>(pagination, db).await
    }

    async fn find_paginated_with_table(
        pagination: &crate::Pagination,
        db: &Database,
        table_name: &str,
    ) -> Result<crate::PaginatedResult<Self>> {
        crate::operations::CrudOperations::find_paginated_with_table::<Self>(
            pagination, db, table_name,
        )
        .await
    }

    async fn find_where_paginated(
        filter: FilterOperator,
        pagination: &crate::Pagination,
        db: &Database,
    ) -> Result<crate::PaginatedResult<Self>> {
        crate::operations::CrudOperations::find_where_paginated::<Self>(filter, pagination, db)
            .await
    }

    async fn find_where_paginated_with_table(
        filter: FilterOperator,
        pagination: &crate::Pagination,
        db: &Database,
        table_name: &str,
    ) -> Result<crate::PaginatedResult<Self>> {
        crate::operations::CrudOperations::find_where_paginated_with_table::<Self>(
            filter, pagination, db, table_name,
        )
        .await
    }

    // Search operations
    async fn search(
        search_filter: &crate::SearchFilter,
        pagination: Option<&crate::Pagination>,
        db: &Database,
    ) -> Result<crate::PaginatedResult<Self>> {
        crate::operations::CrudOperations::search::<Self>(search_filter, pagination, db).await
    }

    async fn search_with_table(
        search_filter: &crate::SearchFilter,
        pagination: Option<&crate::Pagination>,
        db: &Database,
        table_name: &str,
    ) -> Result<crate::PaginatedResult<Self>> {
        crate::operations::CrudOperations::search_with_table::<Self>(
            search_filter,
            pagination,
            db,
            table_name,
        )
        .await
    }

    // Count operations
    async fn count_where(filter: FilterOperator, db: &Database) -> Result<u64> {
        crate::operations::CrudOperations::count_where::<Self>(filter, db).await
    }

    async fn count_where_with_table(
        filter: FilterOperator,
        db: &Database,
        table_name: &str,
    ) -> Result<u64> {
        crate::operations::CrudOperations::count_where_with_table::<Self>(filter, db, table_name)
            .await
    }

    // Delete operations
    async fn delete_where(filter: FilterOperator, db: &Database) -> Result<u64> {
        crate::operations::CrudOperations::delete_where::<Self>(filter, db).await
    }

    async fn delete_where_with_table(
        filter: FilterOperator,
        db: &Database,
        table_name: &str,
    ) -> Result<u64> {
        crate::operations::CrudOperations::delete_where_with_table::<Self>(filter, db, table_name)
            .await
    }

    // List operations with sorting
    async fn list(
        sort: Option<Vec<crate::Sort>>,
        pagination: Option<&crate::Pagination>,
        db: &Database,
    ) -> Result<crate::PaginatedResult<Self>> {
        crate::operations::CrudOperations::list::<Self>(sort, pagination, db).await
    }

    async fn list_with_table(
        sort: Option<Vec<crate::Sort>>,
        pagination: Option<&crate::Pagination>,
        db: &Database,
        table_name: &str,
    ) -> Result<crate::PaginatedResult<Self>> {
        crate::operations::CrudOperations::list_with_table::<Self>(sort, pagination, db, table_name)
            .await
    }

    async fn list_where(
        filter: FilterOperator,
        sort: Option<Vec<crate::Sort>>,
        pagination: Option<&crate::Pagination>,
        db: &Database,
    ) -> Result<crate::PaginatedResult<Self>> {
        crate::operations::CrudOperations::list_where::<Self>(filter, sort, pagination, db).await
    }

    async fn list_where_with_table(
        filter: FilterOperator,
        sort: Option<Vec<crate::Sort>>,
        pagination: Option<&crate::Pagination>,
        db: &Database,
        table_name: &str,
    ) -> Result<crate::PaginatedResult<Self>> {
        crate::operations::CrudOperations::list_where_with_table::<Self>(
            filter, sort, pagination, db, table_name,
        )
        .await
    }

    // Custom query operations
    async fn query(builder: crate::QueryBuilder, db: &Database) -> Result<Vec<Self>> {
        crate::operations::CrudOperations::query::<Self>(builder, db).await
    }

    async fn query_with_table(builder: crate::QueryBuilder, db: &Database) -> Result<Vec<Self>> {
        crate::operations::CrudOperations::query_with_table::<Self>(builder, db).await
    }

    async fn query_paginated(
        builder: crate::QueryBuilder,
        pagination: &crate::Pagination,
        db: &Database,
    ) -> Result<crate::PaginatedResult<Self>> {
        crate::operations::CrudOperations::query_paginated::<Self>(builder, pagination, db).await
    }

    async fn query_paginated_with_table(
        builder: crate::QueryBuilder,
        pagination: &crate::Pagination,
        db: &Database,
    ) -> Result<crate::PaginatedResult<Self>> {
        crate::operations::CrudOperations::query_paginated_with_table::<Self>(
            builder, pagination, db,
        )
        .await
    }

    // Aggregate operations
    async fn aggregate(
        function: crate::Aggregate,
        column: &str,
        filter: Option<FilterOperator>,
        db: &Database,
    ) -> Result<Option<f64>> {
        crate::operations::CrudOperations::aggregate::<Self>(function, column, filter, db).await
    }

    async fn aggregate_with_table(
        function: crate::Aggregate,
        column: &str,
        filter: Option<FilterOperator>,
        db: &Database,
        table_name: &str,
    ) -> Result<Option<f64>> {
        crate::operations::CrudOperations::aggregate_with_table::<Self>(
            function, column, filter, db, table_name,
        )
        .await
    }

    // Legacy batch operations (for compatibility)
    async fn batch_insert(records: &[Self], db: &Database) -> Result<u64> {
        Self::batch_create(records, db).await?;
        Ok(records.len() as u64)
    }

    // Filter operations
    fn build_filter_operator(filter: &FilterOperator) -> Result<(String, Vec<libsql::Value>)> {
        crate::filters::FilterOperations::build_filter_operator(filter)
    }

    fn build_filter(filter: &crate::Filter) -> Result<(String, Vec<libsql::Value>)> {
        crate::filters::FilterOperations::build_filter(filter)
    }

    // Conversion functions with default implementations
    fn row_to_map(row: &libsql::Row) -> Result<HashMap<String, crate::Value>> {
        crate::operations::CrudOperations::row_to_map(row)
    }

    fn value_to_libsql_value(value: &crate::Value) -> libsql::Value {
        crate::Utils::value_to_libsql_value(value)
    }

    fn libsql_value_to_value(value: &libsql::Value) -> crate::Value {
        crate::Utils::libsql_value_to_value(value)
    }
}