kami gerami
08/10/2022, 1:24 PMmodel Lock {
id String @id @default(cuid())
from String @unique
to String
locked Boolean @default(false)
User User @relation(fields: [email], references: [email])
email String
@@unique([from, to, email]) // use can only create one entry with matching from, to and email.
}
model User {
id String @id @default(cuid())
name String?
email String @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
role Role @default(USER)
timereport Timereport[]
Lock Lock[]
}
model Timereport {
id String @id @default(cuid())
date String
Event Event @relation(fields: [event], references: [name])
event String
hours Float
User User @relation(fields: [email], references: [email])
email String
Project Project @relation(fields: [project], references: [name])
project String
@@unique([date, event, email]) // Do not allow multiple entries of same date + event + email
}
kami gerami
08/10/2022, 1:25 PMkami gerami
08/10/2022, 1:29 PMasync resolve({ ctx, input }) {
const email = ctx.session?.user?.email;
const { from, to, locked } = input;
const create = await ctx.prisma.lock.upsert({
where: {
from: from,
},
update: {
locked: locked,
from: from,
to: to,
},
create: {
from,
to,
locked,
email: email!,
},
});
return create;
},
})
Richard Ward
08/10/2022, 1:51 PMwhere
pieceRichard Ward
08/10/2022, 1:52 PMasync resolve({ ctx, input }) {
const email = ctx.session?.user?.email;
const { from, to, locked } = input;
const create = await ctx.prisma.lock.upsert({
where: {
from: from,
user: { email: email },
},
update: {
locked: locked,
from: from,
to: to,
},
create: {
from,
to,
locked,
email: email!,
},
});
return create;
},
})
Richard Ward
08/10/2022, 1:52 PMuser: { email: email },
to the where
Richard Ward
08/10/2022, 1:53 PMemail
column in your Lock
table is the user email then you can simplify it to:
async resolve({ ctx, input }) {
const email = ctx.session?.user?.email;
const { from, to, locked } = input;
const create = await ctx.prisma.lock.upsert({
where: {
from: from,
email: email,
},
update: {
locked: locked,
from: from,
to: to,
},
create: {
from,
to,
locked,
email: email!,
},
});
return create;
},
})
kami gerami
08/10/2022, 2:05 PMkami gerami
08/10/2022, 2:05 PMkami gerami
08/10/2022, 2:05 PMkami gerami
08/10/2022, 2:06 PMkami gerami
08/10/2022, 2:07 PMRichard Ward
08/10/2022, 3:28 PMupsertMany
? (if that exists)kami gerami
08/10/2022, 3:46 PMkami gerami
08/10/2022, 3:46 PM