Skip to content

Conversation

@MaxArt2501
Copy link

I think this is a rather painless change. My only doubt is whether we should overload _expand for this, or use another special parameter, but IMO the use cases should be non-overlapping so we should be fine.

I've added some hints about it in the readme, plus some examples (I think that part wasn't quite crystal-clear).

@MaxArt2501
Copy link
Author

It seems Prettier got angry at parts that I didn't touch.
Huh, what now?

@metapraveen
Copy link

@MaxArt2501 I tried fixing the build here #666

@haleksandre
Copy link

haleksandre commented Jan 20, 2018

@MaxArt2501

If you add an array check to those lines

https://github.com/MaxArt2501/json-server/blob/756b7d2bd8e3952ec947c60c30c34e719f8d33ad/src/server/router/plural.js#L137-L149

Something along the lines of :

... else if (_.isArray(elementValue)) { return elementValue.some(el => el === value) } ...

You'd make it so the list and nested requests work with foreignId when it's an array of length 2+. Without this check, if someone was to use /:resource?foreignId={id} or /:foreign/:id/:resource and the foreignId is of length 2+, it would not find them.

@MaxArt2501
Copy link
Author

Maybe it's an idea worth considering, but how is that related to #366, though?
This PR aims to fix that and nothing else.

@haleksandre
Copy link

haleksandre commented Jan 21, 2018

While you made the many to many support on _embed, if someone were to adopt the foreign key arrays, it would indirectly break the regular (with foreign key as a query) and nested requests.

Let's say you have something like this:

{ "authors": [ { "id": 1, "name": "Alex", "postId": [1,2,3] }, { "id": 2, "name": "Max", "postId": [3] } ], "posts": [ { "id": 1, "title": "Post 1", "content": "Content 1", }, { "id": 2, "title": "Post 2", "content": "Content 2", }, { "id": 3, "title": "Post 3", "content": "Content 3", } ] }
// Request '/authors?postId=1' // or '/posts/1/authors' // Expected Reponse [ { "id": 1, "name": "Alex", "postId": [1,2,3] } ] // Actual Response [] // Request '/authors?postId=3' // or '/posts/3/authors' // Expected Response [ { "id": 1, "name": "Alex", "postId": [1,2,3] }, { "id": 2, "name": "Max", "postId": [3] } ] // Actual Reponse [ { "id": 2, "name": "Max", "postId": [3] } ]

This happens because of this line where the element value gets converted to a string.

return value === elementValue.toString()

When you only have 1 element everything is fine but once you have 2+ elements it "breaks", since toString() will convert [1,2] to "1,2". Which means any authors with multiple posts will always get filtered out of the response because its foreign key postId will never match a single posts id

@HKhademian
Copy link

any change happens?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants