diff --git a/.changeset/yellow-knives-itch.md b/.changeset/yellow-knives-itch.md new file mode 100644 index 0000000000..a7731340d9 --- /dev/null +++ b/.changeset/yellow-knives-itch.md @@ -0,0 +1,5 @@ +--- +"electric-sql": patch +--- + +Fixes type errors with WhereUniqueInputSchema in generated client. diff --git a/clients/typescript/src/cli/migrations/migrate.ts b/clients/typescript/src/cli/migrations/migrate.ts index 15a3d0686c..572d90079e 100644 --- a/clients/typescript/src/cli/migrations/migrate.ts +++ b/clients/typescript/src/cli/migrations/migrate.ts @@ -281,6 +281,12 @@ async function _generate(opts: Omit) { // Modify the type of JSON input values in the generated Prisma client // because we deviate from Prisma's typing for JSON values await extendJsonType(config.CLIENT_PATH) + // Modify Prisma's `AtLeast` type because it was used in Prisma v5 + // but we want to keep the type as it was in Prisma v4, + // however, due to a bug in v4 that makes the Prisma generator crash + // if the user has a Prisma v5 dependency in their project, we had to use v5, + // hence, the need for this patch + await modifyAtLeastType(config.CLIENT_PATH) // Delete all files generated for the Prisma client, except the typings await keepOnlyPrismaTypings(config.CLIENT_PATH) console.log(`Successfully generated Electric client at: ./${relativePath}`) @@ -755,6 +761,31 @@ function extendJsonType(prismaDir: string): Promise { return findAndReplaceInFile(inputJsonValueRegex, replacement, prismaTypings) } +/* + * Modifies Prisma's `AtLeast` type such that + * it returns a union of the unique properties: + * type AtLeast = + * K extends any + * ? { [P in K]: O[P] } + * : never + * + * Example usage: + * type ProfileWhereUniqueInput = AtLeast<{ + * id?: number + * userId?: string, + * foo: number + * }, "id" | "userId"> + * + * The above example resolves to the type `{ id?: number } | { userId?: string }` + */ +function modifyAtLeastType(prismaDir: string): Promise { + const prismaTypings = path.join(prismaDir, 'index.d.ts') + const atLeastRegex = /^\s*type\s*AtLeast\s*<[^;]*;/gm + const replacement = + 'type AtLeast = K extends any ? { [P in K]: O[P] } : never' + return findAndReplaceInFile(atLeastRegex, replacement, prismaTypings) +} + async function keepOnlyPrismaTypings(prismaDir: string): Promise { const contents = await fs.readdir(prismaDir) // Delete all files except the generated Electric client and the Prisma typings