I'm creating a mock application with JSON server as the backend and I'm wondering if it is possible to get the total number of records contained at an end point without loading all the records themselves? Assuming the db.json file looks like the JSON snippet below, how would I find out that the end point only has one record without fetching the record itself, provided it's possible?
{
"books": [{
"title": "The Da Vinci Code",
"rating": "0"}]
}
I'm creating a mock application with JSON server as the backend and I'm wondering if it is possible to get the total number of records contained at an end point without loading all the records themselves? Assuming the db.json file looks like the JSON snippet below, how would I find out that the end point only has one record without fetching the record itself, provided it's possible?
{
"books": [{
"title": "The Da Vinci Code",
"rating": "0"}]
}
Share
Improve this question
asked Feb 25, 2017 at 18:47
Happy KoalaHappy Koala
2491 gold badge4 silver badges10 bronze badges
2
- Im not sure if i understand it correctly , do you want to return number of properties ? If so , you can parse it and return number of keys. – Darlyn Commented Feb 25, 2017 at 18:54
- Nah, I'm just wondering if I could get the total number of records by using a query string like localhost:3001/books?total. There's nothing about it in the documentation, so it's probably not possible, in which case I'll just have to fetch all the records and find out how many of them there are that way. My reason for asking is because I want to use the pagination feature, but I don't see how it would be useful if I can't get the total number of records without fetching them all in the first place. – Happy Koala Commented Feb 25, 2017 at 19:04
5 Answers
Reset to default 7Whenever you fetch the data, json-server actually returns the total count by default (it has an x-total-count
property:
Example:
axios
.get("http://localhost:3001/users", {
params: {
_page: 1,
_limit: 10
}
})
.then(res => {
console.log(res.data); // access your data which is limited to "10" per page
console.log(res.headers["x-total-count"]); // length of your data without page limit
});
You can simply retrieve the X-Total-Count
header
This is a screen-shot of a response headers returned by JSON Server when enabling pagination i.e using the _page
parameter (e.g. localhost:3000/contacts?_page=1
)
You've three options. I'd recommend the 3rd one to you:
Return all the records and count them. This could be slow and send a lot of data over the wire but probably is the smallest code change for you. It also opens you up to attacks where people can hammer your server by requesting many records repeatedly.
Add a new endpoint. You could add a new endpoint that simply returns the count. It's simple but slightly annoying having a 2nd endpointime to document and maintain.
Modify the existing endpoint. Return something like
{
count: 157,
rows: [...data]
}
The benefit of 3 is its all in one endpoint. It also nears you toward a point where you can add a skip
and take
parameter in future to allow pagination of the resultant data.
let response = await fetch("http://localhost:3001/books?_page=1");
let total = response.headers.get('X-Total-Count');
You will write another end point that returns number of records. Usually also you may want end point for limit and offset to be used with pagination.