-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Indirect expand #312
Comments
A POC was already implemented while working on #376 so here are some details of the current implementation:
Nested indirect expansions will be supported - both for regular relations and sub indirect relations. In the above example, if There are some caveats:
Nothing is set in stone yet, so suggestions and feedback on the format are welcomed. |
This looks great, really like the syntax. I don't have a lot of feedback or concerns, I have one questions though. In one of my
My collections are setup like this:
To
I think I would do something like this:
And then I (think) I would get something like this:
Nearly there, but I am still missing the expanded user relation. Am I interpreting the syntax correctly if that would be resolved with:
If so, that would be really cool 😎 |
Yes, that's correct. The only difference from your sample output is that you'll have an additional [
{
"id": "event1",
"created": "2022-09-11 00:00:00.123",
"updated": "2022-09-11 00:00:00.123",
"title": "Example event"
"@expand": {
"responses(event)": [
{
"id": "response1",
"created": "2022-09-11 00:00:00.123",
"updated": "2022-09-11 00:00:00.123",
"event": "event1",
"user": "user123"
"@expand": {
"user": {
"id": "user123",
"created": "2022-09-11 00:00:00.123",
"updated": "2022-09-11 00:00:00.123",
"name": "...",
}
}
},
{
"id": "response2",
"created": "2022-09-11 00:00:00.123",
"updated": "2022-09-11 00:00:00.123",
"event": "event1",
"user": "user456"
"@expand": {
"user": {
"id": "user456",
"created": "2022-09-11 00:00:00.123",
"updated": "2022-09-11 00:00:00.123",
"name": "...",
}
}
}
]
}
}
...
] |
This is now part of the v0.8.0-rc1 pre-release. Please note that the documentation is currently missing for this feature since I'm still not sure how to document it clearly, but it will it be added before the final v0.8.0 release. |
I have to say that indirect expand together with the generic methods make the code very clean. I now do something along the lines of: const queryParams: Record<string, string | null> = {
sort: sort,
filter: filters.join(' && '),
expand: 'venue.city, artists, responses(event).user'
};
const events = await locals.pb.collection('events').getList<Event>(page, perPage, queryParams);
return {
events: structuredClone(events),
sort: sort
}; where I have export interface Event extends BaseRecord {
name: string;
venue: RecordIdString;
artists: RecordIdString[];
type: 'concert' | 'music-festival' | 'movie';
cancelled: boolean;
starts: string;
ends: string;
url?: string;
expand: {
venue: Venue;
artists: Artist[];
'responses(event)': Response[];
};
} which allows me to do this in my Svelte component: ...
<tbody>
{#each events.items as event}
<tr>
<td><a href="/events/{event.id}">{event.name}</a></td>
<td>{formatDate(event.starts)}</td>
<td>{event.expand.venue.name}</td>
<td>
{#if event.expand['responses(event)'] !== undefined}
{#each event.expand['responses(event)'] as response}
<img
use:tooltip={{ content: response.expand.user.name }}
src={response.expand.user.avatar}
alt="avatar for {response.expand.user.name}"
/>
{/each}
{/if}
</td>
<td />
</tr>
{/each}
</tbody>
... which I think looks very clean! thanks a lot for implementing this! |
Just for info if someone stumble on this issue - this is documented in the new docs with v0.8.0 in https://pocketbase.io/docs/expanding-relations/ |
Reply: #312 (comment)
Are there plans to change this in the future, as the "initially" implied? |
This was discussed in #289 (comment).
We could add support for a new query parameter
?indirectExpand
that will allow eager loading non-directly related collection records within a single client request.It should work similarly to the existing
?expand
parameter but with additional 2 extra prefixed keys that points to the related collection and field (an additional finalizer prop key may also be needed in case of duplicated indirect expand fields from different collections).The format in the related discussion is not final and may change after we start working on the issue.
The text was updated successfully, but these errors were encountered: