Skip to content

Commit

Permalink
feature: allow setting discriminator value manually
Browse files Browse the repository at this point in the history
This commit allows using an instance of a base class in a
Single Table Inheritance scenario and setting the discriminator value
manually.

Related to: typeorm#9033
  • Loading branch information
felix-gohla committed May 27, 2022
1 parent 3eab9e5 commit a880b59
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/metadata/EntityMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,13 @@ export class EntityMetadata {
this.inheritancePattern === "STI" &&
this.childEntityMetadatas.length > 0
) {
// There could be a column on the base class that can manually be set to override the type.
let manuallySetDiscriminatorValue: unknown
if (this.discriminatorColumn) {
manuallySetDiscriminatorValue = value[this.discriminatorColumn.propertyName]
}
return this.childEntityMetadatas.find(
(meta) => value.constructor === meta.target,
(meta) => manuallySetDiscriminatorValue === meta.discriminatorValue || value.constructor === meta.target,
) || this
}
return this
Expand Down

0 comments on commit a880b59

Please sign in to comment.