Тэга Prisma не было, поэтому указал просто js.
Schema:
model User {
id Int @id @default(autoincrement())
name String?
email String @unique
password String
avatar String? @default("noavatar.png")
teams UsersOnTeams[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Team {
id Int @id @default(autoincrement())
name String @unique
avatar String? @default("noavatar.png")
users UsersOnTeams[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model UsersOnTeams {
user User @relation(fields: [userId], references: [id])
userId Int
team Team @relation(fields: [teamId], references: [id])
teamId Int
@@id([userId, teamId])
}
Seed:
const userPassword = await bcrypt.hash('pass', 10);
const user = await prisma.user.upsert({
where: { email: 'mail@gmail.com' },
update: {},
create: {
email: 'mail@gmail.com',
name: 'first last',
password: userPassword,
},
});
const team = await prisma.team.upsert({
where: { name: 'UnknownTeam' },
update: {},
create: {
name: 'UnknownTeam',
},
});
// Это корректный метод добавления новых пользователей в команду?
const updatedTeam = await prisma.team.update({
where: { id: team.id },
data: {
users: {
create: {
userId: user.id,
},
},
},
});