Hello! So I came across prisma recently and was a ...
# mongodb
b
Hello! So I came across prisma recently and was a bit interested in using it. I currently have an existing app that is using mongodb and was thinking of instead of using graphql-compose-mongoose to replace it with prisma since it would be nice to be able to have auto completion while writing queries. But I do have a lot of embedded documents and based on the research I’ve done it seems like relations don’t get added automatically and it’s unclear if relations can work with embedded documents. I wanted to post an example mongo collection from my nutritionLogs model and double check to see if this is possible to add a relation for (inside the data array there is a product which would relate to my Product models) or if this is something that’s not supported yet. Because based on what I’ve tried or read this wouldn’t be possible it seems to write a prisma schema for.
Copy code
{
  "_id": {
    "$oid": "5fecd796a088e37d37817f00"
  },
  "meal1": {
    "data": [
      {
        "_id": {
          "$oid": "5feda93cd80307ce947e38dd"
        },
        "product": {
          "$oid": "5febc98e61b47abd9b7cf653"
        },
        "modifier": 1
      },
      {
        "_id": {
          "$oid": "5feda93cd80307ce947e38de"
        },
        "product": {
          "$oid": "5febc96f61b47abd9b7cf652"
        },
        "modifier": 3.6
      }
    ]
  },
  "snack1": {
    "data": [
      {
        "_id": {
          "$oid": "5fecd796a088e37d37817f02"
        },
        "product": {
          "$oid": "5febc96f61b47abd9b7cf652"
        },
        "modifier": 1
      }
    ]
  },
  "meal2": {
    "data": [
      {
        "_id": {
          "$oid": "5fed135f10a6c79a718eb525"
        },
        "product": {
          "$oid": "5febc98e61b47abd9b7cf653"
        },
        "modifier": 2.9
      }
    ]
  },
  "snack2": {
    "data": []
  },
  "meal3": {
    "data": []
  },
  "snack3": {
    "data": []
  },
  "createdAt": {
    "$date": "2021-07-04T19:40:06.319Z"
  },
  "updatedAt": {
    "$date": "2021-07-04T19:40:06.319Z"
  },
  "__v": 0
}
m
Thanks for reaching out! You're correct that relations inside embeds isn't built into Prisma yet. What you can do is link it up manually. Something like: Using the example above:
Copy code
// users data
[
  {
    id: 1,
	name: "Alice",
    meals: [
      { type: "breakfast", friends: [2, 3, 4] }   
    ]
  },
  {
    id: 2,
	name: "Bob",
    meals: []
  },
  {
    id: 3,
    name: "Annie",
    meals: []
  },
  {
    id: 4,
	name: "Jim",
    meals: []
  }
]
Your schema would look like:
Copy code
model User {
  id Int
  name String
  meals Meal[]
}

type Meal {
  type String
  friends Int[] // references user ids
}
👍 1
b
I appreciate your response! So that means if I am I using the prisma client I’d need to loop through each of those ids and do a separate call at that point to get a specific friends model correct? Is there any specific timelines on when there might be support added for relations in embedded documents?