prax-codegen 0.11.0

Procedural macros for code generation in the 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
//! Lower DSL `order_by:` (and `cursor:`) values.
//!
//! `order_by` accepts either a single block (`{ created_at: desc }`)
//! or a list of blocks (`[{ a: asc }, { b: desc }]`). Both lower to a
//! `prax_query::types::OrderBy` built up via
//! `OrderBy::from_fields(...)`. Going directly to the runtime IR
//! sidesteps the phase-2 limitation that `OrderByInput::into_ir`
//! returns a single `OrderBy` (so multiple
//! `with_order_by_input` calls would clobber each other).
//!
//! ## Aggregate fields in `order_by:`
//!
//! When a key in `order_by:` is a schema-level aggregate field, the
//! generated `OrderByField` uses the scalar-subquery SQL as the column
//! expression. The `SqlBuilder` emits it verbatim in the `ORDER BY`
//! clause — e.g. `ORDER BY (SELECT COUNT(*) ...) DESC`. This is the
//! same approach used by Prisma's generated SQL for aggregate ordering.
//!
//! `cursor` lowers to the per-model `<Model>WhereUniqueInput` enum
//! variant: the block must have exactly one key, matched against a
//! `@unique` column.

#![allow(dead_code)]

use convert_case::{Case, Casing};
use prax_schema::{FieldType, Model};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use super::LowerCtx;
use super::scalar_filter::category_for_scalar;
use crate::macros::dsl::ast::{DslBlock, DslField, DslValue};

/// Lower `order_by:` value.
pub fn lower_order_by(value: &DslValue, ctx: &LowerCtx<'_>) -> syn::Result<TokenStream> {
    let blocks: Vec<&DslBlock> = match value {
        DslValue::Block(b) => vec![b],
        DslValue::List(items) => items
            .iter()
            .map(|v| match v {
                DslValue::Block(b) => Ok(b),
                _ => Err(syn::Error::new(
                    proc_macro2::Span::call_site(),
                    "`order_by:` list entries must be `{ field: dir }` blocks",
                )),
            })
            .collect::<syn::Result<_>>()?,
        _ => {
            return Err(syn::Error::new(
                proc_macro2::Span::call_site(),
                "`order_by:` expects a `{ ... }` block or a list of blocks",
            ));
        }
    };

    let mut fields: Vec<TokenStream> = Vec::new();
    for b in blocks {
        for entry in &b.fields {
            let DslField::Pair { key, value, .. } = entry else {
                return Err(syn::Error::new(
                    proc_macro2::Span::call_site(),
                    "`order_by:` does not support spread or conditional fields yet",
                ));
            };
            let key_str = key.to_string();
            let dir = match value {
                DslValue::BareIdent(id) => id.to_string(),
                DslValue::Path(p) => p
                    .segments
                    .last()
                    .map(|s| s.ident.to_string())
                    .ok_or_else(|| syn::Error::new(key.span(), "empty sort path"))?,
                _ => {
                    return Err(syn::Error::new(
                        key.span(),
                        "order_by direction must be `asc` or `desc`",
                    ));
                }
            };
            let dir_lower = dir.to_lowercase();
            if !matches!(dir_lower.as_str(), "asc" | "desc") {
                return Err(syn::Error::new(
                    key.span(),
                    format!("unknown sort direction `{dir}`; expected `asc` or `desc`"),
                ));
            }

            // Check if this is an aggregate field (order by scalar subquery).
            if let Some(model_field) = ctx.model.get_field(&key_str)
                && let Some(agg) = model_field.aggregate()
            {
                let subquery = build_aggregate_order_by_column(&key_str, &agg, key.span(), ctx)?;
                let sort_order = if dir_lower == "asc" {
                    quote! { ::prax_query::types::SortOrder::Asc }
                } else {
                    quote! { ::prax_query::types::SortOrder::Desc }
                };
                fields.push(quote! {
                    ::prax_query::types::OrderByField::new(
                        ::std::string::String::from(#subquery),
                        #sort_order,
                    )
                });
                continue;
            }

            let dir_ident = quote::format_ident!("{}", dir_lower);
            let column = lookup_column(ctx.model, &key_str).ok_or_else(|| {
                syn::Error::new(
                    key.span(),
                    format!(
                        "unknown order_by field `{}` on model `{}`",
                        key_str,
                        ctx.model.name()
                    ),
                )
            })?;
            fields.push(quote! {
                ::prax_query::types::OrderByField::#dir_ident(#column)
            });
        }
    }

    Ok(quote! {
        ::prax_query::types::OrderBy::from_fields(::std::vec![ #(#fields),* ])
    })
}

/// Lower `cursor:` value to a `<Model>WhereUniqueInput::Variant(value)`.
///
/// The phase-2 codegen emits `<Model>WhereUniqueInput` as an enum with
/// one variant per `@unique` column (PascalCase variant name, scalar
/// payload). Unique columns whose type codegen skips (enum-typed,
/// unmappable scalar category, non-scalar) are rejected here with a
/// diagnostic rather than emitting a variant that was never generated.
pub fn lower_cursor(block: &DslBlock, ctx: &LowerCtx<'_>) -> syn::Result<TokenStream> {
    if block.fields.len() != 1 {
        return Err(syn::Error::new(
            block.span,
            "cursor block must have exactly one unique-key field",
        ));
    }
    let DslField::Pair { key, value, .. } = &block.fields[0] else {
        return Err(syn::Error::new(
            block.span,
            "cursor block must be a `{ field: value }` pair",
        ));
    };
    let key_str = key.to_string();
    let field = ctx.model.get_field(&key_str).ok_or_else(|| {
        syn::Error::new(
            key.span(),
            format!("unknown cursor field `{key_str}` on `{}`", ctx.model.name()),
        )
    })?;
    if !field.is_id() && !field.is_unique() {
        return Err(syn::Error::new(
            key.span(),
            format!(
                "cursor field `{}` is not a unique column. \
                 Use a field marked `@id` or `@unique`.",
                key_str
            ),
        ));
    }
    // Mirror the codegen admission rule in
    // `generators::model::collect_unique_columns`: unique columns whose
    // type codegen skips (enum-typed, unmappable scalar category,
    // relation / composite / unsupported) get no `<Model>WhereUniqueInput`
    // variant. Admitting one here would emit a reference to a variant
    // that was never generated — a confusing E0599 in the user's crate.
    match &field.field_type {
        FieldType::Scalar(s) if category_for_scalar(s).is_some() => {}
        FieldType::Enum(_) => {
            return Err(syn::Error::new(
                key.span(),
                format!(
                    "cursor field `{key_str}` is not yet supported for cursor/unique-where \
                     (enum-typed unique columns pending enum-aware codegen)"
                ),
            ));
        }
        FieldType::Scalar(s) => {
            return Err(syn::Error::new(
                key.span(),
                format!(
                    "cursor field `{key_str}` is not yet supported for cursor/unique-where \
                     (scalar type `{s:?}` has no filter category)"
                ),
            ));
        }
        _ => {
            return Err(syn::Error::new(
                key.span(),
                format!(
                    "cursor field `{key_str}` is not yet supported for cursor/unique-where \
                     (only scalar unique columns are supported)"
                ),
            ));
        }
    }
    let variant = format_ident!("{}", key_str.to_case(Case::Pascal));
    let module_ident = format_ident!("{}", ctx.model.name().to_case(Case::Snake));
    let unique_ident = format_ident!("{}WhereUniqueInput", ctx.model.name());
    let payload = match value {
        DslValue::Lit(l) => quote! { ::core::convert::Into::into(#l) },
        DslValue::Expr(e) => quote! { ::core::convert::Into::into(#e) },
        DslValue::Path(p) => quote! { #p },
        DslValue::BareIdent(b) => quote! { #b },
        _ => {
            return Err(syn::Error::new(
                key.span(),
                "cursor value must be a literal or `@(expr)`",
            ));
        }
    };
    Ok(quote! {
        #module_ident::#unique_ident::#variant(#payload)
    })
}

fn lookup_column(model: &Model, field_name: &str) -> Option<String> {
    let f = model.get_field(field_name)?;
    // Aggregate fields don't map to DB columns — they use subquery SQL.
    if f.is_aggregate() {
        return None;
    }
    let attrs = f.extract_attributes();
    Some(attrs.map.unwrap_or_else(|| f.name().to_string()))
}

/// Build the scalar-subquery SQL string for an aggregate field used in
/// `order_by:`. The SQL is emitted verbatim as the `OrderByField::column`.
fn build_aggregate_order_by_column(
    field_name: &str,
    agg: &prax_schema::AggregateAttribute,
    span: proc_macro2::Span,
    ctx: &LowerCtx<'_>,
) -> syn::Result<String> {
    use crate::macros::lower::select_input::aggregate_sql;

    let rel_name = agg.relation.as_str();
    let rel_field = ctx.model.get_field(rel_name).ok_or_else(|| {
        syn::Error::new(
            span,
            format!(
                "aggregate field `{field_name}` references relation `{rel_name}` \
                 which does not exist on model `{}`",
                ctx.model.name()
            ),
        )
    })?;

    let prax_schema::FieldType::Model(target_model_name) = &rel_field.field_type else {
        return Err(syn::Error::new(
            span,
            format!("field `{rel_name}` is not a relation"),
        ));
    };

    let target_model = ctx.schema.get_model(target_model_name).ok_or_else(|| {
        syn::Error::new(
            span,
            format!("model `{target_model_name}` not found in schema"),
        )
    })?;

    let attrs = rel_field.extract_attributes();
    let rel_attr = attrs.relation.ok_or_else(|| {
        syn::Error::new(
            span,
            format!("relation `{rel_name}` has no `@relation(fields: [...], references: [...])`"),
        )
    })?;

    if rel_attr.fields.is_empty() || rel_attr.references.is_empty() {
        return Err(syn::Error::new(
            span,
            format!("relation `{rel_name}` must declare `fields` and `references`"),
        ));
    }

    let parent_table = ctx.model.table_name();
    let parent_pk = rel_attr.fields[0].as_str();
    let target_table = target_model.table_name();
    let fk_column = rel_attr.references[0].as_str();

    let kind = match agg.kind {
        prax_schema::AggregateKind::Count => "count",
        prax_schema::AggregateKind::Sum => "sum",
        prax_schema::AggregateKind::Avg => "avg",
        prax_schema::AggregateKind::Min => "min",
        prax_schema::AggregateKind::Max => "max",
    };
    let agg_field = agg.field.as_deref();

    Ok(aggregate_sql(
        kind,
        target_table,
        fk_column,
        parent_table,
        parent_pk,
        agg_field,
    ))
}

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

    const SCHEMA: &str = include_str!("../../../tests/fixtures/schema.prax");

    fn ctx<'a>(schema: &'a prax_schema::Schema, model: &'a Model) -> LowerCtx<'a> {
        LowerCtx::new(schema, model)
    }

    #[test]
    fn lower_order_by_single_block() {
        let schema = parse_schema(SCHEMA).unwrap();
        let model = schema.get_model("User").unwrap().clone();
        let ctx = ctx(&schema, &model);
        let block = syn::parse2::<DslBlock>(quote!({ created_at: desc })).unwrap();
        let out = lower_order_by(&DslValue::Block(block), &ctx)
            .unwrap()
            .to_string();
        assert!(out.contains("OrderBy"));
        assert!(out.contains("desc"));
        assert!(out.contains("created_at"));
    }

    #[test]
    fn lower_order_by_list_of_blocks() {
        let schema = parse_schema(SCHEMA).unwrap();
        let model = schema.get_model("User").unwrap().clone();
        let ctx = ctx(&schema, &model);
        // `DslValue` doesn't impl `Parse`; construct the list manually.
        let v1 = syn::parse2::<DslBlock>(quote!({ id: asc })).unwrap();
        let v2 = syn::parse2::<DslBlock>(quote!({ email: desc })).unwrap();
        let val = DslValue::List(vec![DslValue::Block(v1), DslValue::Block(v2)]);
        let out = lower_order_by(&val, &ctx).unwrap().to_string();
        assert!(out.contains("asc"));
        assert!(out.contains("desc"));
    }

    #[test]
    fn lower_order_by_unknown_field_errors() {
        let schema = parse_schema(SCHEMA).unwrap();
        let model = schema.get_model("User").unwrap().clone();
        let ctx = ctx(&schema, &model);
        let block = syn::parse2::<DslBlock>(quote!({ nope: asc })).unwrap();
        let err = lower_order_by(&DslValue::Block(block), &ctx).unwrap_err();
        assert!(err.to_string().contains("unknown order_by field"));
    }

    #[test]
    fn lower_cursor_unique_column() {
        let schema = parse_schema(SCHEMA).unwrap();
        let model = schema.get_model("User").unwrap().clone();
        let ctx = ctx(&schema, &model);
        let block = syn::parse2::<DslBlock>(quote!({ email: "alice@x.com" })).unwrap();
        let out = lower_cursor(&block, &ctx).unwrap().to_string();
        assert!(out.contains("UserWhereUniqueInput"));
        assert!(out.contains("Email"));
    }

    #[test]
    fn lower_cursor_id_column() {
        let schema = parse_schema(SCHEMA).unwrap();
        let model = schema.get_model("User").unwrap().clone();
        let ctx = ctx(&schema, &model);
        let block = syn::parse2::<DslBlock>(quote!({ id: 42 })).unwrap();
        let out = lower_cursor(&block, &ctx).unwrap().to_string();
        assert!(out.contains("Id"));
    }

    #[test]
    fn lower_cursor_non_unique_field_errors() {
        let schema = parse_schema(SCHEMA).unwrap();
        let model = schema.get_model("User").unwrap().clone();
        let ctx = ctx(&schema, &model);
        let block = syn::parse2::<DslBlock>(quote!({ name: "x" })).unwrap();
        let err = lower_cursor(&block, &ctx).unwrap_err();
        assert!(err.to_string().contains("not a unique"));
    }

    #[test]
    fn lower_cursor_enum_unique_field_errors() {
        // Enum-typed `@unique` columns get no `<Model>WhereUniqueInput`
        // variant from codegen (enum-aware codegen pending), so the
        // lowering must reject them with a clear diagnostic instead of
        // emitting a reference to a variant that was never generated.
        let schema = parse_schema(
            r#"
            enum Status {
                Active
                Banned
            }

            model Account {
                id     Int    @id @auto
                status Status @unique
            }
        "#,
        )
        .unwrap();
        let model = schema.get_model("Account").unwrap().clone();
        let ctx = ctx(&schema, &model);
        let block = syn::parse2::<DslBlock>(quote!({ status: Active })).unwrap();
        let err = lower_cursor(&block, &ctx).unwrap_err();
        assert!(
            err.to_string().contains("only scalar unique columns"),
            "got: {err}"
        );
    }

    #[test]
    fn lower_cursor_multiple_fields_errors() {
        let schema = parse_schema(SCHEMA).unwrap();
        let model = schema.get_model("User").unwrap().clone();
        let ctx = ctx(&schema, &model);
        let block = syn::parse2::<DslBlock>(quote!({ id: 1, email: "x" })).unwrap();
        let err = lower_cursor(&block, &ctx).unwrap_err();
        assert!(err.to_string().contains("exactly one"));
    }

    #[test]
    fn lower_order_by_aggregate_field_emits_subquery_column() {
        // post_count is an @count(posts) field in the fixture schema.
        let schema = parse_schema(SCHEMA).unwrap();
        let model = schema.get_model("User").unwrap().clone();
        let ctx = ctx(&schema, &model);
        let block = syn::parse2::<DslBlock>(quote!({ post_count: desc })).unwrap();
        let out = lower_order_by(&DslValue::Block(block), &ctx)
            .unwrap()
            .to_string();
        assert!(out.contains("OrderBy"), "got: {out}");
        assert!(out.contains("COUNT"), "got: {out}");
        assert!(out.contains("Desc"), "got: {out}");
        // The subquery SQL should be the column expression, not a bare column name.
        assert!(out.contains("SELECT"), "got: {out}");
    }
}