fraiseql_cli/codegen/row_views.rs
1//! Row-shaped SQL view (`vr_*`) DDL generation for the gRPC transport.
2//!
3//! Generates `CREATE VIEW vr_<entity>` statements that extract individual
4//! scalar fields from the JSONB `data` column into typed SQL columns.
5//! These views are CQRS read projections optimized for protobuf wire encoding —
6//! the database returns native typed columns instead of JSON.
7
8use fraiseql_core::{db::dialect::SqlDialect, schema::TypeDefinition};
9
10use super::proto_gen::graphql_to_row_view_type;
11
12/// Generate the DDL for a row-shaped view from a type definition.
13///
14/// The view selects from the command-side table (`tb_<sql_source>`) and
15/// extracts each scalar field as a typed column using the dialect's
16/// `row_view_column_expr()` method.
17///
18/// # Arguments
19///
20/// * `dialect` — SQL dialect for type casting and DDL syntax.
21/// * `type_def` — The GraphQL type definition containing field metadata.
22///
23/// # Returns
24///
25/// A complete DDL string (e.g., `CREATE OR REPLACE VIEW "vr_user" AS ...`).
26pub fn generate_row_view_sql(dialect: &dyn SqlDialect, type_def: &TypeDefinition) -> String {
27 let source_table = format!("tb_{}", type_def.sql_source);
28 let view_name = format!("vr_{}", type_def.sql_source);
29
30 let columns: Vec<(String, String)> = type_def
31 .fields
32 .iter()
33 .filter(|f| f.field_type.is_scalar())
34 .map(|f| {
35 let col_type = graphql_to_row_view_type(&f.field_type.to_graphql_string());
36 let expr =
37 dialect.row_view_column_expr(&type_def.jsonb_column, f.name.as_ref(), &col_type);
38 (f.name.to_string(), expr)
39 })
40 .collect();
41
42 dialect.create_row_view_ddl(&view_name, &source_table, &columns)
43}
44
45/// Generate DDL for all types in a compiled schema.
46///
47/// Returns one DDL statement per type, separated by blank lines.
48/// Non-scalar-only types (those with no scalar fields) are skipped.
49///
50/// # Arguments
51///
52/// * `dialect` — SQL dialect for type casting and DDL syntax.
53/// * `types` — Slice of type definitions to generate views for.
54/// * `include_types` — Whitelist of type names (empty = all).
55/// * `exclude_types` — Blacklist of type names.
56pub fn generate_all_row_views(
57 dialect: &dyn SqlDialect,
58 types: &[TypeDefinition],
59 include_types: &[String],
60 exclude_types: &[String],
61) -> String {
62 let mut ddl_parts = Vec::new();
63
64 for td in types {
65 let name: &str = td.name.as_ref();
66 if !include_types.is_empty() && !include_types.iter().any(|t| t == name) {
67 continue;
68 }
69 if exclude_types.iter().any(|t| t == name) {
70 continue;
71 }
72
73 let has_scalars = td.fields.iter().any(|f| f.field_type.is_scalar());
74 if !has_scalars {
75 continue;
76 }
77
78 ddl_parts.push(generate_row_view_sql(dialect, td));
79 }
80
81 ddl_parts.join("\n\n")
82}