Skip to content

Commit

Permalink
feat: custom STI discriminator value for EntitySchema (#10508)
Browse files Browse the repository at this point in the history
add discriminator value metadata to EntitySchema

Closes: #10494
  • Loading branch information
gabrielkim13 committed Dec 29, 2023
1 parent 48f5f85 commit b240d87
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/separating-entity-definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand Down
5 changes: 5 additions & 0 deletions src/entity-schema/EntitySchemaOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,9 @@ export class EntitySchemaOptions<T> {
* Inheritance options.
*/
inheritance?: EntitySchemaInheritanceOptions

/**
* Custom discriminator value for Single Table Inheritance.
*/
discriminatorValue?: string
}
9 changes: 9 additions & 0 deletions src/entity-schema/EntitySchemaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down
7 changes: 7 additions & 0 deletions test/github-issues/10494/entity/A.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Base } from "./Base"

export class A extends Base {
constructor(public a: boolean) {
super()
}
}
7 changes: 7 additions & 0 deletions test/github-issues/10494/entity/B.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Base } from "./Base"

export class B extends Base {
constructor(public b: number) {
super()
}
}
6 changes: 6 additions & 0 deletions test/github-issues/10494/entity/Base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export abstract class Base {
id!: number
type!: string
createdAt!: Date
updatedAt!: Date
}
7 changes: 7 additions & 0 deletions test/github-issues/10494/entity/C.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Base } from "./Base"

export class C extends Base {
constructor(public c: string) {
super()
}
}
5 changes: 5 additions & 0 deletions test/github-issues/10494/entity/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./Base"

export * from "./A"
export * from "./B"
export * from "./C"
72 changes: 72 additions & 0 deletions test/github-issues/10494/issues-10494.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 18 additions & 0 deletions test/github-issues/10494/schema/A.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions test/github-issues/10494/schema/B.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 30 additions & 0 deletions test/github-issues/10494/schema/Base.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 18 additions & 0 deletions test/github-issues/10494/schema/C.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions test/github-issues/10494/schema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./Base"

export * from "./A"
export * from "./B"
export * from "./C"

0 comments on commit b240d87

Please sign in to comment.