json-server icon indicating copy to clipboard operation
json-server copied to clipboard

find a bug about realtionId

Open youwillbe opened this issue 6 years ago • 3 comments

this is my db:

{
    "users": [],
    "posts": []
}

then I create a user:

POST http://localhost:3000/users
{
    "name": "aaa"
}

now db like this: image then i create a post by user's id:

POST http://localhost:3000/users/1/posts
{
    "name": "post1"
}

now db is: image I can't get post embed into user with_embed, because userId is a string: image if I change the userId to int,it works: image image so I think it's a bug.

youwillbe avatar Dec 20 '18 10:12 youwillbe

@YouWillBe I would say it is not a pure bug as it is an inherited behaviour from lodash APIs.

json-server uses lowdb .. lowdb uses lodash APIs, specifically in this case, the APIs it uses is filter here

Giving the behavior of lodash isMatch function, it uses strict equal === to check values equality when filter using _.matches iteratee shorthand`

For example.. open lodash documentation page and use your developer tools to run the following to see the difference.

var users = [
  { 'user': 'barney', 'age': 36, 'active': true },
  { 'user': 'fred',   'age': 40, 'active': false }
];
_.filter(users, { 'age': 36, 'active': true });
_.filter(users, { 'age': '36', 'active': true });

On the otherhand, I think it could be solve by using custom iteratee filter function

from

const query = {}
const singularResource = pluralize.singular(name)
query[`${singularResource}${opts.foreignKeySuffix}`] = resource.id

to

const singularResource = pluralize.singular(name)
const query = (subResource) => subResource[`${singularResource}${opts.foreignKeySuffix}`] ==  resource.id

I didn't test it, however it should work.

@typicode what do you think? I can push PR with it if you are ok with that.

Nilegfx avatar Dec 24 '18 01:12 Nilegfx

Any update on this? I think this should be a must have feature.

mancioshell avatar Nov 21 '21 10:11 mancioshell

Any update on this? I think this should be a must have feature.

Still like this. I think i should reopen it.

youwillbe avatar Nov 22 '21 06:11 youwillbe