Skip to content

Commit 1a39bc3

Browse files
committed
fix: simplified types
1 parent 8f63c04 commit 1a39bc3

5 files changed

Lines changed: 84 additions & 348 deletions

File tree

src/relations.ts

Lines changed: 11 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createSchema } from "@rocicorp/zero";
2+
import type { RelationshipsSchema } from "@rocicorp/zero/react";
23
import {
34
createTableRelationsHelpers,
45
getTableName,
@@ -17,12 +18,8 @@ import {
1718
import type {
1819
ColumnIndexKeys,
1920
DefaultTableColumnsConfig,
20-
FindRelationsForTable,
21-
FindTableByKey,
2221
FindTableByName,
23-
FindTableKeyByTableName,
2422
Flatten,
25-
RelationsConfig,
2623
TableColumnsConfig,
2724
} from "./types";
2825
import { 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
*/
24498
type 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(

src/tables.ts

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import type {
2323
ColumnNames,
2424
Columns,
2525
FindPrimaryKeyFromTable,
26-
Flatten,
2726
HasCapital,
2827
} from "./types";
2928
import { debugLog, typedEntries } from "./util";
@@ -57,21 +56,19 @@ type TypeOverride<TCustomType> = {
5756
*/
5857
export type ColumnsConfig<TTable extends Table> =
5958
| boolean
60-
| Partial<
61-
Flatten<{
62-
/**
63-
* The columns to include in the Zero schema.
64-
* Set to true to use default mapping, or provide a TypeOverride for custom mapping.
65-
*/
66-
readonly [KColumn in ColumnNames<TTable>]:
67-
| boolean
68-
| ColumnBuilder<
69-
TypeOverride<
70-
ZeroTypeToTypescriptType[DrizzleDataTypeToZeroType[Columns<TTable>[KColumn]["dataType"]]]
71-
>
72-
>;
73-
}>
74-
>;
59+
| Partial<{
60+
/**
61+
* The columns to include in the Zero schema.
62+
* Set to true to use default mapping, or provide a TypeOverride for custom mapping.
63+
*/
64+
readonly [KColumn in ColumnNames<TTable>]:
65+
| boolean
66+
| ColumnBuilder<
67+
TypeOverride<
68+
ZeroTypeToTypescriptType[DrizzleDataTypeToZeroType[Columns<TTable>[KColumn]["dataType"]]]
69+
>
70+
>;
71+
}>;
7572

7673
/**
7774
* Maps a Drizzle column type to its corresponding Zero type.
@@ -167,46 +164,37 @@ type ZeroColumnDefinition<
167164

168165
/**
169166
* Maps the columns configuration to their Zero schema definitions.
170-
* @template TTable The Drizzle table type
171-
* @template TColumnConfig The columns configuration
172167
*/
173168
export type ZeroColumns<
174169
TTable extends Table,
175-
TColumnConfig extends ColumnsConfig<TTable>,
170+
TColumnConfig extends ColumnsConfig<TTable> | undefined,
176171
TCasing extends ZeroTableCasing,
177172
> = {
178-
[KColumn in ColumnNames<TTable> &
179-
keyof TColumnConfig]: KColumn extends ColumnNames<TTable>
180-
? TColumnConfig[KColumn] extends ColumnBuilder<any>
181-
? TColumnConfig[KColumn]["schema"]
173+
[KColumn in ColumnNames<TTable>]: KColumn extends keyof TColumnConfig
174+
? TColumnConfig[KColumn & keyof TColumnConfig] extends ColumnBuilder<any>
175+
? TColumnConfig[KColumn & keyof TColumnConfig]["schema"]
182176
: ZeroColumnDefinition<TTable, KColumn, TCasing>
183-
: never;
177+
: ZeroColumnDefinition<TTable, KColumn, TCasing>;
184178
};
185179

186180
/**
187181
* Represents the underlying schema for a Zero table.
188-
* @template TTableName The name of the table
189-
* @template TTable The Drizzle table type
190-
* @template TColumnConfig The columns configuration
191182
*/
192183
export type ZeroTableBuilderSchema<
193184
TTableName extends string,
194185
TTable extends Table,
195-
TColumnConfig extends ColumnsConfig<TTable>,
186+
TColumnConfig extends ColumnsConfig<TTable> | undefined,
196187
TCasing extends ZeroTableCasing,
197188
> = {
198189
name: TTableName;
199190
primaryKey: FindPrimaryKeyFromTable<TTable> extends [never]
200191
? readonly [string, ...string[]]
201192
: readonly [string, ...string[]] & FindPrimaryKeyFromTable<TTable>;
202-
columns: Flatten<ZeroColumns<TTable, TColumnConfig, TCasing>>;
193+
columns: ZeroColumns<TTable, TColumnConfig, TCasing>;
203194
}; // Zero does not support this properly yet: & (TTable['_']['name'] extends TTableName ? {} : { serverName: string });
204195

205196
/**
206197
* Represents the complete Zero schema for a Drizzle table.
207-
* @template TTableName The name of the table
208-
* @template TTable The Drizzle table type
209-
* @template TColumnConfig The columns configuration
210198
*/
211199
type ZeroTableBuilder<
212200
TTableName extends string,
@@ -288,9 +276,10 @@ const createZeroTableBuilder = <
288276
const columnsMapped = typedEntries(tableColumns).reduce(
289277
(acc, [key, column]) => {
290278
const columnConfig =
291-
typeof columns === "object" && columns !== null
292-
? columns[key as keyof TColumnConfig]
279+
typeof columns === "object" && columns !== null && columns !== undefined
280+
? columns?.[key as keyof TColumnConfig]
293281
: undefined;
282+
294283
const isColumnConfigOverride = isColumnBuilder(columnConfig);
295284

296285
// From https://github.com/drizzle-team/drizzle-orm/blob/e5c63db0df0eaff5cae8321d97a77e5b47c5800d/drizzle-kit/src/serializer/utils.ts#L5

0 commit comments

Comments
 (0)