Let’s say you have two different entities in your app, Parent and Child, in which each Parent instance can be associated to multiple Child instances, and each Child instance can be associated to a single Parent instance, aka one-to-many relationship. There are many different ways to model your backend in Parse Server, including:
- You can create a single class called Parent, with a field called children of type array in which you will store each Child instance as an embeded document inside the Parent instance. Your data would be:
// Parent Class Data
{
"objectId": "ParentId1",
"name": "Parent 1",
"children": [
{ "name": "Child 1.1" },
{ "name": "Child 1.2" }
]
}
{
"objectId": "ParentId2",
"name": "Parent 2",
"children": [
{ "name": "Child 2.1" },
{ "name": "Child 2.2" }
]
}
- You can create a single class called Child, with a field called parent of type object in which you will store each Parent instance as an embeded document inside the Child instance. Your data would be:
// Child Class Data
{
"objectId": "ChildId11",
"name": "Child 1.1",
"parent": {
"name": "Parent 1"
}
}
{
"objectId": "ChildId12",
"name": "Child 1.2",
"parent": {
"name": "Parent 1"
}
}
{
"objectId": "ChildId21",
"name": "Child 2.1",
"parent": {
"name": "Parent 2"
}
}
{
"objectId": "ChildId22",
"name": "Child 2.2",
"parent": {
"name": "Parent 2"
}
}
- You can create two different classes, Parent and Child, and create a pointer called parent in the class Child with target class set to Parent. In this case, you do not have an embeded document, since the information of each Parent and Child instances are stored in their own classes. Your data would be:
// Parent Class Data
{
"objectId": "ParentId1",
"name": "Parent 1"
}
{
"objectId": "ParentId2",
"name": "Parent 2"
}
// Child Class Data
{
"objectId": "ChildId11",
"name": "Child 1.1",
"parent": {
"__type": "Pointer",
"className": "Parent",
"objectId": "ParentId1"
}
}
{
"objectId": "ChildId12",
"name": "Child 1.2",
"parent": {
"__type": "Pointer",
"className": "Parent",
"objectId": "ParentId1"
}
}
{
"objectId": "ChildId21",
"name": "Child 2.1",
"parent": {
"__type": "Pointer",
"className": "Parent",
"objectId": "ParentId2"
}
}
{
"objectId": "ChildId22",
"name": "Child 2.2",
"parent": {
"__type": "Pointer",
"className": "Parent",
"objectId": "ParentId2"
}
}
There is no right way to go. You need to choose or item combine any of these options (and actually there are different ways to model the backend) according to your app needs, and mainly the queries that you will need to perform later.