bitovi / querystring-parser

MIT License
8 stars 5 forks source link

Export getSequelizeFindOptions #80

Closed roy-coder closed 7 months ago

roy-coder commented 7 months ago

This PR exposes existing functionality under getSequelizeFindOptions.

With this, we will be able make further changes for hatchedKoa.parse.findAndCountAll to have something like:

Input:

"include=user&filter[name]=laundry&fields[Todo]=id,name,dueDate&fields[User]=name&page[number]=3&page[size]=5"

Output:

{
  "filter": { "=": ["#name", "laundry"] },
  "fields": { "Todo": ["id", "name", "dueDate"], "User": ["name"] },
  "page": { "number": 3, "size": 5 },
  "sort": [],
  "include": ["user"],
  "errors": { "filter": [], "fields": [], "page": [], "sort": [], "include": [] }
}

Then hatchedKoa.recordQuery.findAndCountAll will take our ORM-agnostic query, query Sequelize and return plain objects:

Input:

{
  "filter": { "=": ["#name", "laundry"] },
  "fields": { "Todo": ["id", "name", "dueDate"], "User": ["name"] },
  "page": { "number": 3, "size": 5 },
  "sort": [],
  "include": ["user"],
  "errors": { "filter": [], "fields": [], "page": [], "sort": [], "include": [] }
}

Output:

{
  "count": 1,
  "rows": [
    {
      "id": 1,
      "name": "laundry",
      "dueDate": "2024-01-01",
      "user": {
        "name": "John"
      }
    }
  ]
}

And then hatchedKoa.serialize.findAndCountAll will covert it to proper JSONAPI response:

Input:

{
  "count": 1,
  "rows": [
    {
      "id": 1,
      "name": "laundry",
      "dueDate": "2024-01-01",
      "user": {
        "name": "John"
      }
    }
  ]
}

Output:

{
  "jsonapi": { "version": "1.0" },
  "meta": { "unpaginatedCount": 1 },
  "data": [ ... ],
  "included": [ ... ]
}

Anyone who wants to keep altering the Sequelize queries will have to switch from hatchedKoa.model.Todo to hatchedKoa.orm.models.Todo.

roy-coder commented 7 months ago

Decided not to take this path.