rullst-orm 12.3.0

Fast, un-opinionated, database-agnostic ORM and query builder for Rust
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
use std::marker::PhantomData;

use super::{
    PolyglotError, TursoCodec, TursoModel, TursoQueryLimit, TursoRow, TursoStatement, TursoStore,
    TursoValue,
};

const DEFAULT_QUERY_LIMIT: u32 = 500;
const MAX_FILTERS: usize = 64;

/// Validated sort direction for a Turso model query.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum TursoOrder {
    /// Ascending order.
    Asc,
    /// Descending order.
    Desc,
}

impl TursoOrder {
    const fn sql(self) -> &'static str {
        match self {
            Self::Asc => "ASC",
            Self::Desc => "DESC",
        }
    }
}

/// A typed repository bound to one explicit Turso store.
pub struct TursoRepository<'store, Model> {
    store: &'store TursoStore,
    marker: PhantomData<Model>,
}

impl<'store, Model> Clone for TursoRepository<'store, Model> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<Model> Copy for TursoRepository<'_, Model> {}

impl<'store, Model> TursoRepository<'store, Model>
where
    Model: TursoModel,
{
    pub(super) const fn new(store: &'store TursoStore) -> Self {
        Self {
            store,
            marker: PhantomData,
        }
    }

    /// Starts a bounded, parameterized query for this model.
    pub fn query(self) -> TursoQuery<'store, Model> {
        TursoQuery::new(self.store)
    }

    /// Returns at most 10,000 records in primary-key order.
    pub async fn all(self) -> Result<Vec<Model>, PolyglotError> {
        self.query()
            .order_by(Model::primary_key_column(), TursoOrder::Asc)?
            .limit(10_000)?
            .get()
            .await
    }

    /// Finds one model by primary key.
    pub async fn find<Key>(self, key: Key) -> Result<Option<Model>, PolyglotError>
    where
        Key: TursoCodec,
    {
        self.query()
            .where_eq(Model::primary_key_column(), &key)?
            .first()
            .await
    }

    /// Inserts a new model or updates a model whose primary key is set.
    pub async fn save(self, model: &mut Model) -> Result<(), PolyglotError> {
        if model.primary_key_is_unset() {
            self.insert(model, false).await
        } else {
            self.update(model).await
        }
    }

    /// Inserts a model even when an application-assigned primary key is set.
    pub async fn create(self, model: &mut Model) -> Result<(), PolyglotError> {
        self.insert(model, !model.primary_key_is_unset()).await
    }

    /// Deletes one model by its primary key.
    pub async fn delete(self, model: &Model) -> Result<(), PolyglotError> {
        if model.primary_key_is_unset() {
            return Err(PolyglotError::NotFound);
        }
        let sql = format!(
            "DELETE FROM {} WHERE {} = ?1",
            quoted(Model::table_name()),
            quoted(Model::primary_key_column())
        );
        let affected = self
            .store
            .execute(TursoStatement::new(sql, vec![model.primary_key_value()?])?)
            .await?;
        if affected == 0 {
            return Err(PolyglotError::NotFound);
        }
        Ok(())
    }

    async fn insert(self, model: &mut Model, include_primary: bool) -> Result<(), PolyglotError> {
        let columns = Model::columns();
        let values = model.encode_turso()?;
        validate_shape(columns, &values)?;
        let primary_index = primary_index::<Model>()?;
        let insert_columns = columns
            .iter()
            .enumerate()
            .filter(|(index, _)| include_primary || *index != primary_index)
            .map(|(_, column)| quoted(column))
            .collect::<Vec<_>>();
        let parameters = values
            .into_iter()
            .enumerate()
            .filter(|(index, _)| include_primary || *index != primary_index)
            .map(|(_, value)| value)
            .collect::<Vec<_>>();
        let sql = if insert_columns.is_empty() {
            format!(
                "INSERT INTO {} DEFAULT VALUES RETURNING {}",
                quoted(Model::table_name()),
                quoted(Model::primary_key_column())
            )
        } else {
            let placeholders = (1..=insert_columns.len())
                .map(|index| format!("?{index}"))
                .collect::<Vec<_>>()
                .join(", ");
            format!(
                "INSERT INTO {} ({}) VALUES ({}) RETURNING {}",
                quoted(Model::table_name()),
                insert_columns.join(", "),
                placeholders,
                quoted(Model::primary_key_column())
            )
        };
        let rows = self
            .store
            .query(
                TursoStatement::new(sql, parameters)?,
                TursoQueryLimit::new(1)?,
            )
            .await?;
        let row = rows.first().ok_or_else(|| PolyglotError::Driver {
            backend: "Turso",
            message: "insert did not return a primary key".to_owned(),
        })?;
        let value = required_cell(row, Model::primary_key_column())?;
        model.assign_primary_key(value)
    }

    async fn update(self, model: &Model) -> Result<(), PolyglotError> {
        let columns = Model::columns();
        let values = model.encode_turso()?;
        validate_shape(columns, &values)?;
        let primary_index = primary_index::<Model>()?;
        let mut parameters = Vec::with_capacity(values.len());
        let mut assignments = Vec::with_capacity(values.len().saturating_sub(1));
        for (index, (column, value)) in columns.iter().zip(values).enumerate() {
            if index == primary_index {
                continue;
            }
            parameters.push(value);
            assignments.push(format!("{} = ?{}", quoted(column), parameters.len()));
        }
        if assignments.is_empty() {
            return Err(PolyglotError::InvalidIdentifier {
                kind: "Turso model",
                reason: "an update requires at least one non-primary-key column",
            });
        }
        parameters.push(model.primary_key_value()?);
        let sql = format!(
            "UPDATE {} SET {} WHERE {} = ?{}",
            quoted(Model::table_name()),
            assignments.join(", "),
            quoted(Model::primary_key_column()),
            parameters.len()
        );
        let affected = self
            .store
            .execute(TursoStatement::new(sql, parameters)?)
            .await?;
        if affected == 0 {
            return Err(PolyglotError::NotFound);
        }
        Ok(())
    }
}

/// Fluent, parameterized query builder for a typed Turso model.
pub struct TursoQuery<'store, Model> {
    store: &'store TursoStore,
    filters: Vec<(&'static str, TursoValue)>,
    order: Option<(&'static str, TursoOrder)>,
    limit: u32,
    offset: u64,
    marker: PhantomData<Model>,
}

impl<'store, Model> TursoQuery<'store, Model>
where
    Model: TursoModel,
{
    fn new(store: &'store TursoStore) -> Self {
        Self {
            store,
            filters: Vec::new(),
            order: None,
            limit: DEFAULT_QUERY_LIMIT,
            offset: 0,
            marker: PhantomData,
        }
    }

    /// Adds one equality predicate after validating the model column.
    pub fn where_eq<Value>(mut self, column: &str, value: &Value) -> Result<Self, PolyglotError>
    where
        Value: TursoCodec,
    {
        if self.filters.len() >= MAX_FILTERS {
            return Err(PolyglotError::InvalidIdentifier {
                kind: "Turso query",
                reason: "a query accepts at most 64 filters",
            });
        }
        let column = model_column::<Model>(column)?;
        self.filters.push((column, value.encode_turso()?));
        Ok(self)
    }

    /// Selects one validated model column for deterministic ordering.
    pub fn order_by(mut self, column: &str, order: TursoOrder) -> Result<Self, PolyglotError> {
        self.order = Some((model_column::<Model>(column)?, order));
        Ok(self)
    }

    /// Sets a mandatory 1–10,000 materialization and SQL limit.
    pub fn limit(mut self, limit: u32) -> Result<Self, PolyglotError> {
        TursoQueryLimit::new(limit)?;
        self.limit = limit;
        Ok(self)
    }

    /// Sets a checked SQL offset.
    pub fn offset(mut self, offset: u64) -> Result<Self, PolyglotError> {
        if offset > i64::MAX as u64 {
            return Err(PolyglotError::InvalidIdentifier {
                kind: "Turso query offset",
                reason: "offset must fit in a signed 64-bit integer",
            });
        }
        self.offset = offset;
        Ok(self)
    }

    /// Executes and decodes the query.
    pub async fn get(self) -> Result<Vec<Model>, PolyglotError> {
        let (sql, parameters) = self.select_sql();
        let rows = self
            .store
            .query(
                TursoStatement::new(sql, parameters)?,
                TursoQueryLimit::new(self.limit)?,
            )
            .await?;
        rows.iter().map(Model::decode_turso).collect()
    }

    /// Returns the first matching model.
    pub async fn first(mut self) -> Result<Option<Model>, PolyglotError> {
        self.limit = 1;
        self.get().await.map(|rows| rows.into_iter().next())
    }

    /// Counts rows matching the current predicates.
    pub async fn count(self) -> Result<u64, PolyglotError> {
        let (where_sql, parameters) = self.where_sql();
        let sql = format!(
            "SELECT COUNT(*) AS rullst_count FROM {}{}",
            quoted(Model::table_name()),
            where_sql
        );
        let rows = self
            .store
            .query(
                TursoStatement::new(sql, parameters)?,
                TursoQueryLimit::new(1)?,
            )
            .await?;
        match rows.first().and_then(|row| row.get("rullst_count")) {
            Some(TursoValue::Integer(value)) => {
                u64::try_from(*value).map_err(PolyglotError::serialization)
            }
            Some(other) => Err(PolyglotError::Serialization(format!(
                "expected INTEGER count, received {}",
                other.kind_name()
            ))),
            None => Err(PolyglotError::Driver {
                backend: "Turso",
                message: "count query returned no value".to_owned(),
            }),
        }
    }

    fn select_sql(&self) -> (String, Vec<TursoValue>) {
        let columns = Model::columns()
            .iter()
            .map(|column| quoted(column))
            .collect::<Vec<_>>()
            .join(", ");
        let (where_sql, parameters) = self.where_sql();
        let order_sql = self
            .order
            .map(|(column, order)| format!(" ORDER BY {} {}", quoted(column), order.sql()))
            .unwrap_or_default();
        (
            format!(
                "SELECT {columns} FROM {}{where_sql}{order_sql} LIMIT {} OFFSET {}",
                quoted(Model::table_name()),
                self.limit,
                self.offset
            ),
            parameters,
        )
    }

    fn where_sql(&self) -> (String, Vec<TursoValue>) {
        if self.filters.is_empty() {
            return (String::new(), Vec::new());
        }
        let predicates = self
            .filters
            .iter()
            .enumerate()
            .map(|(index, (column, _))| format!("{} = ?{}", quoted(column), index + 1))
            .collect::<Vec<_>>()
            .join(" AND ");
        let parameters = self
            .filters
            .iter()
            .map(|(_, value)| value.clone())
            .collect();
        (format!(" WHERE {predicates}"), parameters)
    }
}

impl TursoStore {
    /// Creates a typed model repository over this store.
    pub fn models<Model>(&self) -> TursoRepository<'_, Model>
    where
        Model: TursoModel,
    {
        TursoRepository::new(self)
    }
}

fn validate_shape(columns: &[&str], values: &[TursoValue]) -> Result<(), PolyglotError> {
    if columns.is_empty() || columns.len() != values.len() {
        return Err(PolyglotError::InvalidIdentifier {
            kind: "Turso model",
            reason: "column and encoded-value counts must match and be non-empty",
        });
    }
    Ok(())
}

fn primary_index<Model>() -> Result<usize, PolyglotError>
where
    Model: TursoModel,
{
    Model::columns()
        .iter()
        .position(|column| *column == Model::primary_key_column())
        .ok_or(PolyglotError::InvalidIdentifier {
            kind: "Turso model",
            reason: "primary key must be part of the model columns",
        })
}

fn model_column<Model>(requested: &str) -> Result<&'static str, PolyglotError>
where
    Model: TursoModel,
{
    Model::columns()
        .iter()
        .copied()
        .find(|column| *column == requested)
        .ok_or(PolyglotError::InvalidIdentifier {
            kind: "Turso model column",
            reason: "column is not declared by the model",
        })
}

fn quoted(identifier: &str) -> String {
    format!("\"{}\"", identifier.replace('"', "\"\""))
}

fn required_cell<'row>(
    row: &'row TursoRow,
    column: &str,
) -> Result<&'row TursoValue, PolyglotError> {
    row.get(column).ok_or_else(|| PolyglotError::Driver {
        backend: "Turso",
        message: format!("result is missing required column {column}"),
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn manual_model_identifiers_are_safely_quoted() {
        assert_eq!(quoted("users"), "\"users\"");
        assert_eq!(
            quoted("users\"; DROP TABLE audit; --"),
            "\"users\"\"; DROP TABLE audit; --\""
        );
    }
}