-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(client): Fix model-vs-model type clashes
In some cases, generated types for one of the models will clash with generated types for another. In that case, we would generate duplicate type. This PR fixes this: approach is very different to namespaces proposal I shared earlier. Turns out, most of the conflicts are solved by just renaming default args types from `ModelArgs` to `ModelDefaultArgs`. Deperecated alias for an old name would still be created if it does not conflict. Somewhat different case is `Payload` type. First, they are created at top level of the file, rather then in `Prisma` namespace. @millsp and I agreed that thouse types are not public, so it is safe to move them to namespace and rename them. So, `UserPayload` would now become `Prisma.$UserPayload`. This allows to both use `UserPayload` as a model name as well as use `Batch` as a model name (we have built-in `BatchPayload` type that is unrelated to aforementioned `Payload` types). Fix #19967 Fix #18902 Fix #16940 Fix #9568 Fix #7518
- Loading branch information
Showing
21 changed files
with
4,398 additions
and
3,878 deletions.
There are no files selected for viewing
1,382 changes: 733 additions & 649 deletions
1,382
...src/__tests__/integration/happy/exhaustive-schema-mongo/__snapshots__/binary.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
1,382 changes: 733 additions & 649 deletions
1,382
...__tests__/integration/happy/exhaustive-schema-mongo/__snapshots__/data-proxy.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
1,382 changes: 733 additions & 649 deletions
1,382
...rc/__tests__/integration/happy/exhaustive-schema-mongo/__snapshots__/library.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
1,201 changes: 635 additions & 566 deletions
1,201
...lient/src/__tests__/integration/happy/exhaustive-schema/__snapshots__/binary.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
1,201 changes: 635 additions & 566 deletions
1,201
...t/src/__tests__/integration/happy/exhaustive-schema/__snapshots__/data-proxy.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
1,201 changes: 635 additions & 566 deletions
1,201
...ient/src/__tests__/integration/happy/exhaustive-schema/__snapshots__/library.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/client/src/generation/TSClient/DefaultArgsAliases.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { DMMFHelper } from '../dmmf' | ||
import * as ts from '../ts-builders' | ||
import { extArgsParam, getLegacyModelArgName, getModelArgName } from '../utils' | ||
|
||
export class DefaultArgsAliases { | ||
private existingArgTypes = new Set<string>() | ||
|
||
registerArgName(name: string) { | ||
this.existingArgTypes.add(name) | ||
} | ||
|
||
generateAliases(dmmf: DMMFHelper) { | ||
const aliases: string[] = [] | ||
for (const modelName of Object.keys(dmmf.typeAndModelMap)) { | ||
const legacyName = getLegacyModelArgName(modelName) | ||
if (this.existingArgTypes.has(legacyName)) { | ||
// alias to and old name is not created if there | ||
// is already existing arg type with the same name | ||
continue | ||
} | ||
|
||
const newName = getModelArgName(modelName) | ||
|
||
aliases.push( | ||
ts.stringify( | ||
ts | ||
.moduleExport( | ||
ts | ||
.typeDeclaration(legacyName, ts.namedType(newName).addGenericArgument(extArgsParam.toArgument())) | ||
.addGenericParameter(extArgsParam), | ||
) | ||
.setDocComment(ts.docComment(`@deprecated Use ${newName} instead`)), | ||
{ indentLevel: 1 }, | ||
), | ||
) | ||
} | ||
|
||
return aliases.join('\n') | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/client/src/generation/TSClient/GenerateContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { GeneratorConfig } from '@prisma/generator-helper' | ||
|
||
import { DMMFHelper } from '../dmmf' | ||
import { GenericArgsInfo } from '../GenericsArgsInfo' | ||
import { DefaultArgsAliases } from './DefaultArgsAliases' | ||
|
||
export interface GenerateContext { | ||
dmmf: DMMFHelper | ||
genericArgsInfo: GenericArgsInfo | ||
defaultArgsAliases: DefaultArgsAliases | ||
generator?: GeneratorConfig | ||
} |
Oops, something went wrong.