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

Add `_is_null` and `_is_not_null` operators

Open zlargon opened this issue 1 year ago • 1 comments

This PR is related to #682 and #823.

  • Add _is_null and _is_not_null operators.
  • Add test cases.
  • Update README.

example

db.todos = [
  { id: 1, completeDate: null },
  { id: 2, completeDate: null },
  { id: 3, completeDate: '2022-11-20T13:02:47.210Z' },
  { id: 4, completeDate: '2023-12-05T17:58:17.420Z' },
]

attr_is_null

// GET /todos?completeDate_is_null
[
  { id: 1, completeDate: null },
  { id: 2, completeDate: null }
]

attr_is_not_null

// GET /todos?completeDate_is_not_null
[
  { id: 3, completeDate: '2022-11-20T13:02:47.210Z' },
  { id: 4, completeDate: '2023-12-05T17:58:17.420Z' },
]

zlargon avatar Dec 05 '23 11:12 zlargon

Hi @typicode,

I want to express my gratitude for the valuable tool that json-server is, enabling us to swiftly set up mock servers for testing purposes. Your effort in providing this as an open-source project is highly appreciated.

While working with json-server, I noticed a limitation in its current functionality related to querying null and non-null values. I believe incorporating support for such queries would be a beneficial addition. This idea has also been raised in issues #682 and #823.

I observed attempts by others to implement this as a custom filter, but unfortunately, it didn't yield the desired results. Consequently, I took the initiative to contribute a feature that allows querying null values.

Initially, I envisioned designing the single is_null operator as follows:

URL description
GET /todos?completeDate_is_null return null values (default is true)
GET /todos?completeDate_is_null=true return null values
GET /todos?completeDate_is_null=false return non-null values

However, during the implementation, it seems that this design required some code refactoring. Consequently, I opted for a solution involving separate is_null and is_not_null operators. I would greatly appreciate any advice or guidance you could provide on this approach.

Thank you for your time and consideration.

Best, Leon

zlargon avatar Dec 07 '23 04:12 zlargon