mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
feat: custom STI discriminator value for EntitySchema (#10508)
add discriminator value metadata to EntitySchema Closes: #10494
This commit is contained in:
parent
48f5f85d68
commit
b240d87f34
@ -303,6 +303,9 @@ const ASchema = new EntitySchema<A>({
|
||||
target: A,
|
||||
name: "A",
|
||||
type: "entity-child",
|
||||
// When saving instances of 'A', the "type" column will have the value
|
||||
// specified on the 'discriminatorValue' property
|
||||
discriminatorValue: "my-custom-discriminator-value-for-A",
|
||||
columns: {
|
||||
...BaseSchema.options.columns,
|
||||
a: {
|
||||
@ -315,6 +318,7 @@ const BSchema = new EntitySchema<B>({
|
||||
target: B,
|
||||
name: "B",
|
||||
type: "entity-child",
|
||||
discriminatorValue: undefined, // Defaults to the class name (e.g. "B")
|
||||
columns: {
|
||||
...BaseSchema.options.columns,
|
||||
b: {
|
||||
@ -327,6 +331,7 @@ const CSchema = new EntitySchema<C>({
|
||||
target: C,
|
||||
name: "C",
|
||||
type: "entity-child",
|
||||
discriminatorValue: "my-custom-discriminator-value-for-C",
|
||||
columns: {
|
||||
...BaseSchema.options.columns,
|
||||
c: {
|
||||
|
||||
@ -124,4 +124,9 @@ export class EntitySchemaOptions<T> {
|
||||
* Inheritance options.
|
||||
*/
|
||||
inheritance?: EntitySchemaInheritanceOptions
|
||||
|
||||
/**
|
||||
* Custom discriminator value for Single Table Inheritance.
|
||||
*/
|
||||
discriminatorValue?: string
|
||||
}
|
||||
|
||||
@ -65,6 +65,15 @@ export class EntitySchemaTransformer {
|
||||
} as InheritanceMetadataArgs)
|
||||
}
|
||||
|
||||
const { discriminatorValue } = options
|
||||
|
||||
if (discriminatorValue) {
|
||||
metadataArgsStorage.discriminatorValues.push({
|
||||
target: options.target || options.name,
|
||||
value: discriminatorValue,
|
||||
})
|
||||
}
|
||||
|
||||
this.transformColumnsRecursive(options, metadataArgsStorage)
|
||||
})
|
||||
|
||||
|
||||
7
test/github-issues/10494/entity/A.ts
Normal file
7
test/github-issues/10494/entity/A.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { Base } from "./Base"
|
||||
|
||||
export class A extends Base {
|
||||
constructor(public a: boolean) {
|
||||
super()
|
||||
}
|
||||
}
|
||||
7
test/github-issues/10494/entity/B.ts
Normal file
7
test/github-issues/10494/entity/B.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { Base } from "./Base"
|
||||
|
||||
export class B extends Base {
|
||||
constructor(public b: number) {
|
||||
super()
|
||||
}
|
||||
}
|
||||
6
test/github-issues/10494/entity/Base.ts
Normal file
6
test/github-issues/10494/entity/Base.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export abstract class Base {
|
||||
id!: number
|
||||
type!: string
|
||||
createdAt!: Date
|
||||
updatedAt!: Date
|
||||
}
|
||||
7
test/github-issues/10494/entity/C.ts
Normal file
7
test/github-issues/10494/entity/C.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { Base } from "./Base"
|
||||
|
||||
export class C extends Base {
|
||||
constructor(public c: string) {
|
||||
super()
|
||||
}
|
||||
}
|
||||
5
test/github-issues/10494/entity/index.ts
Normal file
5
test/github-issues/10494/entity/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export * from "./Base"
|
||||
|
||||
export * from "./A"
|
||||
export * from "./B"
|
||||
export * from "./C"
|
||||
72
test/github-issues/10494/issues-10494.ts
Normal file
72
test/github-issues/10494/issues-10494.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import "reflect-metadata"
|
||||
|
||||
import { expect } from "chai"
|
||||
|
||||
import {
|
||||
createTestingConnections,
|
||||
closeTestingConnections,
|
||||
reloadTestingDatabases,
|
||||
} from "../../utils/test-utils"
|
||||
import { DataSource } from "../../../src/data-source/DataSource"
|
||||
import { Repository } from "../../../src"
|
||||
|
||||
import { Base, A, B, C } from "./entity"
|
||||
import { BaseSchema, ASchema, BSchema, CSchema } from "./schema"
|
||||
|
||||
describe("github issues > #10494 Custom discriminator values when using Single Table Inheritance with Entity Schemas", () => {
|
||||
let dataSources: DataSource[]
|
||||
|
||||
before(
|
||||
async () =>
|
||||
(dataSources = await createTestingConnections({
|
||||
entities: [BaseSchema, ASchema, BSchema, CSchema],
|
||||
schemaCreate: true,
|
||||
dropSchema: true,
|
||||
enabledDrivers: [
|
||||
"better-sqlite3",
|
||||
"cockroachdb",
|
||||
"mariadb",
|
||||
"mssql",
|
||||
"mysql",
|
||||
"oracle",
|
||||
"postgres",
|
||||
"spanner",
|
||||
"sqlite",
|
||||
],
|
||||
})),
|
||||
)
|
||||
|
||||
beforeEach(() => reloadTestingDatabases(dataSources))
|
||||
|
||||
after(() => closeTestingConnections(dataSources))
|
||||
|
||||
it("should use custom discriminator values, when specified", () =>
|
||||
Promise.all(
|
||||
dataSources.map(async (dataSource) => {
|
||||
// Arrange
|
||||
const repository: Repository<Base> =
|
||||
dataSource.getRepository(Base)
|
||||
|
||||
const entities: Base[] = [new A(true), new B(42), new C("foo")]
|
||||
|
||||
await repository.save(entities)
|
||||
|
||||
// Act
|
||||
const loadedEntities = await repository.find({
|
||||
order: { type: "ASC" },
|
||||
})
|
||||
|
||||
// Assert
|
||||
// B doesn't specify a discriminator value, so it should
|
||||
// default to its class name
|
||||
expect(loadedEntities[0]).to.be.instanceOf(B)
|
||||
expect(loadedEntities[0].type).to.be.equal("B")
|
||||
|
||||
expect(loadedEntities[1]).to.be.instanceOf(A)
|
||||
expect(loadedEntities[1].type).to.be.equal("custom-a")
|
||||
|
||||
expect(loadedEntities[2]).to.be.instanceOf(C)
|
||||
expect(loadedEntities[2].type).to.be.equal("custom-c")
|
||||
}),
|
||||
))
|
||||
})
|
||||
18
test/github-issues/10494/schema/A.ts
Normal file
18
test/github-issues/10494/schema/A.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { EntitySchema } from "../../../../src"
|
||||
|
||||
import { A } from "../entity"
|
||||
|
||||
import { BaseSchema } from "./Base"
|
||||
|
||||
export const ASchema = new EntitySchema<A>({
|
||||
target: A,
|
||||
name: "A",
|
||||
type: "entity-child",
|
||||
discriminatorValue: "custom-a",
|
||||
columns: {
|
||||
...BaseSchema.options.columns,
|
||||
a: {
|
||||
type: Boolean,
|
||||
},
|
||||
},
|
||||
})
|
||||
17
test/github-issues/10494/schema/B.ts
Normal file
17
test/github-issues/10494/schema/B.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { EntitySchema } from "../../../../src"
|
||||
|
||||
import { B } from "../entity"
|
||||
|
||||
import { BaseSchema } from "./Base"
|
||||
|
||||
export const BSchema = new EntitySchema<B>({
|
||||
target: B,
|
||||
name: "B",
|
||||
type: "entity-child",
|
||||
columns: {
|
||||
...BaseSchema.options.columns,
|
||||
b: {
|
||||
type: Number,
|
||||
},
|
||||
},
|
||||
})
|
||||
30
test/github-issues/10494/schema/Base.ts
Normal file
30
test/github-issues/10494/schema/Base.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { EntitySchema } from "../../../../src"
|
||||
|
||||
import { Base } from "../entity"
|
||||
|
||||
export const BaseSchema = new EntitySchema<Base>({
|
||||
target: Base,
|
||||
name: "Base",
|
||||
columns: {
|
||||
id: {
|
||||
type: Number,
|
||||
primary: true,
|
||||
generated: "increment",
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
},
|
||||
createdAt: {
|
||||
type: Date,
|
||||
createDate: true,
|
||||
},
|
||||
updatedAt: {
|
||||
type: Date,
|
||||
updateDate: true,
|
||||
},
|
||||
},
|
||||
inheritance: {
|
||||
pattern: "STI",
|
||||
column: "type",
|
||||
},
|
||||
})
|
||||
18
test/github-issues/10494/schema/C.ts
Normal file
18
test/github-issues/10494/schema/C.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { EntitySchema } from "../../../../src"
|
||||
|
||||
import { C } from "../entity"
|
||||
|
||||
import { BaseSchema } from "./Base"
|
||||
|
||||
export const CSchema = new EntitySchema<C>({
|
||||
target: C,
|
||||
name: "C",
|
||||
type: "entity-child",
|
||||
discriminatorValue: "custom-c",
|
||||
columns: {
|
||||
...BaseSchema.options.columns,
|
||||
c: {
|
||||
type: String,
|
||||
},
|
||||
},
|
||||
})
|
||||
5
test/github-issues/10494/schema/index.ts
Normal file
5
test/github-issues/10494/schema/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export * from "./Base"
|
||||
|
||||
export * from "./A"
|
||||
export * from "./B"
|
||||
export * from "./C"
|
||||
Loading…
x
Reference in New Issue
Block a user