Skip to content

Commit 7d97a5f

Browse files
authored
Merge pull request #134 from 0xcadams/0xcadams/types
fix: simplified types
2 parents 8f63c04 + db15567 commit 7d97a5f

10 files changed

Lines changed: 544 additions & 490 deletions

File tree

no-config-integration/tests/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ import {
2525
message,
2626
user,
2727
} from "../drizzle/schema";
28-
import { schema } from "../schema";
28+
import { schema, type Schema } from "../schema";
2929

3030
const PG_PORT = process.env.PG_VERSION === "17" ? 5732 : 5632;
3131
const ZERO_PORT = process.env.PG_VERSION === "17" ? 5949 : 4949;
3232

33-
export const getNewZero = async () => {
33+
export const getNewZero = async (): Promise<Zero<Schema>> => {
3434
return new Zero({
3535
server: `http://localhost:${ZERO_PORT}`,
3636
userID: "1",

no-config-integration/zero-schema.gen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { ZeroCustomType } from "drizzle-zero";
1919
import type * as drizzleSchema from "./drizzle/schema";
2020
import type { DrizzleToZeroSchema } from "drizzle-zero";
2121

22-
type ZeroSchema = DrizzleToZeroSchema<typeof drizzleSchema, "snake_case">;
22+
type ZeroSchema = DrizzleToZeroSchema<typeof drizzleSchema>;
2323

2424
/**
2525
* The Zero schema object.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-zero",
3-
"version": "0.13.0",
3+
"version": "0.13.1",
44
"description": "Generate Zero schemas from Drizzle ORM schemas",
55
"type": "module",
66
"scripts": {

src/cli/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export async function getGeneratedSchema({
7070
zeroSchemaGenerated.addTypeAlias({
7171
name: "ZeroSchema",
7272
isExported: false,
73-
type: `DrizzleToZeroSchema<typeof drizzleSchema${result.drizzleCasing ? `, "${result.drizzleCasing}"` : ""}>`,
73+
type: `DrizzleToZeroSchema<typeof drizzleSchema>`,
7474
});
7575

7676
zeroSchemaSpecifier = "ZeroSchema";

src/relations.ts

Lines changed: 8 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ import {
1717
import type {
1818
ColumnIndexKeys,
1919
DefaultTableColumnsConfig,
20-
FindRelationsForTable,
21-
FindTableByKey,
2220
FindTableByName,
23-
FindTableKeyByTableName,
2421
Flatten,
25-
RelationsConfig,
2622
TableColumnsConfig,
2723
} from "./types";
2824
import { debugLog, typedEntries } from "./util";
@@ -48,124 +44,6 @@ type ZeroCustomType<
4844
? T & {}
4945
: unknown;
5046

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-
16947
/**
17048
* Configuration type for many-to-many relationships for a specific table.
17149
* @template TDrizzleSchema - The complete Drizzle schema
@@ -213,88 +91,26 @@ type ManyConfig<TDrizzleSchema extends Record<string, unknown>> = {
21391
string]?: ManyTableConfig<TDrizzleSchema, TSourceTableName>;
21492
};
21593

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-
23694
/**
23795
* 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
24396
*/
24497
type DrizzleToZeroSchema<
24598
TDrizzleSchema extends { [K in string]: unknown },
246-
TCasing extends ZeroTableCasing = undefined,
24799
TColumnConfig extends
248100
TableColumnsConfig<TDrizzleSchema> = DefaultTableColumnsConfig<TDrizzleSchema>,
249-
TManyConfig extends ManyConfig<TDrizzleSchema> | undefined = undefined,
250101
> = {
251102
readonly tables: {
252-
readonly [K in keyof TDrizzleSchema &
253-
keyof TColumnConfig as TDrizzleSchema[K] extends Table<any>
103+
readonly [K in keyof TDrizzleSchema as TDrizzleSchema[K] extends Table<any>
254104
? K
255105
: never]: TDrizzleSchema[K] extends Table<any>
256-
? Flatten<
257-
ZeroTableBuilderSchema<
258-
K & string,
259-
TDrizzleSchema[K],
260-
NonNullable<TColumnConfig[K]>,
261-
TCasing
262-
>
263-
>
264-
: never;
265-
};
266-
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]
106+
? ZeroTableBuilderSchema<
107+
K & string,
108+
TDrizzleSchema[K],
109+
TColumnConfig[K & keyof TColumnConfig]
295110
>
296111
: never;
297112
};
113+
readonly relationships: any;
298114
};
299115

300116
/**
@@ -435,9 +251,7 @@ const drizzleZeroConfig = <
435251
*/
436252
readonly debug?: boolean;
437253
},
438-
): Flatten<
439-
DrizzleToZeroSchema<TDrizzleSchema, TCasing, TColumnConfig, TManyConfig>
440-
> => {
254+
): Flatten<DrizzleToZeroSchema<TDrizzleSchema, TColumnConfig>> => {
441255
let tables: any[] = [];
442256

443257
const tableColumnNamesForSourceTable = new Map<string, Set<string>>();
@@ -823,12 +637,7 @@ const drizzleZeroConfig = <
823637
name: key,
824638
relationships: value,
825639
})),
826-
} as any) as unknown as DrizzleToZeroSchema<
827-
TDrizzleSchema,
828-
TCasing,
829-
TColumnConfig,
830-
TManyConfig
831-
>;
640+
} as any) as unknown as DrizzleToZeroSchema<TDrizzleSchema, TColumnConfig>;
832641

833642
debugLog(
834643
config?.debug,

0 commit comments

Comments
 (0)