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

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

Open matronator opened this issue 2 years ago • 3 comments

DB file:

{
    "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.

matronator avatar Mar 10 '24 17:03 matronator

@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.

MadhuSaini22 avatar Mar 15 '24 15:03 MadhuSaini22

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?

matronator avatar Mar 15 '24 19:03 matronator

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.

NarciSource avatar Apr 20 '24 08:04 NarciSource