Notes about the upcoming back relation support #4417
Closed
ganigeorgiev
announced in
Announcements
Replies: 4 comments 10 replies
-
Regarding the back relation syntax... what about // ex. comments_from_post
yourCollection_from_yourRelField.* Although I don't know how common it is to use |
Beta Was this translation helpful? Give feedback.
3 replies
-
Hi @ganigeorgiev , what about OData V4 $filter and $expand query parameters? Can you implement something similar for PB? Thank you |
Beta Was this translation helpful? Give feedback.
1 reply
-
What if you named a collection
and another
you would end up with a query
I think i would much prefer |
Beta Was this translation helpful? Give feedback.
5 replies
This comment was marked as off-topic.
This comment was marked as off-topic.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Below are some notes regarding the upcoming back relation
filter
andsort
support (for both single and multiplerelation
fields)._via_
syntaxWe already have syntax for referencing back relations as part of the
expand
parameter:We could technically continue to use the same syntax for the
filter
andsort
but several users complaint that the parenthesis makes it difficult to serialize the api responses since most of the programming languages doesn't allow(
or)
characters in the variable/property name and it requires manually specifying an alias or brackets access notation (eg.obj["yourCollection(yourField)"].someField.*
).Therefore instead of parenthesis I've decided to change it to use
_via_
as a fixed string separator:(I'm still not 100% sure about
_via_
but I decided on it instead of_by_
to avoid the stuttering with common relation fields like "createdBy")This both satisfy the requirement the key to be valid variable/property name and at least to me reads just as fine as the parenthesis.
The drawback from the fixed string separator is that we'll no longer allow collections and fields with
_via_
in their name to avoid ambiguities and collisions (I think this would be rare).For the
expand
parameter the old parenthesis syntax will continue to work alongside the new one in order to minimize the breaking changes, but if you use the old syntax there will be a deprecated warning message printed to the stderr.In general back relations references will be treated like dynamic relation fields:
posts_via_createdBy
)@request.auth.yourCollection_via_yourRelField.*
expressions (eg.@request.auth.posts_via_createdBy.*
)@request.data.yourCollection_via_yourRelField.*
expressions (eg.@request.data.posts_via_createdBy.*
)@collection.baseCollection.yourCollection_via_yourRelField.*
expressions (eg.@collection.usersView.users_via_owner.*
)Single vs Multiple relation fields resolution
Similar to how back relation references in
expand
works, when filtering back relations we can't rely only on theMaxSelect
relation field option to determine whether thefilter
expression will resolve to a single field or multiple because it is possible the main record to have multiple single back relation references.Therefore a back relation reference is considered "single" only if it has a unique index for the relation field (to avoid ambiguities partial index constraints are ignored).
Or in other words,
comments_via_post.*
will resolve to a single record constraint if there is a unique index in thecomments
collection for thepost
field.Additionally, as a side effect of the recent changes, the existing limitation for
expand
not working with multiple back relation fields is lifted and multiple back relation expands will be supported with the next release.Security and performance
In some rare cases, especially now with the back relations, security researches could argue that there is a risk of information disclosure because someone could try to use the
filter
to find out indirectly whether a back relation record exists or not.If we ignore the non-descriptive API rule errors and blank responses, to some extent this is possible, but keep in mind that users still need first to comply with the List/Search API rule of the main collection and even then they will not be able to retrieve the complete relation record if they don't satisfy the specific API rule of the related collection.
If developers strictly don't want to allow filtering relation references they can always enforce manual checks using something like
@request.query.filter !~ "someRelField."
or via the app hooks (with the refactoring this may be further simplified with individual "Filterable" field option).Therefore in my opinion such cases are negligible for the sake of better performance and the cleaner developer APIs.
Regarding query performance - I still have to do some manual performance tests on larger datasets before finalizing the implementation, but the expected query performance should be similar to the regular relations.
For the Admin UI, because generating all autocomplete back relation fields and their various nesting options could block the interface, the autocomplete generator was migrated to execute separately in a worker. I've tested the UI with ~30k autocomplete fields (combined with
@request.*
and@collection.*
completions) and locally seems OK (it can be further optimized but the Admin UI is not that critical and it will be refactored anyway).That's it. I'm planning to release PocketBase v0.22.0 sometime later next week or the week following it.
In the meantime feel free to comment if you have something else in mind in regards to the back relations.
If you want to test it, I've pushed the latest changes in the
develop
branch.Beta Was this translation helpful? Give feedback.
All reactions