1313 */
1414
1515import type {
16- BlueprintDefinition ,
17- BlueprintTable ,
16+ BlueprintDefinition as BaseBlueprintDefinition ,
17+ BlueprintTable as BaseBlueprintTable ,
1818 BlueprintNode ,
19- BlueprintRelation ,
19+ BlueprintRelation as BaseBlueprintRelation ,
2020 BlueprintField ,
21- BlueprintIndex ,
21+ BlueprintIndex as BaseBlueprintIndex ,
2222 BlueprintFullTextSearch ,
2323} from 'node-type-registry' ;
2424
@@ -29,17 +29,58 @@ import {
2929 type PlatformClient ,
3030} from './helpers' ;
3131
32- // Re-export blueprint types for consumers
33- export type {
34- BlueprintDefinition ,
35- BlueprintTable ,
36- BlueprintNode ,
37- BlueprintRelation ,
38- BlueprintField ,
39- BlueprintIndex ,
40- BlueprintFullTextSearch ,
32+ // ---------------------------------------------------------------------------
33+ // Blueprint types with client-side cross-ref support
34+ // ---------------------------------------------------------------------------
35+ //
36+ // The canonical node-type-registry blueprint shapes require concrete
37+ // table_name / source_table / target_table strings. In provision schemas we
38+ // want to declare tables and cross-references by a local ref ID so schemas
39+ // can be written in any order — provisionBlueprint then resolves
40+ // ref → table_name before sending to construct_blueprint.
41+ //
42+ // These extended types add ref / table_ref / source_ref / target_ref as
43+ // optional siblings of the canonical fields.
44+
45+ export type BlueprintTable = BaseBlueprintTable & {
46+ /** Client-side cross-ref ID; resolved to `table_name` by provisionBlueprint. */
47+ ref ?: string ;
48+ } ;
49+
50+ export type BlueprintIndex = Omit < BaseBlueprintIndex , 'table_name' > & {
51+ table_name ?: string ;
52+ /** Client-side cross-ref to a table by its `ref`; resolved to `table_name`. */
53+ table_ref ?: string ;
4154} ;
4255
56+ type AddRelationRefs < T > = T extends {
57+ source_table : string ;
58+ target_table : string ;
59+ }
60+ ? Omit < T , 'source_table' | 'target_table' > & {
61+ source_table ?: string ;
62+ target_table ?: string ;
63+ /** Client-side cross-ref; resolved to `source_table` by provisionBlueprint. */
64+ source_ref ?: string ;
65+ /** Client-side cross-ref; resolved to `target_table` by provisionBlueprint. */
66+ target_ref ?: string ;
67+ }
68+ : T ;
69+
70+ export type BlueprintRelation = AddRelationRefs < BaseBlueprintRelation > ;
71+
72+ export type BlueprintDefinition = Omit <
73+ BaseBlueprintDefinition ,
74+ 'tables' | 'relations' | 'indexes'
75+ > & {
76+ tables : BlueprintTable [ ] ;
77+ relations ?: BlueprintRelation [ ] ;
78+ indexes ?: BlueprintIndex [ ] ;
79+ } ;
80+
81+ // Re-export passthrough blueprint types for consumers
82+ export type { BlueprintNode , BlueprintField , BlueprintFullTextSearch } ;
83+
4384// ---------------------------------------------------------------------------
4485// Shared constants — standard org-scoped table defaults
4586// ---------------------------------------------------------------------------
@@ -129,24 +170,24 @@ export async function provisionBlueprint(
129170 } ) ;
130171
131172 const serverDef : Record < string , unknown > = {
132- tables : definition . tables . map ( ( t ) => ( {
173+ tables : definition . tables . map ( ( t ) : Record < string , unknown > => ( {
133174 ref : t . ref ,
134175 table_name : t . table_name ,
135176 nodes : t . nodes ,
136177 fields : t . fields ,
137178 // Explicitly disable security — prevents construct_blueprint from
138179 // defaulting grant_roles to ['authenticated'] which would generate
139180 // invalid GRANT SQL when the grants array is empty.
140- grant_roles : [ ] ,
141- grants : [ ] ,
142- policies : [ ] ,
181+ grant_roles : [ ] as string [ ] ,
182+ grants : [ ] as unknown [ ] ,
183+ policies : [ ] as unknown [ ] ,
143184 use_rls : false ,
144185 } ) ) ,
145- relations : resolvedRelations . map ( ( r ) => ( {
186+ relations : resolvedRelations . map ( ( r ) : Record < string , unknown > => ( {
146187 ...r ,
147- grant_roles : [ ] ,
148- grant_privileges : [ ] ,
149- policies : [ ] ,
188+ grant_roles : [ ] as string [ ] ,
189+ grant_privileges : [ ] as unknown [ ] ,
190+ policies : [ ] as unknown [ ] ,
150191 } ) ) ,
151192 indexes : resolvedIndexes ,
152193 full_text_searches : definition . full_text_searches ?? [ ] ,
0 commit comments