1 10
import {EntityMetadata} from "../metadata/EntityMetadata";
2 10
import {ColumnMetadata} from "../metadata/ColumnMetadata";
3 10
import {ForeignKeyMetadata} from "../metadata/ForeignKeyMetadata";
4
import {Connection} from "../connection/Connection";
5 10
import {IndexMetadata} from "../metadata/IndexMetadata";
6

7
/**
8
 * Creates EntityMetadata for junction tables of the closure entities.
9
 * Closure junction tables are tables generated by closure entities.
10
 */
11 10
export class ClosureJunctionEntityMetadataBuilder {
12

13
    // -------------------------------------------------------------------------
14
    // Constructor
15
    // -------------------------------------------------------------------------
16

17 10
    constructor(private connection: Connection) {
18
    }
19

20
    // -------------------------------------------------------------------------
21
    // Public Methods
22
    // -------------------------------------------------------------------------
23

24
    /**
25
     * Builds EntityMetadata for the closure junction of the given closure entity.
26
     */
27 10
    build(parentClosureEntityMetadata: EntityMetadata) {
28

29
        // create entity metadata itself
30 10
        const entityMetadata = new EntityMetadata({
31
            parentClosureEntityMetadata: parentClosureEntityMetadata,
32
            connection: this.connection,
33
            args: {
34
                target: "",
35 10
                name: parentClosureEntityMetadata.treeOptions && parentClosureEntityMetadata.treeOptions.closureTableName ? parentClosureEntityMetadata.treeOptions.closureTableName : parentClosureEntityMetadata.tableNameWithoutPrefix,
36
                type: "closure-junction"
37
            }
38
        });
39 10
        entityMetadata.build();
40

41
        // create ancestor and descendant columns for new closure junction table
42 10
        parentClosureEntityMetadata.primaryColumns.forEach(primaryColumn => {
43 10
            entityMetadata.ownColumns.push(new ColumnMetadata({
44
                connection: this.connection,
45
                entityMetadata: entityMetadata,
46
                closureType: "ancestor",
47
                referencedColumn: primaryColumn,
48
                args: {
49
                    target: "",
50
                    mode: "virtual",
51 10
                    propertyName: parentClosureEntityMetadata.treeOptions && parentClosureEntityMetadata.treeOptions.ancestorColumnName ? parentClosureEntityMetadata.treeOptions.ancestorColumnName(primaryColumn) : primaryColumn.propertyName + "_ancestor",
52
                    options: {
53
                        primary: true,
54
                        length: primaryColumn.length,
55
                        type: primaryColumn.type
56
                    }
57
                }
58
            }));
59 10
            entityMetadata.ownColumns.push(new ColumnMetadata({
60
                connection: this.connection,
61
                entityMetadata: entityMetadata,
62
                closureType: "descendant",
63
                referencedColumn: primaryColumn,
64
                args: {
65
                    target: "",
66
                    mode: "virtual",
67 10
                    propertyName: parentClosureEntityMetadata.treeOptions && parentClosureEntityMetadata.treeOptions.descendantColumnName ? parentClosureEntityMetadata.treeOptions.descendantColumnName(primaryColumn) : primaryColumn.propertyName + "_descendant",
68
                    options: {
69
                        primary: true,
70
                        length: primaryColumn.length,
71
                        type: primaryColumn.type,
72
                    }
73
                }
74
            }));
75
        });
76

77 10
        entityMetadata.ownIndices = [
78
            new IndexMetadata({
79
                entityMetadata: entityMetadata,
80
                columns: [entityMetadata.ownColumns[0]],
81
                args: {
82
                    target: entityMetadata.target,
83
                    synchronize: true
84
                }
85
            }),
86
            new IndexMetadata({
87
                entityMetadata: entityMetadata,
88
                columns: [entityMetadata.ownColumns[1]],
89
                args: {
90
                    target: entityMetadata.target,
91
                    synchronize: true
92
                }
93
            })
94
        ];
95

96
        // if tree level column was defined by a closure entity then add it to the junction columns as well
97 10
        if (parentClosureEntityMetadata.treeLevelColumn) {
98 10
            entityMetadata.ownColumns.push(new ColumnMetadata({
99
                connection: this.connection,
100
                entityMetadata: entityMetadata,
101
                args: {
102
                    target: "",
103
                    mode: "virtual",
104
                    propertyName: "level",
105
                    options: {
106
                        type: this.connection.driver.mappedDataTypes.treeLevel,
107
                    }
108
                }
109
            }));
110
        }
111

112
        // create junction table foreign keys
113 10
        entityMetadata.foreignKeys = [
114
            new ForeignKeyMetadata({
115
                entityMetadata: entityMetadata,
116
                referencedEntityMetadata: parentClosureEntityMetadata,
117
                columns: [entityMetadata.ownColumns[0]],
118
                referencedColumns: parentClosureEntityMetadata.primaryColumns,
119
                // onDelete: "CASCADE" // todo: does not work in mssql for some reason
120
            }),
121
            new ForeignKeyMetadata({
122
                entityMetadata: entityMetadata,
123
                referencedEntityMetadata: parentClosureEntityMetadata,
124
                columns: [entityMetadata.ownColumns[1]],
125
                referencedColumns: parentClosureEntityMetadata.primaryColumns,
126
                // onDelete: "CASCADE" // todo: does not work in mssql for some reason
127
            }),
128
        ];
129

130 10
        return entityMetadata;
131
    }
132

133 10
}

Read our documentation on viewing source code .

Loading