feat(entity): support polymorphic-table cascades via additionalFieldFilters#615
Draft
szdziedzic wants to merge 1 commit into
Draft
feat(entity): support polymorphic-table cascades via additionalFieldFilters#615szdziedzic wants to merge 1 commit into
szdziedzic wants to merge 1 commit into
Conversation
Author
This stack of pull requests is managed by Graphite. Learn more about stacking. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #615 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 110 110
Lines 17694 17901 +207
Branches 911 1541 +630
==========================================
+ Hits 17694 17901 +207
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…ilters
## Summary
Adds an optional `additionalFieldFilters` field on `EntityAssociationDefinition` that AND's extra field-equality conditions onto the cascade load when the source entity is deleted. This lets polymorphic child tables — multiple entity classes backed by one DB table and distinguished by scope-specific columns — register each class with a scope-disambiguating filter so cascade loads through one class never see rows that belong to a sibling class (which would otherwise trip the wrong-scope constructor invariant and abort the entire cascade).
## Motivation
For polymorphic tables where multiple entity classes share a table and each enforces a scope-specific constructor invariant (e.g. `TurtleJobRunEntity` requires `appId !== null`, `AccountTurtleJobRunEntity` requires `accountId !== null`), the parent-delete cascade currently loads every row matching the foreign key regardless of scope. Loading mixed-scope rows through one class throws on the wrong-scope rows and aborts the cascade. Consumers had to work around this with helper functions; this PR makes the scope filter expressible in the association config itself.
## Mechanism
- New `FieldEqualityCondition` types live in the base `@expo/entity` package (re-exported from `@expo/entity-database-adapter-knex` for back-compat).
- New `fetchManyByFieldEqualityConjunctionAsync` on the base `EntityDatabaseAdapter` with a default impl that uses single-value operands for the SQL WHERE and applies multi-value operands as an in-memory filter. The knex adapter overrides it for native SQL conjunction filtering (with proper `IS NULL` semantics).
- New `loadManyByFieldEqualityConjunctionAsync` on both `AuthorizationResultBasedEntityLoader` and `EnforcingEntityLoader`.
- The cascade in `AuthorizationResultBasedEntityMutator.processEntityDeletionForInboundEdgesAsync` builds `[{ primary }, ...association.additionalFieldFilters]` and calls the new loader method. All four `EntityEdgeDeletionBehavior` branches benefit automatically.
- `additionalFieldFilters` is typed via a new `TSourceFields` generic threaded through `EntityFieldDefinition`, its options, and `EntityConfiguration.schema`, so filter field names are type-checked against the source entity's `TFields` at the schema literal.
- No behavior change for associations that don't set `additionalFieldFilters` — the cascade load is equivalent to the previous single-equality load.
## Related
- Original consumer motivation: expo/universe#27183
- Prototype helper in consumer code: expo/universe#27406
## Test plan
- [x] `yarn tsc`
- [x] `yarn lint`
- [x] `yarn test` — 735 unit tests, including 4 new in `EntityEdgesAdditionalFieldFilters-test.ts` covering: no-filter regression, polymorphic scope filters, multi-row cascade, no-op filter
- [x] `yarn integration` — 123 integration tests, including 2 new in `EntityEdgesAdditionalFieldFiltersIntegration-test.ts` exercising the native SQL conjunction path against Postgres
ecd7856 to
5827cce
Compare
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.

Summary
Adds an optional
additionalFieldFiltersfield onEntityAssociationDefinitionthat AND's extra field-equality conditions onto the cascade load when the source entity is deleted. This lets polymorphic child tables — multiple entity classes backed by one DB table and distinguished by scope-specific columns — register each class with a scope-disambiguating filter so cascade loads through one class never see rows that belong to a sibling class (which would otherwise trip the wrong-scope constructor invariant and abort the entire cascade).Motivation
For polymorphic tables where multiple entity classes share a table and each enforces a scope-specific constructor invariant (e.g.
TurtleJobRunEntityrequiresappId !== null,AccountTurtleJobRunEntityrequiresaccountId !== null), the parent-delete cascade currently loads every row matching the foreign key regardless of scope. Loading mixed-scope rows through one class throws on the wrong-scope rows and aborts the cascade. Consumers had to work around this with helper functions; this PR makes the scope filter expressible in the association config itself.Mechanism
FieldEqualityConditiontypes live in the base@expo/entitypackage (re-exported from@expo/entity-database-adapter-knexfor back-compat).fetchManyByFieldEqualityConjunctionAsyncon the baseEntityDatabaseAdapterwith a default impl that uses single-value operands for the SQL WHERE and applies multi-value operands as an in-memory filter. The knex adapter overrides it for native SQL conjunction filtering (with properIS NULLsemantics).loadManyByFieldEqualityConjunctionAsyncon bothAuthorizationResultBasedEntityLoaderandEnforcingEntityLoader.AuthorizationResultBasedEntityMutator.processEntityDeletionForInboundEdgesAsyncbuilds[{ primary }, ...association.additionalFieldFilters]and calls the new loader method. All fourEntityEdgeDeletionBehaviorbranches (CASCADE_DELETE,CASCADE_DELETE_INVALIDATE_CACHE_ONLY,SET_NULL,SET_NULL_INVALIDATE_CACHE_ONLY) benefit automatically.additionalFieldFiltersis typed via a newTSourceFieldsgeneric threaded throughEntityFieldDefinition, its options, andEntityConfiguration.schema, so filter field names are type-checked against the source entity'sTFieldsat the schema literal.additionalFieldFilters— the cascade load is equivalent to the previous single-equality load.Example
Related
Test plan
Tests