iPheNoMeNaL-oG
iPheNoMeNaL-oG
PPrisma
Created by iPheNoMeNaL-oG on 7/15/2024 in #help-and-questions
Using upsert when `Notifications` table doesn't exist yet for user
I have a query to update a users notification preferences, but there's no data existing data yet inside the table. How can I change the query to accodomate that?
await prisma.notifications.upsert({
where: { id },
data: {
type,
communication,
marketing,
social,
security,
mobile,
},
});
await prisma.notifications.upsert({
where: { id },
data: {
type,
communication,
marketing,
social,
security,
mobile,
},
});
I assume upsert is what needs to be used? For reference here is my Notifications model
model Notifications {
id String @id @default(cuid())
userId String
type NotificationType @default(none)
mobile Boolean
communication Boolean
social Boolean
marketing Boolean
security Boolean
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
model Notifications {
id String @id @default(cuid())
userId String
type NotificationType @default(none)
mobile Boolean
communication Boolean
social Boolean
marketing Boolean
security Boolean
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
3 replies
PPrisma
Created by iPheNoMeNaL-oG on 6/13/2024 in #help-and-questions
Updating user info in db using forms
I have a basic user model
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
password String
username String @unique
role Role @default(user)
accounts Account[]
sessions Session[]
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
password String
username String @unique
role Role @default(user)
accounts Account[]
sessions Session[]
}
I have a form that allows the user to update email, username, etc. I'm using a simple update method
const updateUser = await prisma.user.update({
where: {
username: username,
},
data: {
username: data.username,
},
});
const updateUser = await prisma.user.update({
where: {
username: username,
},
data: {
username: data.username,
},
});
For those using nextjs, is it best to put this function in a server action or make an api route and call it there?
10 replies