evenframe_core 0.1.4

Core functionality for Evenframe - TypeScript type generation and database schema synchronization
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
#[cfg(feature = "surrealdb")]
use crate::schemasync::TableConfig;
#[cfg(feature = "surrealdb")]
use crate::types::StructField;
use crate::types::{EnumRepresentation, FieldType, StructConfig, TaggedUnion, VariantData};
use convert_case::{Case, Casing};
use rand::{rng, seq::IndexedRandom};
use std::collections::HashMap;
use tracing::{debug, trace};

pub fn field_type_to_default_value(
    field_type: &FieldType,
    structs: &HashMap<String, StructConfig>,
    enums: &HashMap<String, TaggedUnion>,
    registry: &crate::types::ForeignTypeRegistry,
) -> String {
    trace!("Generating default value for field type: {:?}", field_type);
    let result = match field_type {
        FieldType::String | FieldType::Char => {
            trace!("Generating default for String/Char type");
            r#""""#.to_string()
        }
        FieldType::Bool => {
            trace!("Generating default for Bool type");
            "false".to_string()
        }
        FieldType::Unit => {
            trace!("Generating default for Unit type");
            "undefined".to_string()
        }
        FieldType::F32
        | FieldType::F64
        | FieldType::I8
        | FieldType::I16
        | FieldType::I32
        | FieldType::I64
        | FieldType::I128
        | FieldType::Isize
        | FieldType::U8
        | FieldType::U16
        | FieldType::U32
        | FieldType::U64
        | FieldType::U128
        | FieldType::Usize => {
            trace!("Generating default for numeric type");
            "0".to_string()
        }
        FieldType::Tuple(inner_types) => {
            trace!(
                "Generating default for Tuple with {} types",
                inner_types.len()
            );
            let tuple_defaults: Vec<String> = inner_types
                .iter()
                .map(|ty| field_type_to_default_value(ty, structs, enums, registry))
                .collect();
            format!("[{}]", tuple_defaults.join(", "))
        }
        FieldType::Struct(fields) => {
            trace!("Generating default for Struct with {} fields", fields.len());
            let fields_str = fields
                .iter()
                .map(|(name, ftype)| {
                    format!(
                        "{}: {}",
                        name.to_case(Case::Camel),
                        field_type_to_default_value(ftype, structs, enums, registry)
                    )
                })
                .collect::<Vec<_>>()
                .join(", ");
            format!("{{ {} }}", fields_str)
        }
        FieldType::Option(inner) => {
            // You can decide whether to produce `null` or `undefined` or something else.
            // For TypeScript, `null` is a more direct representation of "no value."
            trace!("Generating default for Option type with inner: {:?}", inner);
            "null".to_string()
        }
        FieldType::Vec(inner) => {
            // Returns an empty array as the default
            // but recursively if you wanted an "example entry" you could do:
            //   format!("[{}]", field_type_to_default_value(inner, structs, enums))
            trace!("Generating default for Vec type with inner: {:?}", inner);
            "[]".to_string()
        }
        FieldType::HashMap(key, value) => {
            // Return an empty object as default
            trace!(
                "Generating default for HashMap with key: {:?}, value: {:?}",
                key, value
            );
            "{}".to_string()
        }

        FieldType::BTreeMap(key, value) => {
            // Return an empty object as default
            trace!(
                "Generating default for BTreeMap with key: {:?}, value: {:?}",
                key, value
            );
            "{}".to_string()
        }

        FieldType::RecordLink(inner) => {
            // Could produce "null" or "0" depending on your usage pattern.
            // We'll pick "null" for "unlinked".
            trace!("Generating default for RecordLink with inner: {:?}", inner);
            "''".to_string()
        }
        FieldType::Other(name) => {
            // 0) Check if it's a configured foreign type
            if let Some(ftc) = registry.lookup(name) {
                return ftc.default_value_ts.clone();
            }

            // 1) If this is an enum, pick a random variant.
            // 2) Otherwise if it matches a known table, produce a default object for that table.
            // 3) If neither, fall back to 'undefined'.
            debug!("Generating default for Other type: {}", name);

            // First check for an enum of this name
            if let Some(enum_schema) = enums.values().find(|e| e.enum_name == *name) {
                debug!(
                    "Found enum {} with {} variants",
                    name,
                    enum_schema.variants.len()
                );
                let mut rng = rng();
                if let Some(chosen_variant) = enum_schema.variants.choose(&mut rng) {
                    trace!("Chosen variant: {}", chosen_variant.name);
                    // If the variant has data, generate a default for it.
                    if let Some(variant_data) = &chosen_variant.data {
                        let inner_default = match variant_data {
                            VariantData::InlineStruct(enum_struct) => field_type_to_default_value(
                                &FieldType::Other(enum_struct.struct_name.clone()),
                                structs,
                                enums,
                                registry,
                            ),
                            VariantData::DataStructureRef(field_type) => {
                                field_type_to_default_value(field_type, structs, enums, registry)
                            }
                        };
                        return match &enum_schema.representation {
                            EnumRepresentation::ExternallyTagged => {
                                format!("{{ {}: {} }}", chosen_variant.name, inner_default)
                            }
                            EnumRepresentation::InternallyTagged { tag } => {
                                if let VariantData::InlineStruct(_) = variant_data {
                                    // Merge tag into the struct — strip outer braces and prepend tag
                                    let trimmed = inner_default.trim();
                                    if trimmed.starts_with('{') && trimmed.ends_with('}') {
                                        let inner = &trimmed[1..trimmed.len() - 1];
                                        format!(
                                            "{{ {}: '\"{}\"', {} }}",
                                            tag,
                                            chosen_variant.name,
                                            inner.trim()
                                        )
                                    } else {
                                        format!("{{ {}: '\"{}\"' }}", tag, chosen_variant.name)
                                    }
                                } else {
                                    // DataStructureRef — serde doesn't support this, fall back to external
                                    format!("{{ {}: {} }}", chosen_variant.name, inner_default)
                                }
                            }
                            EnumRepresentation::AdjacentlyTagged { tag, content } => {
                                format!(
                                    "{{ {}: '\"{}\"', {}: {} }}",
                                    tag, chosen_variant.name, content, inner_default
                                )
                            }
                            EnumRepresentation::Untagged => inner_default,
                        };
                    } else {
                        // A unit variant without data
                        return match &enum_schema.representation {
                            EnumRepresentation::InternallyTagged { tag }
                            | EnumRepresentation::AdjacentlyTagged { tag, .. } => {
                                format!("{{ {}: '\"{}\"' }}", tag, chosen_variant.name)
                            }
                            _ => format!("'\"{}\"", chosen_variant.name),
                        };
                    }
                } else {
                    // If no variants, fallback to undefined
                    return "undefined".to_string();
                }
            }

            if let Some(struct_config) = structs.values().find(|struct_config| {
                struct_config.struct_name.to_case(Case::Pascal) == name.to_case(Case::Pascal)
            }) {
                debug!(
                    "Found struct {} with {} fields",
                    name,
                    struct_config.fields.len()
                );
                // We treat this similarly to a struct:
                let fields_str = struct_config
                    .fields
                    .iter()
                    .map(|table_field| {
                        format!(
                            "{}: {}",
                            table_field.field_name.to_case(Case::Camel),
                            field_type_to_default_value(
                                &table_field.field_type,
                                structs,
                                enums,
                                registry
                            )
                        )
                    })
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("{{ {} }}", fields_str)
            } else {
                // Not an enum or known table
                trace!(
                    "Type {} not found in enums or structs, returning undefined",
                    name
                );
                "undefined".to_string()
            }
        }
    };
    trace!("Generated default value: {}", result);
    result
}

#[cfg(feature = "surrealdb")]
/// Generate default values for SurrealDB queries (CREATE/UPDATE statements)
pub fn field_type_to_surql_default(
    field_name: &String,
    table_name: &String,
    field_type: &FieldType,
    enums: &HashMap<String, TaggedUnion>,
    app_structs: &HashMap<String, StructConfig>,
    persistable_structs: &HashMap<String, TableConfig>,
    registry: &crate::types::ForeignTypeRegistry,
) -> String {
    trace!(
        "Generating SURQL default for field '{}' in table '{}', type: {:?}",
        field_name, table_name, field_type
    );
    let result = match field_type {
        FieldType::String | FieldType::Char => {
            trace!("Generating SURQL default for String/Char");
            "\'\'".to_string()
        }
        FieldType::Bool => {
            trace!("Generating SURQL default for Bool");
            "false".to_string()
        }
        FieldType::Unit => {
            trace!("Generating SURQL default for Unit");
            "NULL".to_string()
        }
        FieldType::F32 | FieldType::F64 => {
            trace!("Generating SURQL default for float type");
            "0.0f".to_string()
        }
        FieldType::I8
        | FieldType::I16
        | FieldType::I32
        | FieldType::I64
        | FieldType::I128
        | FieldType::Isize
        | FieldType::U8
        | FieldType::U16
        | FieldType::U32
        | FieldType::U64
        | FieldType::U128
        | FieldType::Usize => {
            trace!("Generating SURQL default for integer type");
            "0".to_string()
        }
        FieldType::Tuple(inner_types) => {
            trace!(
                "Generating SURQL default for Tuple with {} types",
                inner_types.len()
            );
            let tuple_defaults: Vec<String> = inner_types
                .iter()
                .map(|ty| {
                    field_type_to_surql_default(
                        field_name,
                        table_name,
                        ty,
                        enums,
                        app_structs,
                        persistable_structs,
                        registry,
                    )
                })
                .collect();
            format!("[{}]", tuple_defaults.join(", "))
        }
        FieldType::Struct(fields) => {
            trace!(
                "Generating SURQL default for Struct with {} fields",
                fields.len()
            );
            let fields_str = fields
                .iter()
                .map(|(name, ftype)| {
                    format!(
                        "{}: {}",
                        name.to_case(Case::Snake), // SurrealDB typically uses snake_case
                        field_type_to_surql_default(
                            field_name,
                            table_name,
                            ftype,
                            enums,
                            app_structs,
                            persistable_structs,
                            registry
                        )
                    )
                })
                .collect::<Vec<_>>()
                .join(", ");
            format!("{{ {} }}", fields_str)
        }
        FieldType::Option(inner) => {
            trace!(
                "Generating SURQL default for Option with inner: {:?}",
                inner
            );
            "NULL".to_string()
        }
        FieldType::Vec(inner) => {
            trace!("Generating SURQL default for Vec with inner: {:?}", inner);
            "[]".to_string()
        }
        FieldType::HashMap(key, value) | FieldType::BTreeMap(key, value) => {
            trace!(
                "Generating SURQL default for Map with key: {:?}, value: {:?}",
                key, value
            );
            "{}".to_string()
        }
        FieldType::RecordLink(inner) => {
            trace!(
                "Generating SURQL default for RecordLink with inner: {:?}",
                inner
            );
            "NULL".to_string()
        }
        FieldType::Other(name) => {
            debug!("Processing Other type '{}' for SURQL default", name);

            // Check if it's a configured foreign type
            if let Some(ftc) = registry.lookup(name) {
                return ftc.default_value_surql.clone();
            }

            // Check if it's an enum
            if let Some(enum_schema) = enums.values().find(|e| e.enum_name == *name) {
                trace!(
                    "Found enum '{}' with {} variants",
                    name,
                    enum_schema.variants.len()
                );
                let chosen_variant = &enum_schema.variants[0];
                if let Some(variant_data) = &chosen_variant.data {
                    let inner_default = match variant_data {
                        VariantData::InlineStruct(enum_struct) => field_type_to_surql_default(
                            field_name,
                            table_name,
                            &FieldType::Other(enum_struct.struct_name.clone()),
                            enums,
                            app_structs,
                            persistable_structs,
                            registry,
                        ),
                        VariantData::DataStructureRef(field_type) => field_type_to_surql_default(
                            field_name,
                            table_name,
                            field_type,
                            enums,
                            app_structs,
                            persistable_structs,
                            registry,
                        ),
                    };
                    match &enum_schema.representation {
                        EnumRepresentation::ExternallyTagged => {
                            format!("{{ {}: {} }}", chosen_variant.name, inner_default)
                        }
                        EnumRepresentation::InternallyTagged { tag } => {
                            if let VariantData::InlineStruct(_) = variant_data {
                                let trimmed = inner_default.trim();
                                if trimmed.starts_with('{') && trimmed.ends_with('}') {
                                    let inner = &trimmed[1..trimmed.len() - 1];
                                    format!(
                                        "{{ {}: '{}', {} }}",
                                        tag,
                                        chosen_variant.name,
                                        inner.trim()
                                    )
                                } else {
                                    format!("{{ {}: '{}' }}", tag, chosen_variant.name)
                                }
                            } else {
                                format!("{{ {}: {} }}", chosen_variant.name, inner_default)
                            }
                        }
                        EnumRepresentation::AdjacentlyTagged { tag, content } => {
                            format!(
                                "{{ {}: '{}', {}: {} }}",
                                tag, chosen_variant.name, content, inner_default
                            )
                        }
                        EnumRepresentation::Untagged => inner_default,
                    }
                } else {
                    // For simple enum variant
                    match &enum_schema.representation {
                        EnumRepresentation::InternallyTagged { tag }
                        | EnumRepresentation::AdjacentlyTagged { tag, .. } => {
                            format!("{{ {}: '{}' }}", tag, chosen_variant.name)
                        }
                        _ => format!("'{}'", chosen_variant.name),
                    }
                }
            }
            // Check if it's a struct
            else if let Some(struct_config) = app_structs.values().find(|struct_config| {
                struct_config.struct_name.to_case(Case::Pascal) == name.to_case(Case::Pascal)
            }) {
                debug!(
                    "Found app struct '{}' with {} fields",
                    name,
                    struct_config.fields.len()
                );
                let fields_str = struct_config
                    .fields
                    .iter()
                    .map(|table_field| {
                        format!(
                            "{}: {}",
                            table_field.field_name.to_case(Case::Snake),
                            field_type_to_surql_default(
                                &table_field.field_name,
                                table_name,
                                &table_field.field_type,
                                enums,
                                app_structs,
                                persistable_structs,
                                registry
                            )
                        )
                    })
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("{{ {} }}", fields_str)
            }
            // Check if it's a persistable struct (table reference)
            else if persistable_structs.get(name).is_some() {
                // For record links to other tables, default to NULL
                debug!("Found persistable struct '{}', defaulting to NULL", name);
                "NULL".to_string()
            } else {
                trace!("Type '{}' not found, defaulting to NULL", name);
                "NULL".to_string()
            }
        }
    };
    trace!("Generated SURQL default: {}", result);
    result
}

#[cfg(feature = "surrealdb")]
pub fn field_type_to_surreal_type(
    field_name: &String,
    table_name: &String,
    field_type: &FieldType,
    enums: &HashMap<String, TaggedUnion>,
    app_structs: &HashMap<String, StructConfig>,
    persistable_structs: &HashMap<String, TableConfig>,
    registry: &crate::types::ForeignTypeRegistry,
) -> (String, bool, Option<String>) {
    trace!(
        "Converting field '{}' in table '{}' to SurrealDB type, field_type: {:?}",
        field_name, table_name, field_type
    );
    let result = match field_type {
        FieldType::String | FieldType::Char => {
            trace!("Converting String/Char to SurrealDB type");
            ("string".to_string(), false, None)
        }
        FieldType::Bool => {
            trace!("Converting Bool to SurrealDB type");
            ("bool".to_string(), false, None)
        }
        FieldType::F32 | FieldType::F64 => {
            trace!("Converting float to SurrealDB type");
            ("float".to_string(), false, None)
        }
        FieldType::I8
        | FieldType::I16
        | FieldType::I32
        | FieldType::I64
        | FieldType::I128
        | FieldType::Isize
        | FieldType::U8
        | FieldType::U16
        | FieldType::U32
        | FieldType::U64
        | FieldType::U128
        | FieldType::Usize => {
            trace!("Converting integer to SurrealDB type");
            ("int".to_string(), false, None)
        }
        FieldType::Unit => {
            trace!("Converting Unit to SurrealDB type");
            ("any".to_string(), false, None)
        }
        FieldType::HashMap(_key, value) => {
            trace!("Converting HashMap to SurrealDB type");
            let (value_type, _, _) = field_type_to_surreal_type(
                field_name,
                table_name,
                value,
                enums,
                app_structs,
                persistable_structs,
                registry,
            );
            ("object".to_string(), true, Some(value_type))
        }
        FieldType::BTreeMap(_key, value) => {
            trace!("Converting BTreeMap to SurrealDB type");
            let (value_type, _, _) = field_type_to_surreal_type(
                field_name,
                table_name,
                value,
                enums,
                app_structs,
                persistable_structs,
                registry,
            );
            ("object".to_string(), true, Some(value_type))
        }
        FieldType::RecordLink(inner) => {
            trace!(
                "Converting RecordLink to SurrealDB type with inner: {:?}",
                inner
            );
            let (inner_type, needs_wildcard, wildcard_type) = field_type_to_surreal_type(
                field_name,
                table_name,
                inner,
                enums,
                app_structs,
                persistable_structs,
                registry,
            );
            (inner_type, needs_wildcard, wildcard_type)
        }
        FieldType::Other(name) => {
            debug!(
                "Processing Other type '{}' for SurrealDB type conversion",
                name
            );

            // Check if it's a configured foreign type
            if let Some(ftc) = registry.lookup(name) {
                let type_str = if field_name == "id" {
                    if let Some(ref id_fmt) = ftc.surrealdb_id_format {
                        id_fmt.replace("{table_name}", table_name)
                    } else {
                        ftc.surrealdb.clone()
                    }
                } else if let Some(ref non_id_fmt) = ftc.surrealdb_non_id_format {
                    non_id_fmt.clone()
                } else {
                    ftc.surrealdb.clone()
                };
                return (type_str, false, None);
            }

            // If this type name is defined as an enum, output its union literal.
            if let Some(enum_def) = enums.get(name) {
                debug!(
                    "Found enum '{}' with {} variants",
                    name,
                    enum_def.variants.len()
                );
                let variants: Vec<String> = enum_def
                    .variants
                    .iter()
                    .map(|v| {
                        if let Some(variant_data) = &v.data {
                            let inner_type = match variant_data {
                                VariantData::InlineStruct(enum_struct) => {
                                    let (t, _, _) = field_type_to_surreal_type(
                                        field_name,
                                        table_name,
                                        &FieldType::Other(enum_struct.struct_name.clone()),
                                        enums,
                                        app_structs,
                                        persistable_structs,
                                        registry,
                                    );
                                    t
                                }
                                VariantData::DataStructureRef(field_type) => {
                                    let (t, _, _) = field_type_to_surreal_type(
                                        field_name,
                                        table_name,
                                        field_type,
                                        enums,
                                        app_structs,
                                        persistable_structs,
                                        registry,
                                    );
                                    t
                                }
                            };
                            match &enum_def.representation {
                                EnumRepresentation::ExternallyTagged => {
                                    format!("{{ {}: {} }}", v.name, inner_type)
                                }
                                EnumRepresentation::InternallyTagged { tag } => {
                                    if let VariantData::InlineStruct(_) = variant_data {
                                        let trimmed = inner_type.trim();
                                        if trimmed.starts_with('{') && trimmed.ends_with('}') {
                                            let inner = &trimmed[1..trimmed.len() - 1];
                                            format!(
                                                "{{ {}: \"{}\", {} }}",
                                                tag,
                                                v.name,
                                                inner.trim()
                                            )
                                        } else {
                                            format!("{{ {}: \"{}\" }}", tag, v.name)
                                        }
                                    } else {
                                        format!("{{ {}: {} }}", v.name, inner_type)
                                    }
                                }
                                EnumRepresentation::AdjacentlyTagged { tag, content } => {
                                    format!(
                                        "{{ {}: \"{}\", {}: {} }}",
                                        tag, v.name, content, inner_type
                                    )
                                }
                                EnumRepresentation::Untagged => inner_type,
                            }
                        } else {
                            match &enum_def.representation {
                                EnumRepresentation::InternallyTagged { tag }
                                | EnumRepresentation::AdjacentlyTagged { tag, .. } => {
                                    format!("{{ {}: \"{}\" }}", tag, v.name)
                                }
                                _ => format!("\"{}\"", v.name),
                            }
                        }
                    })
                    .collect();
                (variants.join(" | "), false, None)
            } else if let Some(app_struct) = app_structs.get(name) {
                debug!(
                    "Found app struct '{}' with {} fields for type conversion",
                    name,
                    app_struct.fields.len()
                );
                let field_defs: Vec<String> = app_struct
                    .fields
                    .iter()
                    .map(|f: &StructField| {
                        let (field_type, _, _) = field_type_to_surreal_type(
                            &f.field_name,
                            table_name,
                            &f.field_type,
                            enums,
                            app_structs,
                            persistable_structs,
                            registry,
                        );
                        format!("{}: {}", f.field_name, field_type)
                    })
                    .collect();

                (format!("{{ {} }}", field_defs.join(", ")), false, None)
            } else if persistable_structs.get(name).is_some() {
                debug!("Creating record type for persistable struct '{}'", name);
                (
                    format!("record<{}>", name.to_case(Case::Snake)),
                    false,
                    None,
                )
            } else {
                trace!("Type '{}' not found in any category, using as-is", name);
                (name.clone(), false, None)
            }
        }
        FieldType::Option(inner) => {
            trace!(
                "Converting Option to SurrealDB type with inner: {:?}",
                inner
            );
            let (inner_type, needs_wildcard, wildcard_type) = field_type_to_surreal_type(
                field_name,
                table_name,
                inner,
                enums,
                app_structs,
                persistable_structs,
                registry,
            );
            (
                format!("null | {}", inner_type),
                needs_wildcard,
                wildcard_type,
            )
        }
        FieldType::Vec(inner) => {
            trace!("Converting Vec to SurrealDB type with inner: {:?}", inner);
            let (inner_type, _, _) = field_type_to_surreal_type(
                field_name,
                table_name,
                inner,
                enums,
                app_structs,
                persistable_structs,
                registry,
            );
            (format!("array<{}>", inner_type), false, None)
        }
        FieldType::Tuple(inner_types) => {
            trace!(
                "Converting Tuple to SurrealDB type with {} types",
                inner_types.len()
            );
            let inner: Vec<String> = inner_types
                .iter()
                .map(|t| {
                    let (inner_type, _, _) = field_type_to_surreal_type(
                        field_name,
                        table_name,
                        t,
                        enums,
                        app_structs,
                        persistable_structs,
                        registry,
                    );
                    inner_type
                })
                .collect();
            // (SurrealDB does not have a dedicated tuple type so we wrap it as an array)
            (format!("array<{}>", inner.join(", ")), false, None)
        }
        FieldType::Struct(fields) => {
            trace!(
                "Converting Struct to SurrealDB type with {} fields",
                fields.len()
            );
            let field_defs: Vec<String> = fields
                .iter()
                .map(|(name, t)| {
                    let (field_type, _, _) = field_type_to_surreal_type(
                        field_name,
                        table_name,
                        t,
                        enums,
                        app_structs,
                        persistable_structs,
                        registry,
                    );
                    format!("{}: {}", name, field_type)
                })
                .collect();
            (format!("{{ {} }}", field_defs.join(", ")), false, None)
        }
    };
    trace!("Generated SurrealDB type: {:?}", result);
    result
}