Add withPreloadScopes to constrain included relations - #7
Merged
Conversation
jsonApi.query() preloads the include tree for you, so a developer cannot reach those relation queries to scope them. withPreloadScopes(), a query builder macro, closes that: a map keyed by relation name constrains each included relation's preload query, at any depth, using callbacks that are the exact shape of Lucid's withScopes(), so a related model's own named scopes are reused rather than redefined. It composes after jsonApi.query() with Lucid's own withScopes() for the root. The map is read when the preload runs (execution), so chain order does not matter. applyIncludes keeps its old two-argument form working; the new model and preload-scope arguments are optional. Docs and a real end-to-end example test are included.
The scope map is keyed by the model's relation names, and each callback's scopes argument is the related model's scope bag, matching withScopes(): a wrong relation name or an undefined scope is a compile error. Deeper includes are constrained by nesting a preload of their own, typed to the next model down. This replaces the flat, any-depth-by-name shape (which could not be typed, and could apply a scope to a same-named relation on another branch) with a structural tree walked alongside the include tree. applyIncludes stays backward compatible.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
withPreloadScopes(), a query-builder macro for constraining the preload queries of included relations, fully typed per relation.jsonApi.query()builds the?include=preload tree for you, so there is no call site where a developer can scope those relation queries. A scope applied to the root with Lucid'swithScopes()never reaches the included rows, which for a visibility rule means hidden rows leak through the include.withPreloadScopes()closes that gap:Typed like
withScopesThe map is keyed by the model's relation names, and each callback's
scopesargument is the related model's scope bag (ExtractScopes<Related>), so scope names autocomplete and a wrong relation name or an undefined scope is a compile error, exactly the safetywithScopes()gives. Deeper includes are constrained by nesting, typed to the next model down:An entry is either a bare callback (scope that relation) or
{ scope?, preload? }to also constrain deeper includes. Scopes apply along the path you write, so a relation on one branch never leaks to a same-named relation on another.Design
withScopes()callback, so the visibility rule lives once on the model.jsonApi.query(). The scope tree is read when Lucid loads the relation (execution time), verified against Lucid's preloader, so chain order does not matter.withScopes/preloadboth return the same builder instance, so the builder-keyedWeakMapthe macro uses is stable.Implementation
withPreloadScopesregistered as aModelQueryBuildermacro in the providerboot(). Two augmentations: the contract carries the typedPreloadScopeMap<Model>for callers; the concrete class carries the loose runtime tree forMacroable.macro's keyof.model: RelatedModel), thenExtractScopes<Related>.WeakMapkeyed by the builder holds the scope tree;applyIncludeswalks it alongside the include tree, applying each relation's scope viawithScopesand descending throughpreload.applyIncludesstays backward compatible: theModeland scope-tree arguments are optional, so the existing low-levelapplyIncludes(query, tree)form is unchanged.Tests
{ scope, preload }descent, object-entry-without-scope, read at preload time (added afterapplyIncludesand still applied), and the tree merge.publishedscope onCommentand a/scoped-articles/:idendpoint usingwithPreloadScopes; the test asserts the scoped include returns only the published comment (1) while the unscoped endpoint returns both (2), proving real SQL filtering through a real HTTP request. The example's typecheck also exercises the typed map (scopes.published()resolved againstExtractScopes<Comment>).Notes
docs/reading-data.mdand a pointer indocs/reference.md.