Skip to content

feat: Add HasMany and BelongsTo Eager Loading - #6

Merged
ThomasHartDev merged 1 commit into
mainfrom
thomas/feat/relations
Sep 11, 2026
Merged

ThomasHartDev merged 1 commit into
mainfrom
thomas/feat/relations

Conversation

@ThomasHartDev

Copy link
Copy Markdown
Owner

Relations were the missing piece after the query builder and migrations. hasMany and belongsTo declare the FK once. relatedQuery compiles a single IN list with unique, non-null keys, and nestHasMany / nestBelongsTo attach the graph in memory.

A loop of per-parent SELECTs is N queries for N parents. The eager path is one extra query, including parents that have no children. belongsTo reuses one parent object per id (identity map). Empty or all-null key sets skip the related query so we never hit the database for nothing.

Closes #5

Declare relations, batch related rows with a unique IN list, and nest
them in memory so N parents cost one extra query instead of N.
@ThomasHartDev
ThomasHartDev merged commit 68544cb into main Sep 11, 2026
1 check passed
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.

Eager hasMany/belongsTo loading to stop N+1 queries

1 participant