feat: Rails-style scopes#1892
Open
stephenh wants to merge 43 commits into
Open
Conversation
joist-codegen
joist-core
joist-graphql-codegen
joist-graphql-resolver-utils
joist-knex
joist-migration-utils
joist-orm
joist-test-utils
joist-utils
commit: |
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.
Add Scope Queries
Fixes #573
Summary
Adds Rails-style, typed scope queries for reusable Joist
em.findfilters:Scopes are static entity properties, but execution still requires an explicit
EntityManagervia terminal methods like.find(em),.findOne(em),.findCount(em), and.findIds(em).Biggest FYI Achieving the chained
Author.adult.popularsyntax in TypeScript currently requires runningjoist-codegenafter each change to a scope definition, so that we can update an always-codegenedAuthorScopestype to help drive the DSL's type checking.Implementation
FilterAndSettings<T>/em.findpath.scope.fn, named-scope chaining, and builder chaining with.where,.orderBy,.limit,.offset, and.softDeletes:static adult = scope({ age: { gte: 18 } })static popular = scope((a) => a.isPopular.eq(true))static named = scope.fn((prefix: string) => (a) => a.firstName.like(${prefix}%))authorScopefunction constants in each<Entity>Codegen.ts, mirroring existing<entity>Configconstants.<Entity>Scopesand<Entity>Scopetypes from a syntax-only codegen pre-scan of user-owned entity files; users need to runjoist-codegenafter each scope declaration change so the generated chainable scope types stay current.<Entity>Scopesinterface and<Entity>Scopealias.Example
After codegen refreshes
AuthorScopes, those declarations become chainable:Tests
packages/tests/integration/src/EntityManager.scopes.test.tsfor runtime behavior and compile-time type assertions.toFindArgsoutput.Verification
Verified:
Review Notes
<Entity>Scopetype, or a function type returning<Entity>Scope, and an initializer rooted atscopeor the entity class.Non-Scope Diff Notes
The
main..@diff also currently contains scope design markdown files, an added batched-paginated-find blog post, and an unrelated GraphQL resolver hint test/behavior change. Consider splitting those out if this PR should stay scope-only.