P
Prisma•7mo ago
DollarNavalex

Extend client Type loose relation

Hey there, i'm having some trouble when I extend a Prisma client type (in typescript). I have a User, which have a oneToOne relation with a Preferences model, looking like that:
model User {
id String @id @default(uuid())
auth_id String @unique
firstname String
lastname String
data Json
activationToken String? @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
following User[] @relation("Follow")
followers User[] @relation("Follow")
exoskeletons ExoskeletonAttribution[]
challenges Challenge[]
authoredChallenges Challenge[] @relation(name: "challengeAuthor")
surveys Survey[]
preferences Preferences?
}

model Preferences {
id String @id @default(uuid())
userId String @unique
user User @relation(fields: [userId], references: [id])
weekStart WeekStartDay @default(MONDAY)
darkMode Boolean @default(false)
unitType UnitType @default(METRIC)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model User {
id String @id @default(uuid())
auth_id String @unique
firstname String
lastname String
data Json
activationToken String? @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
following User[] @relation("Follow")
followers User[] @relation("Follow")
exoskeletons ExoskeletonAttribution[]
challenges Challenge[]
authoredChallenges Challenge[] @relation(name: "challengeAuthor")
surveys Survey[]
preferences Preferences?
}

model Preferences {
id String @id @default(uuid())
userId String @unique
user User @relation(fields: [userId], references: [id])
weekStart WeekStartDay @default(MONDAY)
darkMode Boolean @default(false)
unitType UnitType @default(METRIC)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
I made this interface:
import { Preferences, User } from "@prisma/client"

export interface UserWithRoles extends User {
roles: string[]
}
import { Preferences, User } from "@prisma/client"

export interface UserWithRoles extends User {
roles: string[]
}
And I have a function that find a user, aggregate it in a new object with roles array, and the output of this function seems to drop the preferences relation, even if I explicitly include it in my findUnique function. Not an expert in typescript yet, so I may miss something basic, but it should works from what I understand. Need a lil help on that one please, Thanks !
Solution:
And adding them manually would have force me to explicitly define all Preferences properties too, since the prisma findUnique function doesn't return a Preferences object, but a custom object based on my query (from what I understand), so the object is not Exactly like the Preferences object would be. Here is what i done for my issue: ```typescript export interface UserWithRoles extends Prisma.UserGetPayload<{ include: { preferences: true } }>{...
Operating against partial structures of your model types | Prisma D...
This page documents various scenarios for using the generated types from the Prisma namespace
Jump to solution
3 Replies
Nurul
Nurul•7mo ago
Hey @DollarNavalex 👋 By default the relation fields are not the part of the generated type, so your User type would not have reference to following, followers, preferences etc, relation fields. If you want to include them, then you need to specify them explicitly. Here's how you can do that: https://www.prisma.io/docs/orm/prisma-client/type-safety/operating-against-partial-structures-of-model-types#problem-using-variations-of-the-generated-model-type
Operating against partial structures of your model types | Prisma D...
This page documents various scenarios for using the generated types from the Prisma namespace
DollarNavalex
DollarNavalexOP•7mo ago
Hi, I finally found the Type Helpers on the doc, works much better now, thanks 🙂
Solution
DollarNavalex
DollarNavalex•7mo ago
And adding them manually would have force me to explicitly define all Preferences properties too, since the prisma findUnique function doesn't return a Preferences object, but a custom object based on my query (from what I understand), so the object is not Exactly like the Preferences object would be. Here is what i done for my issue:
export interface UserWithRoles extends Prisma.UserGetPayload<{ include: { preferences: true } }>{
roles: string[]
}
export interface UserWithRoles extends Prisma.UserGetPayload<{ include: { preferences: true } }>{
roles: string[]
}
And here is a source for type helpers of prisma: https://www.prisma.io/docs/orm/prisma-client/type-safety/operating-against-partial-structures-of-model-types
Operating against partial structures of your model types | Prisma D...
This page documents various scenarios for using the generated types from the Prisma namespace
Want results from more Discord servers?
Add your server