-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for STI on EntitySchema (#9834)
* feat: add support for STI on EntitySchema Closes: #9833 * fix: run prettier --------- Co-authored-by: Umed Khudoiberdiev <pleerock.me@gmail.com>
- Loading branch information
1 parent
97280fc
commit bc306fb
Showing
16 changed files
with
324 additions
and
6 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ColumnOptions } from "../decorator/options/ColumnOptions" | ||
|
||
export interface EntitySchemaInheritanceOptions { | ||
/** | ||
* Inheritance pattern. | ||
*/ | ||
pattern?: "STI" | ||
|
||
/** | ||
* Inheritance discriminator column. | ||
*/ | ||
column?: string | ColumnOptions | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Base } from "./Base" | ||
|
||
export class A extends Base { | ||
constructor(public a: boolean) { | ||
super() | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Base } from "./Base" | ||
|
||
export class B extends Base { | ||
constructor(public b: number) { | ||
super() | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export abstract class Base { | ||
id!: number | ||
type!: string | ||
createdAt!: Date | ||
updatedAt!: Date | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Base } from "./Base" | ||
|
||
export class C extends Base { | ||
constructor(public c: string) { | ||
super() | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from "./Base" | ||
|
||
export * from "./A" | ||
export * from "./B" | ||
export * from "./C" |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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 > #9833 Add support for Single Table Inheritance when using 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 instantiate concrete entities when using EntitySchema", () => | ||
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 | ||
expect(loadedEntities[0]).to.be.instanceOf(A) | ||
expect(loadedEntities[1]).to.be.instanceOf(B) | ||
expect(loadedEntities[2]).to.be.instanceOf(C) | ||
}), | ||
)) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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", | ||
columns: { | ||
...BaseSchema.options.columns, | ||
a: { | ||
type: 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
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, | ||
}, | ||
}, | ||
}) |
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 |
---|---|---|
@@ -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", | ||
}, | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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", | ||
columns: { | ||
...BaseSchema.options.columns, | ||
c: { | ||
type: String, | ||
}, | ||
}, | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from "./Base" | ||
|
||
export * from "./A" | ||
export * from "./B" | ||
export * from "./C" |