typicode / json-server

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

`_gte` and `_lte` doesn't work for dates #1528

Open matronator opened 6 months ago

matronator commented 6 months ago

DB file:

```json { "days": [ { "id": 1, "date": "2024-03-04T00:00:00", "hours": 6, "timeRanges": [ { "id": 1, "dayId": 1, "start": "2024-03-04T08:00:00", "end": "2024-03-04T12:00:00", "image": null }, { "id": 2, "dayId": 1, "start": "2024-03-04T13:00:00", "end": "2024-03-04T15:00:00", "image": null } ] }, { "id": 2, "date": "2024-03-05T00:00:00", "hours": 8, "timeRanges": [ { "id": 3, "dayId": 2, "start": "2024-03-05T08:00:00", "end": "2024-03-05T12:00:00", "image": null }, { "id": 4, "dayId": 2, "start": "2024-03-05T13:00:00", "end": "2024-03-05T17:00:00", "image": null } ] } ] } ```

URL: http://localhost:3000/days?date_gte=2024-03-01

Returns an empty array.

MadhuSaini22 commented 6 months ago

@matronator , it's because JSON Server doesn't natively support filtering based on date ranges out of the box.

JSON Server treats query parameters as string values, so when you use _gte, it's comparing the strings lexicographically rather than comparing them as dates. As a result, you're not getting the desired filtering effect.

matronator commented 6 months ago

Why does this say you can do it for dates?

Any idea how to make it work? Do I have to write some extension or save it as timestamps instead?

NarciSource commented 5 months ago

It seems to me that lexicographic comparison of strings is not working at all.

db.json { "days": [ { "id": 0, "data": "a" }, { "id": 1, "data": "b" }, { "id": 2, "data": "c" }, { "id": 3, "data": "d" }, { "id": 4, "data": "e" }, { "id": 5, "data": "f" }, { "id": 6, "data": "g" } ] }

URL: http://localhost:3000/days?data_gt=c

It is also an empty array.