-
-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add support for polymorphic embeddables (#2426)
Polymorphic embeddables allow us to define multiple classes for a single embedded property and the right one will be used based on the discriminator column, similar to how single table inheritance work. ```ts enum AnimalType { CAT, DOG, } @embeddable({ abstract: true, discriminatorColumn: 'type' }) abstract class Animal { @enum(() => AnimalType) type!: AnimalType; @Property() name!: string; } @embeddable({ discriminatorValue: AnimalType.CAT }) class Cat extends Animal { @Property({ nullable: true }) canMeow?: boolean = true; constructor(name: string) { super(); this.type = AnimalType.CAT; this.name = name; } } @embeddable({ discriminatorValue: AnimalType.DOG }) class Dog extends Animal { @Property({ nullable: true }) canBark?: boolean = true; constructor(name: string) { super(); this.type = AnimalType.DOG; this.name = name; } } @entity() class Owner { @PrimaryKey() id!: number; @Property() name!: string; @Embedded(() => [Cat, Dog]) pet!: Cat | Dog; } ``` Closes #1165 BREAKING CHANGE: Embeddable instances are now created via `EntityFactory` and they respect the `forceEntityConstructor` configuration. Due to this we need to have EM instance when assigning to embedded properties. Using `em.assign()` should be preferred to get around this. Deep assigning of child entities now works by default based on the presence of PKs in the payload. This behaviour can be disable via updateByPrimaryKey: false in the assign options. `mergeObjects` option is now enabled by default.
- Loading branch information
Showing
37 changed files
with
1,011 additions
and
153 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
import type { Constructor, Dictionary } from '../typings'; | ||
import { MetadataStorage } from '../metadata'; | ||
|
||
export function Embeddable() { | ||
export function Embeddable(options: EmbeddableOptions = {}) { | ||
return function <T>(target: T & Dictionary) { | ||
const meta = MetadataStorage.getMetadataFromDecorator(target); | ||
meta.class = target as unknown as Constructor<T>; | ||
meta.name = target.name; | ||
meta.embeddable = true; | ||
Object.assign(meta, options); | ||
|
||
return target; | ||
}; | ||
} | ||
|
||
export type EmbeddableOptions = { | ||
discriminatorColumn?: string; | ||
discriminatorMap?: Dictionary<string>; | ||
discriminatorValue?: number | string; | ||
abstract?: boolean; | ||
}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.