pocketbase / dart-sdk

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

Empty/non-set relations should not return as empty string in the result Json #18

Closed TomTom101 closed 1 year ago

TomTom101 commented 1 year ago

When a record with a Relation field (e.g. named "relation") has no relation set, the resulting Json returns an empty string:

{
    "id": "2euaei6s2bs9kfx",
    "created": "2022-11-16 21:36:26.035Z",
    "updated": "2022-11-16 21:36:26.035Z",
    "collectionId": "wx8ugpt1m8dk2o5",
    "collectionName": "items",
    "expand": {},
    "name": "Some record name",
    "relation": ""
}

This is a problem when working with freezed and a model Item that expects a model Relation for the "relation" field.

@freezed
class Item with _$Item {
  const factory Item({
    String? id,
    required String name,
    Relation? relation,
  }) = _Item;

  factory Item.fromJson(Map<String, Object?> json) => _$ItemFromJson(json);
}

A null value would be more appropriate and not pose a problem when working with freezed.

Side note: Before that works, I additionally need to copy the full record data from the expand['relation'] field to relation since the latter would only contain ids, which would neither satisfy the Relation model. So I am questioning whether it makes sense to replace the (list of) id(s) in a relational field when expand=relation is given with the actual data set:

So instead of this (some default field omitted for brevity):

{
    "id": "bvo49tlzn8na3r6",
    "collectionName": "items",
    "expand": {
        "relation": {
            "id": "a74xecr0dl0mgh8",
            "collectionName": "relations",
            "expand": {},
            "name": "A relational record"
        }
    },
    "name": "An Item",
    "relation": "a74xecr0dl0mgh8"
}

have this instead

{
    "id": "bvo49tlzn8na3r6",
    "collectionName": "items",
    "name": "An Item",
    "relation": {
            "id": "a74xecr0dl0mgh8",
            "collectionName": "relations",
            "expand": {},
            "name": "A relational record"
        }
}
ganigeorgiev commented 1 year ago

PocketBase doesn't have null values (with the exception of the json type) and uses zero defaults.

Relations are expanded only under the expand property and the original root-level keys are left untouched.

I'm not sure that I understand your use case, but there are no plans to change the resulting format since it will be too confusing (expand is not always available because it depends on the View API rule of the related collection and if we reuse the same key as the id then you may end up in situation where a single prop will have 2 different types - string and object/array).

TomTom101 commented 1 year ago

Thanks Gani!

What I'm after is a json null for unset relational fields. So "relation": null instead of "relation": "":

{
    "id": "2euaei6s2bs9kfx",
    "created": "2022-11-16 21:36:26.035Z",
    "updated": "2022-11-16 21:36:26.035Z",
    "collectionId": "wx8ugpt1m8dk2o5",
    "collectionName": "items",
    "expand": {},
    "name": "Some record name",
    "relation": null
}
ganigeorgiev commented 1 year ago

@TomTom101 As mentioned previously, we don't have null and this is deliberate (see the table of the supported values in https://github.com/pocketbase/pocketbase/releases/tag/v0.3.0; there will be more documentation on this in the new docs with the v0.8.0 release later this week).

TomTom101 commented 1 year ago

Now I understand "with the exception of the json type" ;) Thanks!