pocketbase/js-sdk

Typescript and relations

Closed this issue · 2 comments

Hi, while making a frontend for my pocketbase i have come into some problems using typescript together with expanded queries in relations.

I have a type like this:

export default class Declaration extends PBModel{
    date?:Date;
    hours?:number;
    type?:DeclarationType;
    assignment?:Assignment;
    user?:User
}

where PB Model is another class that stores the default collection values (id, created, updated)

no my problem lies in the conversion of a queries data into one of these objects.

It fills in all the values fine, however, for all the user and assignment values (which are other objects), because they are hidden behind the value "expand" they dont get set when using a query like this

pb.collection("declarations").getFullList<Declaration>({expand:"user")

so when trying to reach declaration.user, i just get the ID of the user thats actually in the expand.user field.

is there anyway to have the user properly get set to the expanded user instead? or will i always have to manually define an extra field on my models called "expand" with a type "any" or "{assignment?:Assignment, user?:User}"?

The JS SDK returns the response json as it is recceived from the server, meaning that the expanded user data will be under the expand.user prop.

There is no other option, nor I think there is need to complicate it. If you want to apply custom transformations then you can create your own helpers.

Note also that date field values are strings so the Date type used in the above interface is incorrect.

The JS SDK returns the response json as it is recceived from the server, meaning that the expanded user data will be under the expand.user prop.

There is no other option, nor I think there is need to complicate it. If you want to apply custom transformations then you can create your own helpers.

Note also that date field values are strings so the Date type used in the above interface is incorrect.

Ah noted, i ended up solving it by adjusting the base type with a nullable field "expands" that is of type "this" so right now if i want to get the relations all my classes have a value called expands which so i still get static typing, but i just have to be careful not to access the non relational fields, meaning all my classes values have to be nullable (which they already were)

also good note on the date thing, i figured something was wrong when i tried to access the date earlier, ill replace all my dates with getters that return a new date object with the string as a param.