prax-import 0.6.4

Import schemas from Prisma, Diesel, and SeaORM to Prax ORM
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
//! Prisma schema parser and converter.

use crate::converter::{FieldBuilder, ModelBuilder, SchemaBuilder, dummy_span};
use crate::error::ImportResult;
use crate::prisma::types::*;
use once_cell::sync::Lazy;
use prax_schema::ast::*;
use regex_lite::Regex;
use smol_str::SmolStr;
use std::fs;
use std::path::Path;

// Pre-compiled regex patterns for better performance
static DATASOURCE_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(
        r#"(?s)datasource\s+\w+\s*\{[^}]*provider\s*=\s*"([^"]+)"[^}]*url\s*=\s*[^"}]*"([^"]+)""#,
    )
    .unwrap()
});

static MODEL_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?s)model\s+(\w+)\s*\{([^}]+)\}").unwrap());

static FIELD_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(\w+)\s+([\w\[\]?]+)(\s+@[\w\(\)]+)*").unwrap());

static DEFAULT_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"@default\(([^)]+)\)"#).unwrap());

static MAP_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"@map\("([^"]+)"\)"#).unwrap());

static RELATION_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"@relation\(([^)]+)\)"#).unwrap());

static RELATION_NAME_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"name:\s*"([^"]+)""#).unwrap());

static RELATION_FIELDS_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"fields:\s*\[([^\]]+)\]").unwrap());

static RELATION_REFS_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"references:\s*\[([^\]]+)\]").unwrap());

static MODEL_ID_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"@@id\(\[([^\]]+)\]\)").unwrap());

static MODEL_UNIQUE_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"@@unique\(\[([^\]]+)\]").unwrap());

static MODEL_INDEX_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"@@index\(\[([^\]]+)\]").unwrap());

static MODEL_MAP_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"@@map\("([^"]+)"\)"#).unwrap());

static ATTR_NAME_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"name:\s*"([^"]+)""#).unwrap());

static ENUM_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?s)enum\s+(\w+)\s*\{([^}]+)\}").unwrap());

/// Parse a Prisma schema from a string.
///
/// Returns the intermediate `PrismaSchema` representation.
pub fn parse_prisma_schema(input: &str) -> ImportResult<PrismaSchema> {
    let mut schema = PrismaSchema {
        datasource: None,
        models: vec![],
        enums: vec![],
    };

    // Parse datasource
    if let Some(datasource) = parse_datasource(input)? {
        schema.datasource = Some(datasource);
    }

    // Parse models
    for model in parse_models(input)? {
        schema.models.push(model);
    }

    // Parse enums
    for enum_def in parse_enums(input)? {
        schema.enums.push(enum_def);
    }

    Ok(schema)
}

/// Parse a Prisma schema from a file.
pub fn parse_prisma_file<P: AsRef<Path>>(path: P) -> ImportResult<PrismaSchema> {
    let content = fs::read_to_string(path)?;
    parse_prisma_schema(&content)
}

/// Convert Prisma schema to Prax schema.
pub fn import_prisma_schema(input: &str) -> ImportResult<Schema> {
    let prisma_schema = parse_prisma_schema(input)?;
    convert_prisma_to_prax(prisma_schema)
}

/// Convert a Prisma schema file to Prax schema.
pub fn import_prisma_schema_file<P: AsRef<Path>>(path: P) -> ImportResult<Schema> {
    let prisma_schema = parse_prisma_file(path)?;
    convert_prisma_to_prax(prisma_schema)
}

/// Parse datasource block.
fn parse_datasource(input: &str) -> ImportResult<Option<PrismaDatasource>> {
    if let Some(caps) = DATASOURCE_RE.captures(input) {
        let provider = caps.get(1).unwrap().as_str().to_string();
        let url = caps.get(2).unwrap().as_str().to_string();

        Ok(Some(PrismaDatasource { provider, url }))
    } else {
        Ok(None)
    }
}

/// Parse all models from the schema.
fn parse_models(input: &str) -> ImportResult<Vec<PrismaModel>> {
    let mut models = vec![];

    for caps in MODEL_RE.captures_iter(input) {
        let name = caps.get(1).unwrap().as_str().to_string();
        let body = caps.get(2).unwrap().as_str();

        let fields = parse_fields(body)?;
        let attributes = parse_model_attributes(body)?;

        models.push(PrismaModel {
            name,
            fields,
            attributes,
            documentation: None,
        });
    }

    Ok(models)
}

/// Parse fields from model body.
fn parse_fields(body: &str) -> ImportResult<Vec<PrismaField>> {
    let mut fields = vec![];

    for line in body.lines() {
        let line = line.trim();
        if line.starts_with("@@") || line.is_empty() || line.starts_with("//") {
            continue;
        }

        if let Some(caps) = FIELD_RE.captures(line) {
            let name = caps.get(1).unwrap().as_str().to_string();
            let type_str = caps.get(2).unwrap().as_str();

            let (field_type, is_optional, is_list) = parse_field_type(type_str)?;
            let attributes = parse_field_attributes(line)?;

            fields.push(PrismaField {
                name,
                field_type,
                is_optional,
                is_list,
                attributes,
                documentation: None,
            });
        }
    }

    Ok(fields)
}

/// Parse field type and modifiers.
fn parse_field_type(type_str: &str) -> ImportResult<(PrismaFieldType, bool, bool)> {
    let is_optional = type_str.contains('?');
    let is_list = type_str.contains("[]");
    let base_type = type_str.replace('?', "").replace("[]", "");

    let field_type = match base_type.as_str() {
        "String" => PrismaFieldType::String,
        "Boolean" => PrismaFieldType::Boolean,
        "Int" => PrismaFieldType::Int,
        "BigInt" => PrismaFieldType::BigInt,
        "Float" => PrismaFieldType::Float,
        "Decimal" => PrismaFieldType::Decimal,
        "DateTime" => PrismaFieldType::DateTime,
        "Json" => PrismaFieldType::Json,
        "Bytes" => PrismaFieldType::Bytes,
        custom => PrismaFieldType::Custom(custom.to_string()),
    };

    Ok((field_type, is_optional, is_list))
}

/// Parse field attributes.
fn parse_field_attributes(line: &str) -> ImportResult<Vec<PrismaFieldAttribute>> {
    let mut attributes = vec![];

    if line.contains("@id") {
        attributes.push(PrismaFieldAttribute::Id);
    }

    if line.contains("@unique") {
        attributes.push(PrismaFieldAttribute::Unique);
    }

    if line.contains("@updatedAt") {
        attributes.push(PrismaFieldAttribute::UpdatedAt);
    }

    // Parse @default
    if let Some(caps) = DEFAULT_RE.captures(line) {
        let default_val = caps.get(1).unwrap().as_str();
        let default = if default_val.contains('(') {
            PrismaDefaultValue::Function(default_val.trim_end_matches("()").to_string())
        } else {
            PrismaDefaultValue::Literal(default_val.to_string())
        };
        attributes.push(PrismaFieldAttribute::Default(default));
    }

    // Parse @map
    if let Some(caps) = MAP_RE.captures(line) {
        let map_val = caps.get(1).unwrap().as_str().to_string();
        attributes.push(PrismaFieldAttribute::Map(map_val));
    }

    // Parse @relation
    if line.contains("@relation") {
        let relation = parse_relation_attribute(line)?;
        attributes.push(relation);
    }

    Ok(attributes)
}

/// Parse @relation attribute.
fn parse_relation_attribute(line: &str) -> ImportResult<PrismaFieldAttribute> {
    if let Some(caps) = RELATION_RE.captures(line) {
        let args = caps.get(1).unwrap().as_str();

        let name = extract_relation_name(args);
        let fields = extract_relation_fields(args);
        let references = extract_relation_references(args);
        let on_delete = extract_relation_action(args, "onDelete");
        let on_update = extract_relation_action(args, "onUpdate");
        let map = extract_relation_map(args);

        Ok(PrismaFieldAttribute::Relation {
            name,
            fields,
            references,
            on_delete,
            on_update,
            map,
        })
    } else {
        Ok(PrismaFieldAttribute::Relation {
            name: None,
            fields: None,
            references: None,
            on_delete: None,
            on_update: None,
            map: None,
        })
    }
}

fn extract_relation_name(args: &str) -> Option<String> {
    RELATION_NAME_RE
        .captures(args)
        .and_then(|caps| caps.get(1).map(|m| m.as_str().to_string()))
}

fn extract_relation_fields(args: &str) -> Option<Vec<String>> {
    RELATION_FIELDS_RE.captures(args).map(|caps| {
        caps.get(1)
            .unwrap()
            .as_str()
            .split(',')
            .map(|s| s.trim().to_string())
            .collect()
    })
}

fn extract_relation_references(args: &str) -> Option<Vec<String>> {
    RELATION_REFS_RE.captures(args).map(|caps| {
        caps.get(1)
            .unwrap()
            .as_str()
            .split(',')
            .map(|s| s.trim().to_string())
            .collect()
    })
}

fn extract_relation_action(args: &str, action: &str) -> Option<String> {
    let pattern = format!(r"{}:\s*(\w+)", action);
    let re = Regex::new(&pattern).unwrap();
    re.captures(args)
        .and_then(|caps| caps.get(1).map(|m| m.as_str().to_string()))
}

fn extract_relation_map(args: &str) -> Option<String> {
    let re = Regex::new(r#"map:\s*"([^"]+)""#).unwrap();
    re.captures(args)
        .and_then(|caps| caps.get(1).map(|m| m.as_str().to_string()))
}

/// Parse model-level attributes.
fn parse_model_attributes(body: &str) -> ImportResult<Vec<PrismaModelAttribute>> {
    let mut attributes = vec![];

    for line in body.lines() {
        let line = line.trim();

        // Parse @@id
        if line.starts_with("@@id") {
            if let Some(caps) = MODEL_ID_RE.captures(line) {
                let fields = caps
                    .get(1)
                    .unwrap()
                    .as_str()
                    .split(',')
                    .map(|s| s.trim().to_string())
                    .collect();
                attributes.push(PrismaModelAttribute::Id(fields));
            }
        }

        // Parse @@unique
        if line.starts_with("@@unique") {
            if let Some(caps) = MODEL_UNIQUE_RE.captures(line) {
                let fields = caps
                    .get(1)
                    .unwrap()
                    .as_str()
                    .split(',')
                    .map(|s| s.trim().to_string())
                    .collect();

                let name = ATTR_NAME_RE
                    .captures(line)
                    .and_then(|caps| caps.get(1).map(|m| m.as_str().to_string()));

                attributes.push(PrismaModelAttribute::Unique { fields, name });
            }
        }

        // Parse @@index
        if line.starts_with("@@index") {
            if let Some(caps) = MODEL_INDEX_RE.captures(line) {
                let fields = caps
                    .get(1)
                    .unwrap()
                    .as_str()
                    .split(',')
                    .map(|s| s.trim().to_string())
                    .collect();

                let name = ATTR_NAME_RE
                    .captures(line)
                    .and_then(|caps| caps.get(1).map(|m| m.as_str().to_string()));

                attributes.push(PrismaModelAttribute::Index { fields, name });
            }
        }

        // Parse @@map
        if line.starts_with("@@map") {
            if let Some(caps) = MODEL_MAP_RE.captures(line) {
                let map_val = caps.get(1).unwrap().as_str().to_string();
                attributes.push(PrismaModelAttribute::Map(map_val));
            }
        }
    }

    Ok(attributes)
}

/// Parse enums from the schema.
fn parse_enums(input: &str) -> ImportResult<Vec<PrismaEnum>> {
    let mut enums = vec![];

    for caps in ENUM_RE.captures_iter(input) {
        let name = caps.get(1).unwrap().as_str().to_string();
        let body = caps.get(2).unwrap().as_str();

        let values = body
            .lines()
            .map(|l| l.trim())
            .filter(|l| !l.is_empty() && !l.starts_with("//"))
            .map(|l| l.to_string())
            .collect();

        enums.push(PrismaEnum {
            name,
            values,
            documentation: None,
        });
    }

    Ok(enums)
}

/// Convert Prisma schema to Prax schema.
fn convert_prisma_to_prax(prisma_schema: PrismaSchema) -> ImportResult<Schema> {
    let mut builder = SchemaBuilder::new();

    // Convert datasource
    if let Some(datasource) = prisma_schema.datasource {
        builder = builder.with_datasource(datasource.provider, datasource.url);
    }

    // Convert models
    for model in prisma_schema.models {
        let prax_model = convert_model(model)?;
        builder.add_model(prax_model);
    }

    // Convert enums
    for enum_def in prisma_schema.enums {
        let prax_enum = convert_enum(enum_def);
        builder.add_enum(prax_enum);
    }

    Ok(builder.build())
}

/// Convert a Prisma model to a Prax model.
fn convert_model(model: PrismaModel) -> ImportResult<Model> {
    let mut model_builder = ModelBuilder::new(&model.name);

    // Check for @@map attribute
    for attr in &model.attributes {
        if let PrismaModelAttribute::Map(table_name) = attr {
            model_builder = model_builder.with_db_name(table_name);
        }
    }

    // Convert fields
    for field in model.fields {
        let prax_field = convert_field(field)?;
        model_builder.add_field(prax_field);
    }

    // Convert model attributes
    for attr in model.attributes {
        match attr {
            PrismaModelAttribute::Unique { fields, name } => {
                model_builder.add_unique(fields, name);
            }
            PrismaModelAttribute::Index { fields, name } => {
                model_builder.add_index(fields, name);
            }
            PrismaModelAttribute::Map(_) => {
                // Already handled above
            }
            PrismaModelAttribute::Id(fields) => {
                // Composite primary key - add as @@id attribute
                model_builder.add_unique(fields, Some("PRIMARY".to_string()));
            }
        }
    }

    Ok(model_builder.build())
}

/// Convert a Prisma field to a Prax field.
fn convert_field(field: PrismaField) -> ImportResult<Field> {
    let (prax_type, modifier) =
        convert_field_type(&field.field_type, field.is_optional, field.is_list)?;
    let mut field_builder = FieldBuilder::new(&field.name, prax_type, modifier);

    // Convert field attributes
    for attr in field.attributes {
        match attr {
            PrismaFieldAttribute::Id => {
                field_builder = field_builder.with_id();
            }
            PrismaFieldAttribute::Unique => {
                field_builder = field_builder.with_unique();
            }
            PrismaFieldAttribute::Default(default_val) => {
                let prax_default = convert_default_value(default_val);
                field_builder = field_builder.with_default(prax_default);
            }
            PrismaFieldAttribute::Map(col_name) => {
                field_builder = field_builder.with_map(col_name);
            }
            PrismaFieldAttribute::UpdatedAt => {
                // Convert to @default(now())
                field_builder = field_builder
                    .with_default(AttributeValue::Function(SmolStr::from("now"), vec![]));
            }
            PrismaFieldAttribute::Relation {
                fields,
                references,
                on_delete,
                map,
                ..
            } => {
                if let (Some(fields), Some(references)) = (fields, references) {
                    field_builder = field_builder.with_relation(fields, references, on_delete, map);
                }
            }
        }
    }

    Ok(field_builder.build())
}

/// Convert Prisma field type to Prax field type and modifier.
fn convert_field_type(
    field_type: &PrismaFieldType,
    is_optional: bool,
    is_list: bool,
) -> ImportResult<(FieldType, TypeModifier)> {
    let base_type = match field_type {
        PrismaFieldType::String => FieldType::Scalar(ScalarType::String),
        PrismaFieldType::Boolean => FieldType::Scalar(ScalarType::Boolean),
        PrismaFieldType::Int => FieldType::Scalar(ScalarType::Int),
        PrismaFieldType::BigInt => FieldType::Scalar(ScalarType::BigInt),
        PrismaFieldType::Float => FieldType::Scalar(ScalarType::Float),
        PrismaFieldType::Decimal => FieldType::Scalar(ScalarType::Decimal),
        PrismaFieldType::DateTime => FieldType::Scalar(ScalarType::DateTime),
        PrismaFieldType::Json => FieldType::Scalar(ScalarType::Json),
        PrismaFieldType::Bytes => FieldType::Scalar(ScalarType::Bytes),
        PrismaFieldType::Custom(name) => {
            // Could be an enum or a relation
            FieldType::Model(SmolStr::from(name.as_str()))
        }
    };

    let modifier = match (is_optional, is_list) {
        (true, true) => TypeModifier::OptionalList,
        (false, true) => TypeModifier::List,
        (true, false) => TypeModifier::Optional,
        (false, false) => TypeModifier::Required,
    };

    Ok((base_type, modifier))
}

/// Convert Prisma default value to Prax attribute value.
fn convert_default_value(default: PrismaDefaultValue) -> AttributeValue {
    match default {
        PrismaDefaultValue::Literal(val) => {
            // Try to parse as different types
            if val == "true" {
                AttributeValue::Boolean(true)
            } else if val == "false" {
                AttributeValue::Boolean(false)
            } else if let Ok(n) = val.parse::<i64>() {
                AttributeValue::Int(n)
            } else if let Ok(f) = val.parse::<f64>() {
                AttributeValue::Float(f)
            } else {
                AttributeValue::String(val)
            }
        }
        PrismaDefaultValue::Function(func) => {
            let func_name = if func == "autoincrement" {
                "auto"
            } else {
                &func
            };
            AttributeValue::Function(SmolStr::from(func_name), vec![])
        }
    }
}

/// Convert a Prisma enum to a Prax enum.
fn convert_enum(enum_def: PrismaEnum) -> Enum {
    let mut prax_enum = Enum::new(Ident::new(&enum_def.name, dummy_span()), dummy_span());

    for variant_name in enum_def.values {
        let variant = EnumVariant {
            name: Ident::new(&variant_name, dummy_span()),
            attributes: vec![],
            documentation: None,
            span: dummy_span(),
        };
        prax_enum.variants.push(variant);
    }

    if let Some(doc) = enum_def.documentation {
        prax_enum.documentation = Some(Documentation::new(doc, dummy_span()));
    }

    prax_enum
}

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

    #[test]
    fn test_parse_simple_model() {
        let schema = r#"
        model User {
            id    Int    @id @default(autoincrement())
            email String @unique
            name  String?
        }
        "#;

        let result = parse_prisma_schema(schema);
        assert!(result.is_ok());

        let prisma_schema = result.unwrap();
        assert_eq!(prisma_schema.models.len(), 1);
        assert_eq!(prisma_schema.models[0].fields.len(), 3);
    }

    #[test]
    fn test_import_simple_model() {
        let schema = r#"
        model User {
            id    Int    @id @default(autoincrement())
            email String @unique
        }
        "#;

        let result = import_prisma_schema(schema);
        assert!(result.is_ok());

        let prax_schema = result.unwrap();
        assert_eq!(prax_schema.models.len(), 1);
    }
}