Comments (22)
For nested resources to work, you need to structure your db.json
this way:
{
"environments": [
{ "id": 12 },
{ "id": 19 }
],
"reports": [
{ "id": 17, "environmentId": 12 }
{ "id": 25, "environmentId": 12 }
]
}
Then /environments/12/reports
should return reports that have environmentId === 12
from json-server.
@patotoma have you tried custom routes https://github.com/typicode/json-server#add-custom-routes? You can map many different kind of routes and for example nested resources.
"/some/nested/route/posts/:postId/comments/:commentId": "/comments/:commentId"
/some/nested/route/posts/1/comments/2
will rewrite to /comments/2
from json-server.
Documentation should state there's only one level of nesting. Was trying to figure out why I can't go two levels down, found out through here. Would've been searching and hacking at json-server for a while if I hadn't seen this. You can use ?id=3, but it only works for get. Doesn't work for delete. Still json-server is great since you don't have to wait on a back-end to be fully complete to get started.
from json-server.
Love to see nested routes too. Even if it is going down 2 levels instead of 1, that would already be nice.
from json-server.
Ah. kk. Thanks for the update. I'll try that and close this if it works.
from json-server.
Worked like a charm. Would you be averse to a PR clarifying nested requests in README.md? I can have it submitted in a few minutes if you want it.
from json-server.
One more question: should /environments/12/reports/17
work? I thought it was earlier today, but now I'm running it and no dice. 404 error.
from json-server.
Hrmmm... looks like not. The nested routes are being handled statically. I'll take a look at it tonight and see if I can't add nested routing ad infinitum.
from json-server.
You're right, one level of nesting is supported.
Except if there's many people that need infinite nesting, it's not wished for the moment.
Mainly to limit code complexity and also based on the idea that if you know the report id, you can directly make a request to get it /reports/17
However a workaround could be to use filters, there's no limit to how much you can use:
/reports?id=17&environmentId=12&...
There's also an (old) article but still interesting about deep nesting:
http://weblog.jamisbuck.org/2007/2/5/nesting-resources
Do you need deep nesting because of a particular front-end library?
PS: thanks for the PR, I'll take a look at it as soon as I can :)
from json-server.
Thanks for the resource! I'm definitely gonna be going over that.
That being said, the API I'm mocking isn't in my control, I'm building a front-end for it, and I'm just trying to mimic it as closely as possible. It's not that difficult of an update, but if that feature isn't needed in the library as a whole, I'll just source my fork with the changes needed, no worries.
Let me know if there needs to be any changes in that PR, I'm happy to jump in and fix stuff.
from json-server.
Great.
Not sure if it's the best way to fill the gaps for your API, but using the project as a module you could do something like this:
var jsonServer = require('json-server')
var server = jsonServer.create()
server.use(jsonServer.defaults)
// Simple hack
server.use('/environments/:environmentId/reports/:reportId', function (req, res) {
res.redirect('/reports/' + req.params.reportId)
})
server.use(jsonServer.router('db.json'))
server.listen(3000)
I've not tested code, but it should work.
from json-server.
Ok, so here's what I got: https://github.com/therebelrobot/json-server/tree/feature/nested-calls
I rebuilt router.js
quite a bit, it was hard-coding the routes and duplicating code from show
to list
. I changed the name of list
to parse
, and routed all calls through it (it now checks all the ids and manages sub-resources). I split out the show
function to find
(which handles the actual object finding in lowdb) and present
(which handles sorting, paging, etc), making things a little more modular. Also added most internal objects that were in list
to req._internal
so it could be called by subsequent middleware.
I ran all of it through the test suite, also added 4 more tests for more nested calls. Everything is passing 100%.
Also added a coverage folder, and verified >99% code coverage of the new router.
I know you weren't looking at this kind of feature, but if you want a PR for it after all, lemme know :)
from json-server.
I would love to see this PR'd in as I really would love to see nested calls working, even if it was just an optional feature.
I wouldn't normally comment, but this feature would be super-useful.
from json-server.
@isaacrg As per @typicode's desire to keep things simple, I've forked the repo and republished it. Here it is: https://github.com/therebelrobot/json-mock
I'll close this issue now :)
from json-server.
The feross/standard
is a good point, FWIW the project now follows it. Maybe it can save you time.
from json-server.
I would also love to see this feature implemented. I'm currently trying to mock an API which is not in my control, and they are using multiple nested resources, even if the id of an element is already known. 😞
from json-server.
+1
from json-server.
+1 for this feature, @therebelrobot's fork is two years old and I would like much more to see it done here
from json-server.
@typicode hey man thanks for quick response. Have to say that after a while of using json-server I found out why you made it like this. It wants kind of different thinking that I was used to but now I find using nested routing as bad practice. Route rewrites can be used to map existing apis but from now on I am going to use top level routing only. My team is currently working on a service that will generate db.json for you, so that could be cool. BTW: thanks for your great work!
from json-server.
Hey it would be a nice feature to have a deep nested resource feature, the routes approach doesnt cut it. Are there plans to implement it? This would bring already a great service to the next level :)
from json-server.
+1
from json-server.
This would be nice to have
from json-server.
Related Issues (20)
- conditional operators doesn't work HOT 1
- I have a problem opening Json-server --watch.db.json HOT 1
- ?_page= or &_page= no longer working. HOT 1
- _sort=price&_order=asc no longer working HOT 2
- it doesn't work on xxx.js or remote url,though xxx.json is ok
- SyntaxError: Unexpected token '||=' HOT 1
- Please, bring back the `--delay` option HOT 2
- CORS Issue (Access-Control-Allow-Headers)
- _sort=id&_order=desc do not work (--version 1.0.0-beta.0) HOT 1
- paginate should return empty data when there have a bigger `_page`, but return last page data now
- `_like` doesn't work anymore? HOT 1
- _dependent value error
- Licensing HOT 1
- Compressed Uploads
- Getting nested resources doesn't work HOT 1
- cant find package json-server within node_modules HOT 3
- delay [ms] is not working for me...... HOT 2
- I can not not increased by 1 for id HOT 1
- Newly added data is not recognized when trying to update it seconds later HOT 1
- multiple files with beta 1.0.0v1 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-server.