I read about prisma as an ORM provider and than decided to give it a try. My knowledge in database querying especially postrgres is not very experience at the moment, so I got into a situation where I need some help for a proper solution.
Let's assume the following schema.prisma
:
model languages {
language_id Int @id @default(autoincrement())
language_code String @db.VarChar
translations_categories translations_categories[]
}
model categories {
category_id Int @id @default(autoincrement())
category_name String @db.VarChar
translations_categories translations_categories[]
}
model translations_categories {
translation_id Int @id @default(autoincrement())
language_id Int
category_id Int
translation_value String @db.VarChar
categories categories @relation(fields: [category_id], references: [category_id])
language languages @relation(fields: [language_id], references: [language_id])
}
So I try to solve the following update query with some api data:
category_name
translations_categories
(within this step, the provided information contains the language_code
, not the language_id
!)The request body contains the following fields:
{
id: 2
identifier: "TEST-1"
translations: {
de: "german",
en: "english",
fr: "france",
it: "italian"
}
}
The following questions have arisen:
translation
, I thought it would be possible by the provided language_code
on it's own, isn't it?I would suggest creating the schema in the following manner:
model languages {
language_id Int @id @default(autoincrement())
language_code String @unique
translations_categories translations_categories[]
}
model categories {
category_id Int @id @default(autoincrement())
category_name String
translations_categories translations_categories[]
}
model translations_categories {
translation_id Int @id @default(autoincrement())
language_id Int
category_id Int
translation_value String
categories categories @relation(fields: [category_id], references: [category_id])
language languages @relation(fields: [language_id], references: [language_id])
@@unique([language_id, category_id])
}
The reason for this is that language_code
will always be unique for each language so having the @unique
would make it much easier to create or update the category.
So you could create/update the category as follows:
let data = {
id: 2,
identifier: 'TEST-1',
translations: {
de: 'german',
en: 'english',
fr: 'france',
it: 'italian',
},
}
let translations = Object.entries(data.translations).map(([key, val]) => ({
language: {
connectOrCreate: {
where: { language_code: key },
create: { language_code: key },
},
},
translation_value: val,
}))
await prisma.categories.upsert({
where: { category_id: data.id },
create: {
category_name: data.identifier,
translations_categories: { create: translations },
},
update: {
category_name: data.identifier,
translations_categories: { create: translations },
},
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.