DollarNavalex
DollarNavalex
Explore posts from servers
PPrisma
Created by DollarNavalex on 6/13/2024 in #help-and-questions
Import Enum from my TS to the schema
Hey, Wanted to know if it's possible to import an enum inside my schema.prisma file instead of defining it inside ? Because all my types used by both front & back are located to a dedicated package, and it actually force me to define my enum in 2 distinct places, where I would like to only have it in my types package. Thanks!
3 replies
PPrisma
Created by DollarNavalex on 6/12/2024 in #help-and-questions
Export client types in custom package
Hi, Actually I have a project in a monorepo, with a app/ folder and api/ folder. My API folder contain a node project that runs a web server, and also contains my prisma install. Now that i'm setting up the front, I would like to use the sames types as my back, but can't see a good solution to to that. My goal would be to have a types/ folder at the same root as my both projects, that is literally a node_module that contains all types that i want to use across projects. I tried to create a 2nd Prisma client that generate inside this folder, but when I try to build my package with a simple file that `export * from "myCustomPrismaClientPath/index", it throw me a lot of typescript error because it can't find packages like @babel/types or "undici-types". Do you guys have any idea or suggest to fix that ?
2 replies
PPrisma
Created by DollarNavalex on 5/27/2024 in #help-and-questions
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 !
5 replies
PPrisma
Created by DollarNavalex on 4/24/2024 in #help-and-questions
Extends result relations
Hi, I have 3 different objects:
model User {
id String @id @default(uuid())
auth_id String @unique
firstname String
lastname String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sessions Session[]
challenges Challenge[]
exoskeletons ExoskeletonAttribution[]
}

model Exoskeleton {
id String @id @default(uuid())
serial String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sessions Session[]
attributions ExoskeletonAttribution[]
}

model ExoskeletonAttribution {
id String @id @default(uuid())
exoskeletonId String
userId String
createdAt DateTime @default(now())
revokedAt DateTime?
exoskeleton Exoskeleton @relation(fields: [exoskeletonId], references: [id])
user User @relation(fields: [userId], references: [id])
}
model User {
id String @id @default(uuid())
auth_id String @unique
firstname String
lastname String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sessions Session[]
challenges Challenge[]
exoskeletons ExoskeletonAttribution[]
}

model Exoskeleton {
id String @id @default(uuid())
serial String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sessions Session[]
attributions ExoskeletonAttribution[]
}

model ExoskeletonAttribution {
id String @id @default(uuid())
exoskeletonId String
userId String
createdAt DateTime @default(now())
revokedAt DateTime?
exoskeleton Exoskeleton @relation(fields: [exoskeletonId], references: [id])
user User @relation(fields: [userId], references: [id])
}
The last one have a manyToOne relation for both User & Exoskeleton. My goal is to add a custom field to User, named currentExoskeleton, based on the relation to exoskeletonAttribution (i take the last in date), to fetch to associated Exoskeleton. So i started to wrote this:
result: {
user: {
currentExoskeleton: {
needs: {
exoskeletonAttribution: true,
},
compute(user) {
// nothing here for the moment
}
},
},
},
result: {
user: {
currentExoskeleton: {
needs: {
exoskeletonAttribution: true,
},
compute(user) {
// nothing here for the moment
}
},
},
},
But typescript already crying on me on the needs part, "impossible to align type boolean to never". And i would also like to know if it's possible to user async/await on compute ? (if yes, how ? should i return an async function from the compute function ?) Thanks 😉
2 replies