pocketbase / dart-sdk

PocketBase Dart SDK
https://pub.dev/packages/pocketbase
MIT License
511 stars 51 forks source link

How to filter the expanded items only #50

Closed devmuaz closed 1 year ago

devmuaz commented 1 year ago

Hello,

I'm currently having an issue trying to filter out the expanded items to a certain condition, as as shown below:

final data = await pb.collection('categories').getList(expand: 'localization');

The resulted data would be like this:

{
  "page": 1,
  "perPage": 30,
  "totalItems": 1,
  "totalPages": 1,
  "items": [
    {
      "id": "tlcxv2utfpwj1fj",
      "created": "2023-09-15 20:08:53.062Z",
      "updated": "2023-09-15 20:08:53.062Z",
      "collectionId": "2rfgsuc64fouiv1",
      "collectionName": "categories",
      "expand": {
        "localization": [
          {
            "id": "2jlv6efy9s3f5l9",
            "created": "2023-09-15 20:08:34.425Z",
            "updated": "2023-09-15 21:53:48.724Z",
            "collectionId": "bv3sssp0ule357t",
            "collectionName": "categoriesLocalization",
            "expand": {},
            "locale": "ar",
            "subtitle": "تسلا ومرسيديس",
            "title": "سيارات"
          },
          {
            "id": "2ihfygz9uk8ac4d",
            "created": "2023-09-15 20:08:20.141Z",
            "updated": "2023-09-15 21:53:58.972Z",
            "collectionId": "bv3sssp0ule357t",
            "collectionName": "categoriesLocalization",
            "expand": {},
            "locale": "en",
            "subtitle": "Tesla and Mercedes",
            "title": "Cars"
          }
        ]
      },
      "image": "devmuaz_4_square_dPHZafvg8Q.jpg",
      "localization": [
        "2jlv6efy9s3f5l9",
        "2ihfygz9uk8ac4d"
      ]
    }
  ]
}

What I need is a way to filter the expand.localization to be equal to lets say ar only which also minimizes the response size. It would be something like this:

{
  "page": 1,
  "perPage": 30,
  "totalItems": 1,
  "totalPages": 1,
  "items": [
    {
      "id": "tlcxv2utfpwj1fj",
      "created": "2023-09-15 20:08:53.062Z",
      "updated": "2023-09-15 20:08:53.062Z",
      "collectionId": "2rfgsuc64fouiv1",
      "collectionName": "categories",
      "expand": {
        "localization": [
          {
            "id": "2jlv6efy9s3f5l9",
            "created": "2023-09-15 20:08:34.425Z",
            "updated": "2023-09-15 21:53:48.724Z",
            "collectionId": "bv3sssp0ule357t",
            "collectionName": "categoriesLocalization",
            "expand": {},
            "locale": "ar",
            "subtitle": "تسلا ومرسيديس",
            "title": "سيارات"
          }
        ]
      },
      "image": "devmuaz_4_square_dPHZafvg8Q.jpg",
      "localization": [
        "2jlv6efy9s3f5l9",
        "2ihfygz9uk8ac4d"
      ]
    }
  ]
}
ganigeorgiev commented 1 year ago

expand only expand what is in the record relation field and you can't perform further filtering.

The filter query param if set apply only on the main collection records (in your case categories), aka. even if you have: filter: "localization.locale ?= 'ar'", this will return "categories" records that have the matching localization.

If you need filtering or pagination for the "categoriesLocalization" you can send separate getList request(s) to the "categoriesLocalization" collection and manually perform the "categories" grouping (you can emulate a "IN" condition by extracting the categories localization ids and apply a "categoriesLocalization" filter like id = 'id1' || id = 'id2' || ...).

devmuaz commented 1 year ago

I thought there was something I missed out, and actually no I don't need pagination or something like that, but it would be much convenient to make such functionality available.

The localization expandable field should be minimized because it would become much bigger if I have for instance more than 5 locales and most of them are useless except the one that's needed, so minimizing and filtering these data (relational data) would really makes difference!