riccardolardi
riccardolardi
Explore posts from servers
PPrisma
Created by riccardolardi on 7/25/2024 in #help-and-questions
findMany where first/latest of 1:n relation meets certain condition
I can't seem to find a solution to this problem: I would like to do a findMany query and get only those entries whose first (latest) relation of many (1:n) meets a condition, eg. a field equals to some value. I'm using latest Prisma with Postgres. Is this not possible? Should I do a rawQuery? Thanks
3 replies
TTCTheo's Typesafe Cult
Created by riccardolardi on 8/26/2023 in #questions
Storing additional data in Server Auth Session
I have some DB data (user profile) that I'd like to store alongside session and user in Server Auth Session so I can check for it in middleware. How do you do that? I would like to fetch it with Prisma.
2 replies
TTCTheo's Typesafe Cult
Created by riccardolardi on 6/7/2023 in #questions
React guarantee updated state variable
I'm setting a state using the setter. In the next line I have a function which relies on the "updated" value of that state. How can I guarantee that value is the updated one and not stale? Since react does some "magic" pooling for state updates / doesn't execute state updates synchronously?
const [myState, setMyState] = useState(false)

setMyState(true)
console.log(myState) // this is not guaranteed to return true
const [myState, setMyState] = useState(false)

setMyState(true)
console.log(myState) // this is not guaranteed to return true
15 replies
TTCTheo's Typesafe Cult
Created by riccardolardi on 4/22/2023 in #questions
Prisma nested write issue
I get a TS error (and failing query) when trying to execute this nested write.
model Applicant {
id String @id @default(uuid()) @db.Uuid
email String @unique
applications Application[]

@@map("applicants")
}

model Application {
id String @id @default(uuid()) @db.Uuid
name String
applicant Applicant? @relation(fields: [applicantId], references: [id])
applicantId String? @map("applicant_id") @db.Uuid

@@index([applicantId])
@@map("applications")
}
model Applicant {
id String @id @default(uuid()) @db.Uuid
email String @unique
applications Application[]

@@map("applicants")
}

model Application {
id String @id @default(uuid()) @db.Uuid
name String
applicant Applicant? @relation(fields: [applicantId], references: [id])
applicantId String? @map("applicant_id") @db.Uuid

@@index([applicantId])
@@map("applications")
}
prisma.application.create({
data: { // Types of property 'name' are incompatible. Type 'string' is not assignable to type 'undefined'
name: "whatever",
applicant: {
create: {
email: "some@email.com",
},
},
},
})
prisma.application.create({
data: { // Types of property 'name' are incompatible. Type 'string' is not assignable to type 'undefined'
name: "whatever",
applicant: {
create: {
email: "some@email.com",
},
},
},
})
What is going on here? Why is this not a valid nested write? When executing the error throws: Unknown arg "applicant" in data.applicant for type ApplicationUncheckedCreateInput.
8 replies
TTCTheo's Typesafe Cult
Created by riccardolardi on 3/31/2023 in #questions
Global data fetching
Using non-app directory Next.js 13 (latest T3), how do you go about fetching data to be consumed globally? Since _app.tsx doesn't allow using getServerSideProps or getStaticProps ?
10 replies
TTCTheo's Typesafe Cult
Created by riccardolardi on 3/30/2023 in #questions
Basic app architecture question for Auth/session state retrieval & storage
This is my first larger full-stack app so please bear with me 😄 I have a basic question regarding how to handle authentication state throughout the app and different components. So my understanding is, that to some extent you don't actually need global state management since you can rely on the hooks like useSession etc. to sync your frontend with whether a user is authenticated or not / get user data. But I have some parts of user data that is stored in another table, which I need to query with tRPC / Prisma separately (user profile data eg. full name, avatar pic url, etc). My idea was to create a global store using Zustand and set that data there once a session is created. From then on, retrieve it only from there, throughout the app and all components that need that data. Then have some invalidation logic to clear the store data when the sessions ends. My reasoning behind this approach using a global store for user data would be that this way there would be no need to fetch the same piece of data multiple times and passing it around as props would lead to some deep prop drilling as the app grows. Now my question is, is there a better, more best-practice way of doing this? Am I missing something crucial? Thanks a lot
33 replies
TTCTheo's Typesafe Cult
Created by riccardolardi on 3/24/2023 in #questions
Best practice for Tailwind class merging? twMerge?
Do you guys use twMerge or similar? I tried using it but it seems to break something within the stack - I have been using it before though with NextJS standalone, so not sure where the problem is: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
4 replies
TTCTheo's Typesafe Cult
Created by riccardolardi on 3/24/2023 in #questions
Server Side Redirect based on tRPC/Prisma query
How can I redirect before page load based on a tRPC/Prisma query? I have a table which I need to check against, before I can decide whether to redirect or not. Sorry if the question might be stupid - bit of a tRPC/Prisma newbie here. Thanks!
10 replies