Skip to content

Commit

Permalink
🐞 fix: use correct id field types for connect schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
m1212e committed Jun 17, 2024
1 parent 9213c83 commit b5e42f7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,5 @@ dist

dist
prisma/prismabox
playground
playground
prisma/generated
13 changes: 13 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ model Token {
pendingCredentialCreations PendingCredentialCreateTask[]
}

model Item {
id Int @id @default(autoincrement())
name String
categoryId Int
category Category @relation(fields: [categoryId], references: [id])
}

model Category {
id Int @id @default(autoincrement())
name String @unique
Item Item[]
}

model PendingCredentialCreateTask {
id String @id @default(uuid())
Expand Down
28 changes: 25 additions & 3 deletions src/generators/relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function processRelationsInputCreate(
models: DMMF.Model[] | Readonly<DMMF.Model[]>,
) {
for (const m of models) {
const o = stringifyRelationsInputCreate(m);
const o = stringifyRelationsInputCreate(m, models);
if (o) {
processedRelationsInputCreate.push({
name: m.name,
Expand All @@ -82,7 +82,10 @@ export function processRelationsInputCreate(
Object.freeze(processedRelationsInputCreate);
}

export function stringifyRelationsInputCreate(data: DMMF.Model) {
export function stringifyRelationsInputCreate(
data: DMMF.Model,
allModels: DMMF.Model[] | Readonly<DMMF.Model[]>,
) {
const annotations = extractAnnotations(data.documentation);
if (annotations.isHidden || annotations.isHiddenInput) return undefined;

Expand All @@ -99,10 +102,29 @@ export function stringifyRelationsInputCreate(data: DMMF.Model) {
return undefined;
}

let typeboxIdType = "String";

switch (
allModels.find((m) => m.name === field.type)?.fields.find((f) => f.isId)
?.type
) {
case "String":
typeboxIdType = "String";
break;
case "Int":
typeboxIdType = "Integer";
break;
case "BigInt":
typeboxIdType = "Integer";
break;
default:
throw new Error("Unsupported id type");
}

let connectString = `${getConfig().typeboxImportVariableName}.Object({
id: ${
getConfig().typeboxImportVariableName
}.String(${generateTypeboxOptions(annotations)}),
}.${typeboxIdType}(${generateTypeboxOptions(annotations)}),
},${generateTypeboxOptions(annotations)})`;

if (field.isList) {
Expand Down

0 comments on commit b5e42f7

Please sign in to comment.