Skip to content

Commit 3e58acb

Browse files
committed
feat: add relation.type attribute
Template engine which may not have access to instanceof, can use relation.type
1 parent 80222d1 commit 3e58acb

File tree

7 files changed

+45
-3
lines changed

7 files changed

+45
-3
lines changed

src/pg-structure/relation/m2m-relation.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Memoize } from "@typescript-plus/fast-memoize-decorator/dist/src";
2-
import { RelationNameFunctions } from "../../types/index";
2+
import { RelationNameFunctions, RelationType } from "../../types/index";
33
import ForeignKey from "../constraint/foreign-key";
44
import Table from "../entity/table";
55
import Relation, { RelationConstructorArgs, RelationWithout } from "../base/relation";
@@ -70,6 +70,12 @@ export default class M2MRelation extends Relation {
7070
this.targetForeignKey = args.targetForeignKey;
7171
}
7272

73+
/**
74+
* Type of the relation, which is `m2m` for [[M2MRelation]].
75+
* For TypeScript it is enum of `RelationType.M2M`.
76+
*/
77+
public readonly type = RelationType.M2M;
78+
7379
/**
7480
* Whether the relation targets to many. Since, many to many relations targets many, this is `true`.
7581
*/

src/pg-structure/relation/m2o-relation.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Memoize } from "@typescript-plus/fast-memoize-decorator/dist/src";
2-
import { RelationNameFunctions } from "../../types/index";
2+
import { RelationNameFunctions, RelationType } from "../../types/index";
33
import Relation, { RelationConstructorArgs, RelationWithout } from "../base/relation";
44
import Table from "../entity/table";
55
import ForeignKey from "../constraint/foreign-key";
@@ -43,6 +43,12 @@ export default class M2ORelation extends Relation {
4343
this.foreignKey = args.foreignKey;
4444
}
4545

46+
/**
47+
* Type of the relation, which is `m2o` for [[M2ORelation]].
48+
* For TypeScript it is enum of `RelationType.M2O`.
49+
*/
50+
public readonly type = RelationType.M2O;
51+
4652
/**
4753
* Whether the relation targets to many. Since, many to one relations targets single, this is `true`.
4854
*/

src/pg-structure/relation/o2m-relation.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Memoize } from "@typescript-plus/fast-memoize-decorator/dist/src";
2-
import { RelationNameFunctions } from "../../types/index";
2+
import { RelationNameFunctions, RelationType } from "../../types/index";
33
import Relation, { RelationConstructorArgs, RelationWithout } from "../base/relation";
44
import Table from "../entity/table";
55
import ForeignKey from "../constraint/foreign-key";
@@ -41,6 +41,12 @@ export default class O2MRelation extends Relation {
4141
this.foreignKey = args.foreignKey;
4242
}
4343

44+
/**
45+
* Type of the relation, which is `o2m` for [[O2MRelation]].
46+
* For TypeScript it is enum of `RelationType.O2M`.
47+
*/
48+
public readonly type = RelationType.O2M;
49+
4450
/**
4551
* Whether the relation targets to many. Since, one to many relations targets many, this is `true`.
4652
*/

src/types/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@ import M2MRelation from "../pg-structure/relation/m2m-relation";
55

66
export * from "./json";
77

8+
/**
9+
* Type of the numeric type.
10+
*/
811
export const enum NumericType {
912
Integer = "Integer",
1013
Exact = "Exact",
1114
Floating = "Floating",
1215
}
1316

17+
/**
18+
* Type of the relation.
19+
*/
20+
export const enum RelationType {
21+
O2M = "o2m",
22+
M2M = "m2m",
23+
M2O = "m2o",
24+
}
25+
1426
/**
1527
* Actions performed when the data in the foreign key referenced columns is changed.
1628
*/

test/relation/m2m-relation.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ beforeAll(async () => {
2727
});
2828

2929
describe("M2MRelation", () => {
30+
it("should be type of 'm2m'.", () => {
31+
expect(studentTable.m2mRelations[0].type).toEqual("m2m");
32+
});
33+
3034
it("should be toMany.", () => {
3135
expect(studentTable.m2mRelations[0].toMany).toEqual(true);
3236
});

test/relation/m2o-relation.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ beforeAll(async () => {
2525
});
2626

2727
describe("M2ORelation", () => {
28+
it("should be type of 'm2o'.", () => {
29+
expect(messageTable.m2oRelations[0].type).toEqual("m2o");
30+
});
31+
2832
it("should not be toMany.", () => {
2933
expect(messageTable.m2oRelations[0].toMany).toEqual(false);
3034
});

test/relation/o2m-relation.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ beforeAll(async () => {
3232
});
3333

3434
describe("O2MRelation", () => {
35+
it("should be type of 'o2m'.", () => {
36+
expect(studentTable.o2mRelations[0].type).toEqual("o2m");
37+
});
38+
3539
it("should be toMany.", () => {
3640
expect(studentTable.o2mRelations[0].toMany).toEqual(true);
3741
});

0 commit comments

Comments
 (0)