Skip to content

Commit bfefdb1

Browse files
committed
fix: types simplification
1 parent 5c3296d commit bfefdb1

7 files changed

Lines changed: 62 additions & 123 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.

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: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { createSchema } from "@rocicorp/zero";
2-
import type { RelationshipsSchema } from "@rocicorp/zero/react";
32
import {
43
createTableRelationsHelpers,
54
getTableName,
@@ -97,7 +96,6 @@ type ManyConfig<TDrizzleSchema extends Record<string, unknown>> = {
9796
*/
9897
type DrizzleToZeroSchema<
9998
TDrizzleSchema extends { [K in string]: unknown },
100-
TCasing extends ZeroTableCasing = undefined,
10199
TColumnConfig extends
102100
TableColumnsConfig<TDrizzleSchema> = DefaultTableColumnsConfig<TDrizzleSchema>,
103101
> = {
@@ -108,14 +106,11 @@ type DrizzleToZeroSchema<
108106
? ZeroTableBuilderSchema<
109107
K & string,
110108
TDrizzleSchema[K],
111-
TColumnConfig[K & keyof TColumnConfig],
112-
TCasing
113-
> & {_type: TColumnConfig[K & keyof TColumnConfig]}
109+
TColumnConfig[K & keyof TColumnConfig]
110+
>
114111
: never;
115112
};
116-
readonly relationships: {
117-
readonly [table: string]: RelationshipsSchema;
118-
};
113+
readonly relationships: any;
119114
};
120115

121116
/**
@@ -256,7 +251,7 @@ const drizzleZeroConfig = <
256251
*/
257252
readonly debug?: boolean;
258253
},
259-
): Flatten<DrizzleToZeroSchema<TDrizzleSchema, TCasing, TColumnConfig>> => {
254+
): Flatten<DrizzleToZeroSchema<TDrizzleSchema, TColumnConfig>> => {
260255
let tables: any[] = [];
261256

262257
const tableColumnNamesForSourceTable = new Map<string, Set<string>>();
@@ -642,11 +637,7 @@ const drizzleZeroConfig = <
642637
name: key,
643638
relationships: value,
644639
})),
645-
} as any) as unknown as DrizzleToZeroSchema<
646-
TDrizzleSchema,
647-
TCasing,
648-
TColumnConfig
649-
>;
640+
} as any) as unknown as DrizzleToZeroSchema<TDrizzleSchema, TColumnConfig>;
650641

651642
debugLog(
652643
config?.debug,

src/tables.ts

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ import {
1919
drizzleDataTypeToZeroType,
2020
type ZeroTypeToTypescriptType,
2121
} from "./drizzle-to-zero";
22-
import type {
23-
ColumnNames,
24-
Columns,
25-
FindPrimaryKeyFromTable,
26-
HasCapital,
27-
} from "./types";
22+
import type { ColumnNames, Columns, FindPrimaryKeyFromTable } from "./types";
2823
import { debugLog, typedEntries } from "./util";
2924

3025
export type { ColumnBuilder, ReadonlyJSONValue, TableBuilderWithColumns };
@@ -72,9 +67,6 @@ export type ColumnsConfig<TTable extends Table> =
7267

7368
/**
7469
* Maps a Drizzle column type to its corresponding Zero type.
75-
* @template TTable The Drizzle table type
76-
* @template KColumn The column name
77-
* @template CD The column definition type
7870
*/
7971
type ZeroMappedColumnType<
8072
TTable extends Table,
@@ -92,9 +84,6 @@ type ZeroMappedColumnType<
9284
/**
9385
* Maps a Drizzle column to its corresponding TypeScript type in Zero.
9486
* Handles special cases like enums and custom types.
95-
* @template TTable The Drizzle table type
96-
* @template KColumn The column name
97-
* @template CD The column definition type
9887
*/
9988
type ZeroMappedCustomType<
10089
TTable extends Table,
@@ -118,63 +107,29 @@ type ZeroMappedCustomType<
118107

119108
/**
120109
* Defines the structure of a column in the Zero schema.
121-
* @template TTable The Drizzle table type
122-
* @template KColumn The column name
123-
* @template CD The column definition type
124110
*/
125111
type ZeroColumnDefinition<
126112
TTable extends Table,
127113
KColumn extends ColumnNames<TTable>,
128-
TCasing extends ZeroTableCasing,
129-
CD extends ColumnDefinition<TTable, KColumn>["_"] = ColumnDefinition<
130-
TTable,
131-
KColumn
132-
>["_"],
133-
BaseDefinition extends {
134-
optional: false;
135-
type: ZeroMappedColumnType<TTable, KColumn>;
136-
customType: ZeroMappedCustomType<TTable, KColumn>;
137-
} = {
138-
optional: false;
139-
type: ZeroMappedColumnType<TTable, KColumn>;
140-
customType: ZeroMappedCustomType<TTable, KColumn>;
141-
},
142-
BaseOptional extends Omit<BaseDefinition, "optional"> & {
143-
optional: true;
144-
} = Omit<BaseDefinition, "optional"> & { optional: true },
145-
> = (CD extends {
146-
hasDefault: true;
147-
hasRuntimeDefault: false;
148-
}
149-
? BaseOptional
150-
: CD extends { notNull: true }
151-
? BaseDefinition
152-
: Omit<BaseDefinition, "optional"> & { optional: true }) &
153-
(CD extends { name: KColumn }
154-
? TCasing extends "snake_case"
155-
? HasCapital<CD["name"]> extends true
156-
? { serverName: string }
157-
: {}
158-
: TCasing extends "camelCase"
159-
? HasCapital<CD["name"]> extends false
160-
? { serverName: string }
161-
: {}
162-
: {}
163-
: { serverName: string });
114+
> = {
115+
optional: boolean;
116+
type: ZeroMappedColumnType<TTable, KColumn>;
117+
customType: ZeroMappedCustomType<TTable, KColumn>;
118+
serverName?: string;
119+
};
164120

165121
/**
166122
* Maps the columns configuration to their Zero schema definitions.
167123
*/
168124
export type ZeroColumns<
169125
TTable extends Table,
170126
TColumnConfig extends ColumnsConfig<TTable> | undefined,
171-
TCasing extends ZeroTableCasing,
172127
> = {
173128
[KColumn in ColumnNames<TTable>]: KColumn extends keyof TColumnConfig
174129
? TColumnConfig[KColumn & keyof TColumnConfig] extends ColumnBuilder<any>
175130
? TColumnConfig[KColumn & keyof TColumnConfig]["schema"]
176-
: ZeroColumnDefinition<TTable, KColumn, TCasing>
177-
: ZeroColumnDefinition<TTable, KColumn, TCasing>;
131+
: ZeroColumnDefinition<TTable, KColumn>
132+
: ZeroColumnDefinition<TTable, KColumn>;
178133
};
179134

180135
/**
@@ -184,13 +139,12 @@ export type ZeroTableBuilderSchema<
184139
TTableName extends string,
185140
TTable extends Table,
186141
TColumnConfig extends ColumnsConfig<TTable> | undefined,
187-
TCasing extends ZeroTableCasing,
188142
> = {
189143
name: TTableName;
190144
primaryKey: FindPrimaryKeyFromTable<TTable> extends [never]
191145
? readonly [string, ...string[]]
192146
: readonly [string, ...string[]] & FindPrimaryKeyFromTable<TTable>;
193-
columns: ZeroColumns<TTable, TColumnConfig, TCasing>;
147+
columns: ZeroColumns<TTable, TColumnConfig>;
194148
}; // Zero does not support this properly yet: & (TTable['_']['name'] extends TTableName ? {} : { serverName: string });
195149

196150
/**
@@ -200,9 +154,8 @@ type ZeroTableBuilder<
200154
TTableName extends string,
201155
TTable extends Table,
202156
TColumnConfig extends ColumnsConfig<TTable>,
203-
TCasing extends ZeroTableCasing,
204157
> = TableBuilderWithColumns<
205-
Readonly<ZeroTableBuilderSchema<TTableName, TTable, TColumnConfig, TCasing>>
158+
Readonly<ZeroTableBuilderSchema<TTableName, TTable, TColumnConfig>>
206159
>;
207160

208161
/**
@@ -242,7 +195,7 @@ const createZeroTableBuilder = <
242195
* The casing to use for the table name.
243196
*/
244197
casing?: TCasing,
245-
): ZeroTableBuilder<TTableName, TTable, TColumnConfig, TCasing> => {
198+
): ZeroTableBuilder<TTableName, TTable, TColumnConfig> => {
246199
const actualTableName = getTableName(table);
247200
const tableColumns = getTableColumns(table);
248201
const tableConfig = getTableConfigForDatabase(table);
@@ -316,10 +269,10 @@ const createZeroTableBuilder = <
316269

317270
const type =
318271
drizzleColumnTypeToZeroType[
319-
column.columnType as keyof DrizzleColumnTypeToZeroType
272+
column.columnType as keyof typeof drizzleColumnTypeToZeroType
320273
] ??
321274
drizzleDataTypeToZeroType[
322-
column.dataType as keyof DrizzleDataTypeToZeroType
275+
column.dataType as keyof typeof drizzleDataTypeToZeroType
323276
] ??
324277
null;
325278

@@ -394,8 +347,7 @@ const createZeroTableBuilder = <
394347
.primaryKey(...primaryKeys) as ZeroTableBuilder<
395348
TTableName,
396349
TTable,
397-
TColumnConfig,
398-
TCasing
350+
TColumnConfig
399351
>;
400352
};
401353

src/types.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,6 @@ export type FindTableByName<
114114
Table<any>
115115
>;
116116

117-
/**
118-
* Type guard that checks if a string has a capital letter.
119-
* @template S The string to check
120-
*/
121-
export type HasCapital<S extends string> =
122-
S extends `${infer First}${infer Rest}`
123-
? First extends Uppercase<First>
124-
? First extends Lowercase<First>
125-
? HasCapital<Rest>
126-
: true
127-
: HasCapital<Rest>
128-
: false;
129-
130117
/**
131118
* Utility type that flattens an object type by removing any intermediate interfaces.
132119
* @template T The type to flatten

tests/cli.test.ts

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
} from "../src/cli/config";
99
import { getGeneratedSchema } from "../src/cli/shared";
1010
import type { DrizzleToZeroSchema } from "../src/relations";
11-
import * as oneToOneSchema from "./schemas/one-to-one.zero";
1211

1312
describe("getGeneratedSchema", () => {
1413
let tsProject: Project;
@@ -43,7 +42,27 @@ describe("getGeneratedSchema", () => {
4342
tsProject,
4443
result: {
4544
type: "config",
46-
zeroSchema: oneToOneSchema.schema,
45+
zeroSchema: {
46+
tables: {
47+
users: {
48+
name: "users",
49+
primaryKey: ["id"],
50+
columns: {
51+
id: {
52+
type: "integer",
53+
optional: false,
54+
customType: undefined,
55+
},
56+
name: {
57+
type: "string",
58+
optional: false,
59+
customType: undefined,
60+
},
61+
},
62+
},
63+
},
64+
relationships: {},
65+
},
4766
exportName: "schema",
4867
zeroSchemaTypeDeclarations: zeroSchemaTypeDecl,
4968
},
@@ -54,37 +73,12 @@ describe("getGeneratedSchema", () => {
5473
expect(generatedSchema).toContain("export const schema = {");
5574
expect(generatedSchema).toContain('"users": {');
5675

57-
// Check actual schema to ensure our expectations match reality
58-
if (!generatedSchema.includes('"profileInfo": {')) {
59-
// If profileInfo isn't in the schema, check what tables actually are in the test schema
60-
console.log(
61-
"Tables in schema:",
62-
Object.keys(oneToOneSchema.schema.tables),
63-
);
64-
// Adjust test to match actual schema structure
65-
const tables = Object.keys(oneToOneSchema.schema.tables);
66-
expect(tables.length).toBeGreaterThan(0);
67-
tables.forEach((table) => {
68-
expect(generatedSchema).toContain(`"${table}": {`);
69-
});
70-
} else {
71-
expect(generatedSchema).toContain('"profileInfo": {');
72-
}
73-
7476
expect(generatedSchema).toContain("export type Schema = typeof schema");
7577

7678
// Check for fields from the one-to-one schema
7779
expect(generatedSchema).toContain('"id": {');
7880
expect(generatedSchema).toContain('"name": {');
7981

80-
// Similarly, check for these fields conditionally
81-
if (generatedSchema.includes('"userId": {')) {
82-
expect(generatedSchema).toContain('"userId": {');
83-
}
84-
if (generatedSchema.includes('"metadata": {')) {
85-
expect(generatedSchema).toContain('"metadata": {');
86-
}
87-
8882
// Verify the auto-generated comment header
8983
expect(generatedSchema).toContain(
9084
"This file was automatically generated by drizzle-zero",
@@ -403,7 +397,7 @@ describe("getGeneratedSchema", () => {
403397
},
404398
},
405399
relationships: {},
406-
} as any, // Type assertion to avoid TypeScript errors
400+
},
407401
drizzleSchemaSourceFile: mockSource,
408402
drizzleCasing: null,
409403
},
@@ -436,7 +430,22 @@ describe("getGeneratedSchema", () => {
436430
tsProject,
437431
result: {
438432
type: "config",
439-
zeroSchema: oneToOneSchema.schema,
433+
zeroSchema: {
434+
tables: {
435+
users: {
436+
name: "users",
437+
primaryKey: ["id"],
438+
columns: {
439+
id: {
440+
type: "integer",
441+
optional: false,
442+
customType: undefined,
443+
},
444+
},
445+
},
446+
},
447+
relationships: {},
448+
},
440449
exportName: "schema",
441450
zeroSchemaTypeDeclarations: zeroSchemaTypeDecl,
442451
},

0 commit comments

Comments
 (0)