[upstream #4965] feat(core): Auto-initialise customFields for entities that support them - #22
Open
ayim wants to merge 30 commits into
Open
[upstream #4965] feat(core): Auto-initialise customFields for entities that support them#22ayim wants to merge 30 commits into
ayim wants to merge 30 commits into
Conversation
Relates to OSS-408
…o-initialise-configcustomfields-for-plugin-entities
Relates to OSS-408
`getEntityNamesWithCustomFields()` called every `translations` relation target
as a function to read its `.name`. TypeORM also allows a bare string target and
a closure returning a string (both used to break circular imports, e.g.
`@OneToMany('ArticleTranslation', ...)`). The former threw
`relation.type is not a function` — and since this runs unconditionally at the
top of `runPluginConfigurations()`, one such relation anywhere in the process
aborted every bootstrap; the latter yielded `undefined` and silently failed to
exclude the translation entity, re-introducing the duplicate-`customFields`
schema error this exclusion exists to prevent.
Added `getRelationTargetName()` which resolves all three shapes to the entity
name, guarding `typeof type === 'function'` the same way `getEntityTranslation`
in validate-custom-fields-config.ts does. Tests cover string, closure-returning-
string, and constructor-closure targets.
Relates to vendurehq#4965
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.
Mirrored from vendurehq#4965 for the Overwatch review demo.
Original author: @grolmus
Summary
Auto-initialises an empty
customFieldsarray for every entity that supports custom fields, so a plugin'sconfigurationcallback can extend any such entity (core or plugin-defined) without the defensive guard:Resolves OSS-408.
How
getEntityNamesWithCustomFields()(register-custom-entity-fields.ts) detects custom-field-capable entities from the TypeORM metadata (they declare acustomFieldsembedded column) — aHasCustomFieldsimplementscheck isn't available at runtime.runPluginConfigurations()(bootstrap.ts) pre-seedsconfig.customFields[EntityName] = []for each of those before the pluginconfigurationcallbacks run, only where an entry doesn't already exist (never overwrites configured fields).Empty arrays are ignored by
registerCustomEntityFields(it already skips zero-length configs), so this is inert for entities nobody extends — no schema/column changes.The TypeScript half of the original issue (the
as anyfootgun) is already resolved onmain:CustomFieldsnow carries a& { [entity: string]: CustomFieldConfig[] }index signature, soconfig.customFields.CompanyRoleis typed without a cast. This PR adds the matching runtime guarantee.Tests
bootstrap.spec.ts— custom-field-capable entities get[]initialised; existing entries are preserved; and a@VendurePlugin({ configuration })canpushto another entity's custom fields without a guard. Source typechecks clean; related entity specs pass.Relates to OSS-408
Need help on this PR? Tag
/codesmithwith what you need. Autofix is disabled.