-
-
Notifications
You must be signed in to change notification settings - Fork 505
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
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
- Loading branch information
Showing
15 changed files
with
398 additions
and
75 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
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
Oops, something went wrong.