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: @id always optional for default values #9

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
fix: @id always optional for default values
  • Loading branch information
jsbrain committed May 28, 2022
commit 5839caf908edcd0b54c9b3791372e3554f06e0df
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -28,8 +28,8 @@ model User {
}

model Post {
id Int @id @default(autoincrement())
user User? @relation(fields: [userId], references: [id])
id String @id @default(cuid())
user User? @relation(fields: [userId], references: [id])
userId Int?
}

2 changes: 1 addition & 1 deletion prisma/typebox/Post.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { Type, Static } from "@sinclair/typebox";
import { Role } from "./Role";

export const Post = Type.Object({
id: Type.Number(),
id: Type.String(),
user: Type.Optional(
Type.Object({
id: Type.Number(),
2 changes: 1 addition & 1 deletion prisma/typebox/PostInput.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { Type, Static } from "@sinclair/typebox";
import { Role } from "./Role";

export const PostInput = Type.Object({
id: Type.Optional(Type.Number()),
id: Type.Optional(Type.String()),
user: Type.Optional(
Type.Object({
id: Type.Optional(Type.Number()),
2 changes: 1 addition & 1 deletion prisma/typebox/User.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ export const User = Type.Object({
role: Type.Optional(Role),
posts: Type.Array(
Type.Object({
id: Type.Number(),
id: Type.String(),
userId: Type.Optional(Type.Number()),
})
),
2 changes: 1 addition & 1 deletion prisma/typebox/UserInput.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ export const UserInput = Type.Object({
role: Type.Optional(Role),
posts: Type.Array(
Type.Object({
id: Type.Optional(Type.Number()),
id: Type.Optional(Type.String()),
userId: Type.Optional(Type.Number()),
})
),
5 changes: 5 additions & 0 deletions prisma/typebox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './User';
export * from './Post';
export * from './UserInput';
export * from './Role';
export * from './PostInput';
4 changes: 2 additions & 2 deletions src/generator/transformDMMF.ts
Original file line number Diff line number Diff line change
@@ -25,8 +25,8 @@ const transformField = (field: DMMF.Field) => {

inputTokens = [...tokens];

// @id cannot be optional except for input if it's auto increment
if (field.isId && (field?.default as any)?.name === 'autoincrement') {
// @id can be optional for input value if it has a default defined
if (field.isId && (field?.default as any)) {
inputTokens.splice(1, 0, 'Type.Optional(');
inputTokens.splice(inputTokens.length, 0, ')');
}