Skip to content

Conversation

@g12i
Copy link

@g12i g12i commented Jul 22, 2025

This PR adds support for mapping TExpand field with actual relationships between types.

For example, given 3 collections:

Author -> (has many) -> Courses -> (has many) Chapters -> (has many) (sub) Chapters

image

It would generate following schema:

export type AuthorsResponse<Texpand = {
	courses_via_author?: CoursesRecord[]
}> = Required<AuthorsRecord> & BaseSystemFields<Texpand>
export type ChaptersResponse<Texpand = {
	course?: CoursesRecord,
	parent?: ChaptersRecord,
	chapters_via_parent?: ChaptersRecord[],
}> = Required<ChaptersRecord> & BaseSystemFields<Texpand>
export type CoursesResponse<Texpand = {
	author?: AuthorsRecord,
	chapters_via_course?: ChaptersRecord[]
}> = Required<CoursesRecord> & BaseSystemFields<Texpand>

@patmood
Copy link
Owner

patmood commented Jul 24, 2025

Thanks for the contribution and putting the work into this. It's been attempted a few times before but was ultimately too complicated.

Does it handle nested fields? They can be up to 6 levels deep.

@g12i
Copy link
Author

g12i commented Jul 24, 2025

Thanks for the contribution and putting the work into this. It's been attempted a few times before but was ultimately too complicated.

Does it handle nested fields? They can be up to 6 levels deep.

I'm afraid not @patmood - as it types it as ModelRecord and not ModelResponse.

@g12i
Copy link
Author

g12i commented Jul 26, 2025

Hey @patmood, so I'm testing that IRL, and I changed it to expand with ModelResponse. Now it is indeed recursive and works for nested fields.

image

Of course, as we don't know what the user queried for, it shows all possible outcomes.

I tried to contribute to SDK with that feature, but it wasn't approved.

pocketbase/js-sdk#351

But, in theory we could try to implement this at generated types level.

@g12i
Copy link
Author

g12i commented Aug 7, 2025

@patmood any thoughts?

@patmood
Copy link
Owner

patmood commented Aug 12, 2025 via email

@patmood patmood mentioned this pull request Oct 1, 2025
@patmood
Copy link
Owner

patmood commented Oct 7, 2025

I haven't forgotten about this. I think the pb_schema in the repo must be outdated and is missing some relation metadata, which is why the output is unchanged for the example types. I will update it and finish reviewing.

Copy link
Owner

@patmood patmood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the slow review @g12i - This is really good stuff though.

To get this merged I this merged I think we need to:

  • get to the bottom of collectionId in the schema (were you using an old PB version?)
  • after fixing that, generate new example types
  • fix merge conflicts with latest changes

Are you still interested in getting this merged?

system: boolean
required: boolean
unique: boolean
options?: { collectionId?: string }
Copy link
Owner

@patmood patmood Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not exist in the latest version of pocketbase. collectionId is just part of the FieldSchema. So you know where you go this from? Was it a change in a recent version of pocketbase?

Suggested change
options?: { collectionId?: string }
// older pocketbase versions nested data under an `options` field
options?: { collectionId?: string }
collectionId?: string

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @patmood, I need to check. But it might take a while, as I've been doing this on another machine. Will come back to you!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool I think we can support both with a comment that the old structure is only up X version. That way it's backwards compatible.

relationNodes.forEach((childNode) => {
childNode.fields.forEach((field) => {
const parentId =
field.type === "relation" ? field.options?.collectionId : undefined
Copy link
Owner

@patmood patmood Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was why I couldn't get it working....

Suggested change
field.type === "relation" ? field.options?.collectionId : undefined
field.options?.collectionId || field.collectionId

@patmood
Copy link
Owner

patmood commented Dec 26, 2025

Hey @g12i - Did you want to make those updates? If not, I have some time and can merge this into a different branch and take it over.

@g12i
Copy link
Author

g12i commented Jan 7, 2026

Hey @patmood, sorry I was on holiday. Now I'm back and kind of busy with day-to-day, so feel free to take it over

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants