Storing a Triple Nested Object

So my prisma schema is as follows:
model Game {
id Int @id @default(autoincrement())
name String @unique
players Player[]
}

model Player {
id Int @id @default(autoincrement())
game Game @relation(fields: [game_id], references: [id])
game_id Int
name String
owned Owned[]
}

model Owned {
id Int @id @default(autoincrement())
player Player @relation(fields: [player_id], references: [id])
player_id Int
name String
houses Int?
}
model Game {
id Int @id @default(autoincrement())
name String @unique
players Player[]
}

model Player {
id Int @id @default(autoincrement())
game Game @relation(fields: [game_id], references: [id])
game_id Int
name String
owned Owned[]
}

model Owned {
id Int @id @default(autoincrement())
player Player @relation(fields: [player_id], references: [id])
player_id Int
name String
houses Int?
}
and the object I am trying to store is the following:
type game = {
name: string,
players: {
name: string,
owned: {
name: string,
houses: number | undefined,
}[]
}[]
}
type game = {
name: string,
players: {
name: string,
owned: {
name: string,
houses: number | undefined,
}[]
}[]
}
How should I transform this object such that I am able to send the object to my database via prisma, optimally in one request
1 Reply
MartinMueller.dev
You have some optiones as prisma allows those keywords createMany or create like
await ctx.prisma.hackathon.create({
data: {
...input.about,
organizer: { connect: { id: organizerId } },
themes: input.about
? {
createMany: {
data: input.about.themes.map((theme) => ({
name: theme.name,
})),
},
}
: undefined,
await ctx.prisma.hackathon.create({
data: {
...input.about,
organizer: { connect: { id: organizerId } },
themes: input.about
? {
createMany: {
data: input.about.themes.map((theme) => ({
name: theme.name,
})),
},
}
: undefined,
Want results from more Discord servers?
Add your server