che-orm 0.3.1

Typed SQLite and PostgreSQL ORM for che-rest applications
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
use std::marker::PhantomData;

pub(crate) fn schema_object_name(table: &str, kind: &str, parts: &[&str]) -> String {
    let mut hash = 0xcbf29ce484222325_u64;
    for part in std::iter::once(table)
        .chain(std::iter::once(kind))
        .chain(parts.iter().copied())
    {
        for byte in part.bytes().chain(std::iter::once(0)) {
            hash = (hash ^ u64::from(byte)).wrapping_mul(0x100000001b3);
        }
    }
    let prefix = format!("{table}_{kind}");
    let mut end = prefix.len().min(46);
    while !prefix.is_char_boundary(end) {
        end -= 1;
    }
    format!("{}_{hash:016x}", &prefix[..end])
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// SQL column types supported by the schema compiler.
pub enum ColumnType {
    Integer,
    Real,
    Text,
    Binary,
    Boolean,
    DateTime,
    Uuid,
}

/// Maps a Rust field type to a SQL column type.
pub trait ColumnTypeOf {
    fn column_type() -> ColumnType;
    fn nullable() -> bool {
        false
    }
    fn choices() -> Option<Vec<&'static str>> {
        None
    }
}

impl ColumnTypeOf for i64 {
    fn column_type() -> ColumnType {
        ColumnType::Integer
    }
}

impl ColumnTypeOf for uuid::Uuid {
    fn column_type() -> ColumnType {
        ColumnType::Uuid
    }
}

impl ColumnTypeOf for f64 {
    fn column_type() -> ColumnType {
        ColumnType::Real
    }
}

impl ColumnTypeOf for String {
    fn column_type() -> ColumnType {
        ColumnType::Text
    }
}

impl ColumnTypeOf for Vec<u8> {
    fn column_type() -> ColumnType {
        ColumnType::Binary
    }
}

impl ColumnTypeOf for bool {
    fn column_type() -> ColumnType {
        ColumnType::Boolean
    }
}

impl ColumnTypeOf for time::OffsetDateTime {
    fn column_type() -> ColumnType {
        ColumnType::DateTime
    }
}

impl<T: ColumnTypeOf> ColumnTypeOf for Option<T> {
    fn column_type() -> ColumnType {
        T::column_type()
    }
    fn nullable() -> bool {
        true
    }
    fn choices() -> Option<Vec<&'static str>> {
        T::choices()
    }
}

/// A string-backed enum that can be stored in a database column.
pub trait DbEnum: Sized {
    const VALUES: &'static [&'static str];

    fn as_str(&self) -> &'static str;
    fn from_str(value: &str) -> Option<Self>;
}

impl<T: DbEnum> ColumnTypeOf for T {
    fn column_type() -> ColumnType {
        ColumnType::Text
    }

    fn choices() -> Option<Vec<&'static str>> {
        Some(T::VALUES.to_vec())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Foreign key metadata attached to a column.
pub struct ForeignKey {
    target: String,
    on_delete: Option<&'static str>,
}

impl ForeignKey {
    pub fn new(target: impl Into<String>, on_delete: Option<&'static str>) -> Self {
        Self {
            target: target.into(),
            on_delete,
        }
    }

    pub fn target(&self) -> &str {
        &self.target
    }

    pub fn target_parts(&self) -> Option<(&str, &str)> {
        self.target
            .split_once('(')
            .and_then(|(table, column)| column.strip_suffix(')').map(|column| (table, column)))
    }

    pub fn on_delete(&self) -> Option<&'static str> {
        self.on_delete
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Metadata for one table column.
pub struct ColumnSchema {
    /// Rust field name used to identify this column in migration state.
    pub field_name: &'static str,
    /// Physical column name used in SQL.
    pub name: &'static str,
    pub column_type: ColumnType,
    pub nullable: bool,
    pub primary_key: bool,
    pub unique: bool,
    pub default: Option<&'static str>,
    pub check: Option<&'static str>,
    pub choices: Option<Vec<&'static str>>,
    pub references: Option<ForeignKey>,
    pub auto_now_add: bool,
    pub auto_now: bool,
}

impl ColumnSchema {
    pub fn new(name: &'static str, column_type: ColumnType, nullable: bool) -> Self {
        Self {
            field_name: name,
            name,
            column_type,
            nullable,
            primary_key: false,
            unique: false,
            default: None,
            check: None,
            choices: None,
            references: None,
            auto_now_add: false,
            auto_now: false,
        }
    }

    /// Associates a Rust field name with a physical SQL column name.
    pub fn with_field_name(mut self, field_name: &'static str) -> Self {
        self.field_name = field_name;
        self
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Complete model table metadata used for DDL generation.
pub struct TableSchema {
    pub name: &'static str,
    pub columns: Vec<ColumnSchema>,
    pub unique_constraints: Vec<Vec<&'static str>>,
    pub indexes: Vec<Vec<&'static str>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Errors found in model-generated schema metadata.
pub enum SchemaError {
    InvalidIdentifier(String),
    DuplicateTable(String),
    DuplicateColumn { table: String, column: String },
    UnknownColumn { table: String, column: String },
    InvalidForeignKey(String),
    InvalidOnDelete(String),
    IncompatibleSyncRoles,
}

impl TableSchema {
    /// Validates identifiers, indexes, unique constraints and foreign keys.
    pub fn validate(&self) -> Result<(), SchemaError> {
        validate_identifier(self.name)?;
        let mut columns = std::collections::HashSet::new();
        for column in &self.columns {
            validate_identifier(column.name)?;
            if !columns.insert(column.name) {
                return Err(SchemaError::DuplicateColumn {
                    table: self.name.into(),
                    column: column.name.into(),
                });
            }
            if let Some(reference) = &column.references {
                validate_reference(reference.target())?;
                if let Some(action) = reference.on_delete()
                    && !matches!(
                        action.to_ascii_lowercase().as_str(),
                        "cascade" | "restrict" | "no action" | "set null" | "set default"
                    )
                {
                    return Err(SchemaError::InvalidOnDelete(action.into()));
                }
            }
        }
        for group in self.unique_constraints.iter().chain(self.indexes.iter()) {
            for column in group {
                validate_identifier(column)?;
                if !columns.contains(column) {
                    return Err(SchemaError::UnknownColumn {
                        table: self.name.into(),
                        column: (*column).into(),
                    });
                }
            }
        }
        Ok(())
    }
}

impl SchemaSet {
    /// Validates every registered table and rejects duplicate table names.
    pub fn validate(&self) -> Result<(), SchemaError> {
        if let Some(first) = self.sync_models.first()
            && self
                .sync_models
                .iter()
                .any(|model| model.role != first.role)
        {
            return Err(SchemaError::IncompatibleSyncRoles);
        }
        let mut tables = std::collections::HashSet::new();
        for table in &self.tables {
            table.validate()?;
            if !tables.insert(table.name) {
                return Err(SchemaError::DuplicateTable(table.name.into()));
            }
        }
        for table in &self.tables {
            for column in &table.columns {
                let Some(reference) = &column.references else {
                    continue;
                };
                let (target_table, target_column) = reference
                    .target
                    .split_once('(')
                    .and_then(|(table, column)| {
                        column.strip_suffix(')').map(|column| (table, column))
                    })
                    .ok_or_else(|| SchemaError::InvalidForeignKey(reference.target().into()))?;
                let target = self
                    .tables
                    .iter()
                    .find(|candidate| candidate.name == target_table)
                    .ok_or_else(|| SchemaError::InvalidForeignKey(reference.target().into()))?;
                if !target
                    .columns
                    .iter()
                    .any(|candidate| candidate.name == target_column)
                {
                    return Err(SchemaError::UnknownColumn {
                        table: target_table.into(),
                        column: target_column.into(),
                    });
                }
            }
        }
        Ok(())
    }

    /// Exports SQL and returns schema validation errors instead of panicking.
    pub fn try_to_sql<D: crate::SqlDialect>(&self) -> Result<String, SchemaError> {
        self.validate()?;
        Ok(self.to_sql_unchecked::<D>())
    }
}

pub(crate) fn validate_identifier(identifier: &str) -> Result<(), SchemaError> {
    let mut chars = identifier.chars();
    let valid_start = chars
        .next()
        .is_some_and(|ch| ch == '_' || ch.is_ascii_alphabetic());
    if valid_start && chars.all(|ch| ch == '_' || ch.is_ascii_alphanumeric()) {
        Ok(())
    } else {
        Err(SchemaError::InvalidIdentifier(identifier.into()))
    }
}

fn validate_reference(reference: &str) -> Result<(), SchemaError> {
    let Some((table, column)) = reference.split_once('(') else {
        return Err(SchemaError::InvalidForeignKey(reference.into()));
    };
    let Some(column) = column.strip_suffix(')') else {
        return Err(SchemaError::InvalidForeignKey(reference.into()));
    };
    validate_identifier(table)?;
    validate_identifier(column)
}

/// A collection of model schemas exported as one SQL schema.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct SchemaSet {
    tables: Vec<TableSchema>,
    model_names: Vec<&'static str>,
    sync_models: Vec<crate::SyncRegistration>,
}

impl SchemaSet {
    /// Creates an empty schema collection.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns registered tables in schema order.
    pub fn tables(&self) -> &[TableSchema] {
        &self.tables
    }

    /// Returns model names parallel to [`SchemaSet::tables`].
    pub fn model_names(&self) -> &[&'static str] {
        &self.model_names
    }

    /// Adds a model schema in dependency order.
    pub fn model<M: crate::Model>(mut self) -> Self {
        self.tables.push(M::schema());
        self.model_names.push(M::model_name());
        self
    }

    /// Register the local editable projection and its confirmed server shadow.
    pub fn client_model<M: crate::SyncModel>(self) -> Self {
        self.sync_model::<M>(crate::ModelRole::Client)
    }

    /// Register authoritative server data, without a shadow table.
    pub fn server_model<M: crate::SyncModel>(self) -> Self {
        self.sync_model::<M>(crate::ModelRole::Server)
    }

    fn sync_model<M: crate::SyncModel>(mut self, role: crate::ModelRole) -> Self {
        let registration = crate::SyncRegistration::new::<M>(role);
        self.tables.push(registration.schema.clone());
        self.model_names.push(M::model_name());
        self.sync_models.push(registration);
        self
    }

    pub fn sync_models(&self) -> &[crate::SyncRegistration] {
        &self.sync_models
    }

    /// Merges another application schema after the current tables.
    pub fn merge(mut self, other: Self) -> Self {
        self.tables.extend(other.tables);
        self.model_names.extend(other.model_names);
        self.sync_models.extend(other.sync_models);
        self
    }

    /// Exports the desired schema as semicolon-delimited SQL.
    pub fn to_sql<D: crate::SqlDialect>(&self) -> String {
        self.validate().expect("invalid model schema");
        self.to_sql_unchecked::<D>()
    }

    fn to_sql_unchecked<D: crate::SqlDialect>(&self) -> String {
        let mut statements = Vec::new();
        for table in &self.tables {
            let compiled = crate::SqlCompiler::<D>::compile_schema(table);
            statements.push(compiled.table);
            statements.extend(compiled.indexes);
        }
        for operation in
            crate::sync_migration_operations(&self.sync_models).expect("validated sync roles")
        {
            if let crate::MigrationOperation::CreateModel { model } = operation {
                let compiled = crate::SqlCompiler::<D>::compile_table_state(&model.table);
                statements.push(compiled.table);
                statements.extend(compiled.indexes);
            }
        }
        statements.join(";\n") + if statements.is_empty() { "" } else { ";\n" }
    }
}

/// Application-level model registration, similar to a Django app config.
pub trait AppConfig {
    /// Stable application label used in diagnostics and registration.
    fn name() -> &'static str;

    /// Returns models owned by this application in dependency order.
    fn schema() -> SchemaSet;
}

/// Registry that combines schemas from multiple application modules.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct AppRegistry {
    apps: Vec<&'static str>,
    schema: SchemaSet,
    app_schemas: Vec<(&'static str, SchemaSet)>,
}

impl AppRegistry {
    /// Creates an empty application registry.
    pub fn new() -> Self {
        Self::default()
    }

    /// Registers one application and appends its models to the schema.
    pub fn register<A: AppConfig>(mut self) -> Self {
        self.apps.push(A::name());
        let schema = A::schema();
        self.schema = self.schema.merge(schema.clone());
        self.app_schemas.push((A::name(), schema));
        self
    }

    /// Returns registered application labels in registration order.
    pub fn apps(&self) -> &[&'static str] {
        &self.apps
    }

    /// Returns the combined schema.
    pub fn schema(&self) -> &SchemaSet {
        &self.schema
    }

    /// Returns schemas by owning app, preserving registration order for
    /// migration-state construction.
    pub fn app_schemas(&self) -> impl Iterator<Item = (&'static str, &SchemaSet)> {
        self.app_schemas
            .iter()
            .map(|(name, schema)| (*name, schema))
    }

    /// Exports all registered models as SQL.
    pub fn to_sql<D: crate::SqlDialect>(&self) -> String {
        self.schema.to_sql::<D>()
    }
}

#[derive(Debug, Clone)]
/// AST node for a `CREATE TABLE` statement.
pub struct CreateTableAst {
    pub schema: TableSchema,
}

/// Builder for a model's `CREATE TABLE` statement.
pub struct CreateTableQuery<M> {
    ast: CreateTableAst,
    _model: PhantomData<M>,
}

impl<M> CreateTableQuery<M> {
    pub fn new(schema: TableSchema) -> Self {
        Self {
            ast: CreateTableAst { schema },
            _model: PhantomData,
        }
    }

    pub fn into_ast(self) -> crate::QueryAst {
        crate::QueryAst::CreateTable(self.ast)
    }
}