11import { createSchema } from "@rocicorp/zero" ;
2+ import type { RelationshipsSchema } from "@rocicorp/zero/react" ;
23import {
34 createTableRelationsHelpers ,
45 getTableName ,
@@ -17,12 +18,8 @@ import {
1718import type {
1819 ColumnIndexKeys ,
1920 DefaultTableColumnsConfig ,
20- FindRelationsForTable ,
21- FindTableByKey ,
2221 FindTableByName ,
23- FindTableKeyByTableName ,
2422 Flatten ,
25- RelationsConfig ,
2623 TableColumnsConfig ,
2724} from "./types" ;
2825import { debugLog , typedEntries } from "./util" ;
@@ -48,124 +45,6 @@ type ZeroCustomType<
4845 ? T & { }
4946 : unknown ;
5047
51- /**
52- * Extracts the table name from a configuration object or string.
53- * @template TTableConfig - The configuration object or string
54- */
55- type ExtractTableConfigName < TTableConfig > = TTableConfig extends {
56- readonly destTable : string ;
57- }
58- ? TTableConfig [ "destTable" ]
59- : TTableConfig extends string
60- ? TTableConfig
61- : never ;
62-
63- /**
64- * Represents the structure of a many-to-many relationship through a junction table.
65- * @template TDrizzleSchema - The complete Drizzle schema
66- * @template TColumnConfig - Configuration for the tables
67- * @template TManyConfig - Configuration for many-to-many relationships
68- * @template TTableName extends keyof TColumnConfig & keyof TManyConfig
69- */
70- type ManyToManyRelationship <
71- TDrizzleSchema extends { [ K in string ] : unknown } ,
72- TColumnConfig extends TableColumnsConfig < TDrizzleSchema > ,
73- TManyConfig extends ManyConfig < TDrizzleSchema > | undefined ,
74- TTableName extends keyof TColumnConfig & keyof TManyConfig ,
75- > = TManyConfig extends undefined
76- ? { }
77- : TTableName extends keyof TDrizzleSchema
78- ? TManyConfig [ TTableName ] extends ManyTableConfig <
79- TDrizzleSchema ,
80- TTableName & string
81- >
82- ? {
83- [ K in keyof TManyConfig [ TTableName ] ] : [
84- {
85- readonly sourceField : string [ ] ;
86- readonly destField : ColumnIndexKeys <
87- FindTableByKey <
88- TDrizzleSchema ,
89- ExtractTableConfigName < TManyConfig [ TTableName ] [ K ] [ 0 ] >
90- >
91- > [ ] ;
92- readonly destSchema : ExtractTableConfigName <
93- TManyConfig [ TTableName ] [ K ] [ 0 ]
94- > ;
95- readonly cardinality : "many" ;
96- } ,
97- {
98- readonly sourceField : string [ ] ;
99- readonly destField : ColumnIndexKeys <
100- FindTableByKey <
101- TDrizzleSchema ,
102- ExtractTableConfigName < TManyConfig [ TTableName ] [ K ] [ 1 ] >
103- >
104- > [ ] ;
105- readonly destSchema : ExtractTableConfigName <
106- TManyConfig [ TTableName ] [ K ] [ 1 ]
107- > ;
108- readonly cardinality : "many" ;
109- } ,
110- ] ;
111- }
112- : { }
113- : { } ;
114-
115- /**
116- * Gets the valid direct relation keys for a table that have corresponding table configurations.
117- * @template TDrizzleSchema - The complete Drizzle schema
118- * @template TColumnConfig - Configuration for the tables
119- * @template TCurrentTableRelations - Relations defined for the current table
120- */
121- type ValidDirectRelationKeys <
122- TDrizzleSchema extends Record < string , unknown > ,
123- TCurrentTableRelations extends Relations ,
124- > = keyof RelationsConfig < TCurrentTableRelations > &
125- {
126- [ K in keyof RelationsConfig < TCurrentTableRelations > ] : RelationsConfig < TCurrentTableRelations > [ K ] [ "referencedTable" ] extends Table < any >
127- ? {
128- [ SchemaKey in keyof TDrizzleSchema ] : TDrizzleSchema [ SchemaKey ] extends RelationsConfig < TCurrentTableRelations > [ K ] [ "referencedTable" ]
129- ? K
130- : never ;
131- } [ keyof TDrizzleSchema ]
132- : never ;
133- } [ keyof RelationsConfig < TCurrentTableRelations > ] ;
134-
135- /**
136- * Represents a direct relationship between tables.
137- * @template TDrizzleSchema - The complete Drizzle schema
138- * @template TColumnConfig - Configuration for the tables
139- * @template TManyConfig - Configuration for many-to-many relationships
140- * @template TCurrentTable - The current table being processed
141- * @template TCurrentTableRelations - Relations defined for the current table
142- */
143- type DirectRelationships <
144- TDrizzleSchema extends Record < string , unknown > ,
145- TCurrentTableRelations extends Relations ,
146- > = [ TCurrentTableRelations ] extends [ never ]
147- ? { }
148- : {
149- [ K in ValidDirectRelationKeys < TDrizzleSchema , TCurrentTableRelations > ] : [
150- {
151- readonly sourceField : string [ ] ;
152- readonly destField : ColumnIndexKeys <
153- FindTableByName <
154- TDrizzleSchema ,
155- RelationsConfig < TCurrentTableRelations > [ K ] [ "referencedTableName" ]
156- >
157- > [ ] ;
158- readonly destSchema : FindTableKeyByTableName <
159- TDrizzleSchema ,
160- RelationsConfig < TCurrentTableRelations > [ K ] [ "referencedTableName" ]
161- > ;
162- readonly cardinality : RelationsConfig < TCurrentTableRelations > [ K ] extends One
163- ? "one"
164- : "many" ;
165- } ,
166- ] ;
167- } ;
168-
16948/**
17049 * Configuration type for many-to-many relationships for a specific table.
17150 * @template TDrizzleSchema - The complete Drizzle schema
@@ -213,87 +92,29 @@ type ManyConfig<TDrizzleSchema extends Record<string, unknown>> = {
21392 string ] ?: ManyTableConfig < TDrizzleSchema , TSourceTableName > ;
21493} ;
21594
216- /**
217- * Represents all referenced Zero schemas for a table, including both direct and many-to-many relationships.
218- */
219- type ReferencedZeroSchemas <
220- TDrizzleSchema extends Record < string , unknown > ,
221- TColumnConfig extends TableColumnsConfig < TDrizzleSchema > ,
222- TManyConfig extends ManyConfig < TDrizzleSchema > | undefined ,
223- TTableName extends keyof TColumnConfig & keyof TManyConfig ,
224- TTable extends Table < any > ,
225- > = DirectRelationships <
226- TDrizzleSchema ,
227- FindRelationsForTable < TDrizzleSchema , TTable >
228- > &
229- ManyToManyRelationship <
230- TDrizzleSchema ,
231- TColumnConfig ,
232- TManyConfig ,
233- TTableName
234- > ;
235-
23695/**
23796 * The mapped Zero schema from a Drizzle schema with version and tables.
238- *
239- * @template TDrizzleSchema - The complete Drizzle schema
240- * @template TCasing - The casing to use for the table name
241- * @template TColumnConfig - Configuration for the tables
242- * @template TManyConfig - Configuration for many-to-many relationships
24397 */
24498type DrizzleToZeroSchema <
24599 TDrizzleSchema extends { [ K in string ] : unknown } ,
246100 TCasing extends ZeroTableCasing = undefined ,
247101 TColumnConfig extends
248102 TableColumnsConfig < TDrizzleSchema > = DefaultTableColumnsConfig < TDrizzleSchema > ,
249- TManyConfig extends ManyConfig < TDrizzleSchema > | undefined = undefined ,
250103> = {
251104 readonly tables : {
252- readonly [ K in keyof TDrizzleSchema &
253- keyof TColumnConfig as TDrizzleSchema [ K ] extends Table < any >
105+ readonly [ K in keyof TDrizzleSchema as TDrizzleSchema [ K ] extends Table < any >
254106 ? K
255107 : never ] : TDrizzleSchema [ K ] extends Table < any >
256- ? Flatten <
257- ZeroTableBuilderSchema <
258- K & string ,
259- TDrizzleSchema [ K ] ,
260- NonNullable < TColumnConfig [ K ] > ,
261- TCasing
262- >
263- >
108+ ? ZeroTableBuilderSchema <
109+ K & string ,
110+ TDrizzleSchema [ K ] ,
111+ TColumnConfig [ K & keyof TColumnConfig ] ,
112+ TCasing
113+ > & { _type : TColumnConfig [ K & keyof TColumnConfig ] }
264114 : never ;
265115 } ;
266116 readonly relationships : {
267- readonly [ K in keyof TDrizzleSchema &
268- keyof TColumnConfig as TDrizzleSchema [ K ] extends Table < any >
269- ? [
270- ReferencedZeroSchemas <
271- TDrizzleSchema ,
272- TColumnConfig ,
273- TManyConfig ,
274- K & keyof TManyConfig & keyof TColumnConfig ,
275- TDrizzleSchema [ K ]
276- > ,
277- ] extends [ never ]
278- ? never
279- : keyof ReferencedZeroSchemas <
280- TDrizzleSchema ,
281- TColumnConfig ,
282- TManyConfig ,
283- K & keyof TManyConfig & keyof TColumnConfig ,
284- TDrizzleSchema [ K ]
285- > extends never
286- ? never
287- : K
288- : never ] : TDrizzleSchema [ K ] extends Table < any >
289- ? ReferencedZeroSchemas <
290- TDrizzleSchema ,
291- TColumnConfig ,
292- TManyConfig ,
293- K & keyof TManyConfig & keyof TColumnConfig ,
294- TDrizzleSchema [ K ]
295- >
296- : never ;
117+ readonly [ table : string ] : RelationshipsSchema ;
297118 } ;
298119} ;
299120
@@ -435,9 +256,7 @@ const drizzleZeroConfig = <
435256 */
436257 readonly debug ?: boolean ;
437258 } ,
438- ) : Flatten <
439- DrizzleToZeroSchema < TDrizzleSchema , TCasing , TColumnConfig , TManyConfig >
440- > => {
259+ ) : Flatten < DrizzleToZeroSchema < TDrizzleSchema , TCasing , TColumnConfig > > => {
441260 let tables : any [ ] = [ ] ;
442261
443262 const tableColumnNamesForSourceTable = new Map < string , Set < string > > ( ) ;
@@ -826,8 +645,7 @@ const drizzleZeroConfig = <
826645 } as any ) as unknown as DrizzleToZeroSchema <
827646 TDrizzleSchema ,
828647 TCasing ,
829- TColumnConfig ,
830- TManyConfig
648+ TColumnConfig
831649 > ;
832650
833651 debugLog (
0 commit comments