typicode / json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Other
72.36k stars 6.98k forks source link

Question about nested collections and relationships #852

Open ravensorb opened 5 years ago

ravensorb commented 5 years ago

If I have a json file like this

{
    "albums": [
        {
            "name": "Album 1",
            "releaseDate": "2018-01-01",
            "songs": [
                { "id": 1 },
                { "id": 2 }
            ]
        },
        {
            "name": "Album 2",
            "releaseDate": "2018-01-01",
            "songs": [
                { "id": 1 },
                { "id": 3 }
            ]
        }
    ],
    "songs": [
        {
            "name": "Song 1",
            "length": "4:33",
            "albums": [
                { "id": "1" }, 
                { "id": "2" }
            ]
        },
        {
            "name": "Song 2",
            "length": "4:33",
            "albums": [
                { "id": "1" }
            ]
        },
        {
            "name": "Song 3",
            "length": "4:33",
            "albums": [
                { "id": "2" }
            ]
        }
    ]
}

should I be able to make a request like this and get a valid response?

http://localhost:1234/albums/1/songs/2

Right now I get an empty result.

Note: if I try this

http://localhost:1234/albums/1?_expand=songs

I get the toString error that has already been submitted in issue #825

sanBastia commented 5 years ago

@ravensorb hei buddy, try this one

http://localhost:1234/albums/1?_expand=song

please keep me updated about the result, if this work, then good :)

rudnetto commented 5 years ago

@typicode what should we return if we try to do an expand with a plural prop misstype?

look at this scenario:

db.json

{
  "tasks": [
    { "id": 1, "title": "Do groceries", "description": "Buy 3 apples and 6 oranges" }
  ],
  "reminders": [
    { "id": 1, "taskId": 1, "dueDate": "2018-09-30" }
  ]
}

if we try to perform /reminders?_expand=tasks, the expand() function accepts tasks prop because after pluralize the prop still is tasks, a resource that does exist in our DB. But when the function concats the prop with opts.foreignKeySuffix, it will search for tasksId property on reminders, that does not exist.

In actual state, I get a toString() error, because the property tasksId does not exist.

What should we return in this scenario?

I'd like to make this contribution, if possible.

ayoubmehd commented 2 years ago

If you looking for a Quick solution just add an empty string in the "Primary Key".

db.json

{
   "parents": [],
   "childs": [
      {
         "id": 1,
         "parentId": ""
      }
    ]
}