dzql 0.6.19 → 0.6.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/README.md +71 -321
- package/docs/for_ai.md +355 -552
- package/package.json +1 -1
- package/src/cli/codegen/sql.ts +26 -0
package/package.json
CHANGED
package/src/cli/codegen/sql.ts
CHANGED
|
@@ -583,6 +583,31 @@ export function generateGetFunction(name: string, entityIR: EntityIR): string {
|
|
|
583
583
|
// Build SELECT expression excluding hidden fields
|
|
584
584
|
const selectExpr = buildVisibleJsonb(name, entityIR.columns, hidden);
|
|
585
585
|
|
|
586
|
+
// FK expansion for GET (only direct FKs where {key}_id column exists)
|
|
587
|
+
// Reverse FK expansion (one-to-many) should be handled by subscribables for complex queries
|
|
588
|
+
const includes: Record<string, IncludeIR> = entityIR.includes || {};
|
|
589
|
+
const includeKeys = Object.keys(includes);
|
|
590
|
+
const fkExpansion = includeKeys.map(key => {
|
|
591
|
+
const config: IncludeIR = includes[key];
|
|
592
|
+
const targetEntity = config.entity;
|
|
593
|
+
const fkField = `${key}_id`; // Convention: author -> author_id
|
|
594
|
+
|
|
595
|
+
// Only expand direct FKs (key_id column exists on this entity)
|
|
596
|
+
const hasFkColumn = entityIR.columns.some((c: ColumnInfo) => c.name === fkField);
|
|
597
|
+
|
|
598
|
+
if (hasFkColumn) {
|
|
599
|
+
// Direct FK: single object expansion (e.g., author_id -> author object)
|
|
600
|
+
return `
|
|
601
|
+
-- FK: Add ${key} to result (from ${fkField})
|
|
602
|
+
IF (v_result->>'${fkField}') IS NOT NULL THEN
|
|
603
|
+
v_result := v_result || jsonb_build_object('${key}',
|
|
604
|
+
(SELECT to_jsonb(t.*) FROM ${targetEntity} t WHERE t.id = (v_result->>'${fkField}')::int));
|
|
605
|
+
END IF;`;
|
|
606
|
+
}
|
|
607
|
+
// Skip reverse FKs - use subscribables for complex document graphs
|
|
608
|
+
return '';
|
|
609
|
+
}).filter(s => s).join('\n');
|
|
610
|
+
|
|
586
611
|
// M2M expansion for GET
|
|
587
612
|
const m2m: Record<string, ManyToManyIR> = entityIR.manyToMany || {};
|
|
588
613
|
const m2mKeys = Object.keys(m2m);
|
|
@@ -625,6 +650,7 @@ BEGIN
|
|
|
625
650
|
IF v_result IS NULL THEN
|
|
626
651
|
RETURN NULL;
|
|
627
652
|
END IF;
|
|
653
|
+
${fkExpansion}
|
|
628
654
|
${m2mExpansion}
|
|
629
655
|
|
|
630
656
|
RETURN v_result;
|