Как создать или обновить структуру MongoDB с отношениями One to One с помощью prisma?
В базе данных есть сущность User и ассоциированная с ним структура BasicInfo, которая содержит дополнительную информацию о пользователе. При первичной регистрации она не создается, однако когда пользователь заходит в настройки и сохраняет их, на сервер шлется json и этот json должен как-то поспособствовать созданию этой структуры.
База данных:
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
password String
verified Boolean
role Role @default(GUEST)
basicInfo BasicInfo?
}
model FullName {
id String @id @default(auto()) @map("_id") @db.ObjectId
firstName String?
lastName String?
basicInfo BasicInfo @relation(fields:[basicInfoId], references:[id])
basicInfoId String @db.ObjectId @unique
}
model Career {
id String @id @default(auto()) @map("_id") @db.ObjectId
company String?
role String?
basicInfo BasicInfo @relation(fields:[basicInfoId], references:[id])
basicInfoId String @db.ObjectId @unique
}
model Education {
id String @id @default(auto()) @map("_id") @db.ObjectId
university String?
faculty String?
basicInfo BasicInfo @relation(fields:[basicInfoId], references:[id])
basicInfoId String @db.ObjectId @unique
}
model Location {
id String @id @default(auto()) @map("_id") @db.ObjectId
country String?
region String?
timeZone String?
basicInfo BasicInfo @relation(fields:[basicInfoId], references:[id])
basicInfoId String @db.ObjectId @unique
}
model BasicInfo {
id String @id @default(auto()) @map("_id") @db.ObjectId
user User @relation(fields:[userId], references:[id])
userId String @db.ObjectId @unique
fullName FullName?
career Career?
education Education?
location Location?
}
Интерфейс json объекта, который приходит с сервера:
interface FullNameInput {
firstName?: string
lastName?: string
}
interface CareerInput {
company?: string
role?: string
}
interface EducationInput {
university?: string
faculty?: string
}
interface LocationInput {
country?: string
region?: string
timeZone?: string
}
interface BasicInfoInput {
fullName?: FullNameInput
career?: CareerInput
education?: EducationInput
location?: LocationInput
}
Сервис который нужно реализовать:
async upsetBasicInfo(params: {
where: Prisma.UserWhereUniqueInput
data: BasicInfoInput
}): Promise<BasicInfo> {
const { where, data } = params
return this.prisma.basicInfo.upsert({
where: { userId: where.id },
update: { ...data, user: { connect: { id: where.id } } },
create: { ...data, user: { connect: { id: where.id } } },
})
}
}
'data' в таком виде ему не нравится, нужно каким-то образом превратить эту дату в удобоваримый тип призма. Я попробовал распарсить их превратить в BasicInfoCreateInput и в BasicInfoUpdateInput соответственно, но сам не справился :( запутался в иерархии вложенных 'create', 'where' и прочее.
По существу сервис должен либо создавать либо обновлять уже существующую структуру BasicInfo, увязанное с полем User.