Prisma and MongoDB Composite Types

473 Views Asked by At

I'm working with Prisma for the first time and I'm not sure if I'm going about this in the right way.

Here's a snippet of my schema

model Economy {
  id      String @id @default(auto()) @map("_id") @db.ObjectId
  shops   Shop[]
}

type Shop {
  id          Int @default(0)
  name      String     @default("")
  items     ShopItem[]
  EconomyId String?    @db.ObjectId
}

type ShopItem {
  id          Int @default(0)
  enabled     Boolean       @default(true)
  name        String        @default("")
  price       Int           @default(0)
  description String       @default("")
  commands    ShopCommand[]
}

type ShopCommand {
  command    String    @default("")
}

This does work of course and it's easy for me to destructure in JavaScript however inserting and updating is complicated.. here's an example of updating a "ShopItem"

        const newItem = {
            id: 2,
            name: body.name,
            price: parseInt(body.price),
            description: body.description,
            enabled: body.enabled == 'true', 
            commands: newCommands
        }   

        const newEconomy = await prisma.economy.update({
            where: {
                guildId: guildId,                    
            },
            data: {
                shops: {
                    updateMany: {
                        where: {
                            id: 0,
                        } ,
                        data: {
                            items : {
                                push: [newItem]
                            }
                        }
                    }
                }
            }
        }) 

This is hard to read and I feel like I'm doing this all wrong.

I've looked into how other people go about the same thing but I haven't found much information. Should I even be using composite types, should each type be inside it's own collection instead?

1

There are 1 best solutions below

1
Austin Crim On

It looks like Shop and ShopItem could be their own models. They look sufficiently complex and relational in nature to warrant a separate entity.

Composite types are best suited for smaller, structured pieces of data that might be repeated or shared between several other models while not being necessarily relational or unique in nature.