Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (generator): fix type error with WhereUniqueInput schemas #1014

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/yellow-knives-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"electric-sql": patch
---

Fixes type errors with WhereUniqueInputSchema in generated client.
31 changes: 31 additions & 0 deletions clients/typescript/src/cli/migrations/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ async function _generate(opts: Omit<GeneratorOptions, 'watch'>) {
// 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}`)
Expand Down Expand Up @@ -755,6 +761,31 @@ function extendJsonType(prismaDir: string): Promise<void> {
return findAndReplaceInFile(inputJsonValueRegex, replacement, prismaTypings)
}

/*
* Modifies Prisma's `AtLeast` type such that
* it returns a union of the unique properties:
* type AtLeast<O extends object, K extends keyof O> =
* 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<void> {
const prismaTypings = path.join(prismaDir, 'index.d.ts')
const atLeastRegex = /^\s*type\s*AtLeast\s*<[^;]*;/gm
const replacement =
'type AtLeast<O extends object, K extends keyof O> = K extends any ? { [P in K]: O[P] } : never'
return findAndReplaceInFile(atLeastRegex, replacement, prismaTypings)
}

async function keepOnlyPrismaTypings(prismaDir: string): Promise<void> {
const contents = await fs.readdir(prismaDir)
// Delete all files except the generated Electric client and the Prisma typings
Expand Down
Loading