typeorm / typeorm
1
import {RelationMetadataArgs} from "./RelationMetadataArgs";
2
import {ColumnMetadataArgs} from "./ColumnMetadataArgs";
3
import {RelationCountMetadataArgs} from "./RelationCountMetadataArgs";
4
import {IndexMetadataArgs} from "./IndexMetadataArgs";
5
import {EntityListenerMetadataArgs} from "./EntityListenerMetadataArgs";
6
import {TableMetadataArgs} from "./TableMetadataArgs";
7
import {NamingStrategyMetadataArgs} from "./NamingStrategyMetadataArgs";
8
import {JoinTableMetadataArgs} from "./JoinTableMetadataArgs";
9
import {JoinColumnMetadataArgs} from "./JoinColumnMetadataArgs";
10
import {EmbeddedMetadataArgs} from "./EmbeddedMetadataArgs";
11
import {EntitySubscriberMetadataArgs} from "./EntitySubscriberMetadataArgs";
12
import {RelationIdMetadataArgs} from "./RelationIdMetadataArgs";
13
import {InheritanceMetadataArgs} from "./InheritanceMetadataArgs";
14
import {DiscriminatorValueMetadataArgs} from "./DiscriminatorValueMetadataArgs";
15
import {EntityRepositoryMetadataArgs} from "./EntityRepositoryMetadataArgs";
16
import {TransactionEntityMetadataArgs} from "./TransactionEntityMetadataArgs";
17
import {TransactionRepositoryMetadataArgs} from "./TransactionRepositoryMetadataArgs";
18 4
import {MetadataUtils} from "../metadata-builder/MetadataUtils";
19
import {GeneratedMetadataArgs} from "./GeneratedMetadataArgs";
20
import {TreeMetadataArgs} from "./TreeMetadataArgs";
21
import {UniqueMetadataArgs} from "./UniqueMetadataArgs";
22
import {CheckMetadataArgs} from "./CheckMetadataArgs";
23
import {ExclusionMetadataArgs} from "./ExclusionMetadataArgs";
24

25
/**
26
 * Storage all metadatas args of all available types: tables, columns, subscribers, relations, etc.
27
 * Each metadata args represents some specifications of what it represents.
28
 * MetadataArgs used to create a real Metadata objects.
29
 */
30 4
export class MetadataArgsStorage {
31

32
    // -------------------------------------------------------------------------
33
    // Properties
34
    // -------------------------------------------------------------------------
35

36 4
    readonly tables: TableMetadataArgs[] = [];
37 4
    readonly trees: TreeMetadataArgs[] = [];
38 4
    readonly entityRepositories: EntityRepositoryMetadataArgs[] = [];
39 4
    readonly transactionEntityManagers: TransactionEntityMetadataArgs[] = [];
40 4
    readonly transactionRepositories: TransactionRepositoryMetadataArgs[] = [];
41 4
    readonly namingStrategies: NamingStrategyMetadataArgs[] = [];
42 4
    readonly entitySubscribers: EntitySubscriberMetadataArgs[] = [];
43 4
    readonly indices: IndexMetadataArgs[] = [];
44 4
    readonly uniques: UniqueMetadataArgs[] = [];
45 4
    readonly checks: CheckMetadataArgs[] = [];
46 4
    readonly exclusions: ExclusionMetadataArgs[] = [];
47 4
    readonly columns: ColumnMetadataArgs[] = [];
48 4
    readonly generations: GeneratedMetadataArgs[] = [];
49 4
    readonly relations: RelationMetadataArgs[] = [];
50 4
    readonly joinColumns: JoinColumnMetadataArgs[] = [];
51 4
    readonly joinTables: JoinTableMetadataArgs[] = [];
52 4
    readonly entityListeners: EntityListenerMetadataArgs[] = [];
53 4
    readonly relationCounts: RelationCountMetadataArgs[] = [];
54 4
    readonly relationIds: RelationIdMetadataArgs[] = [];
55 4
    readonly embeddeds: EmbeddedMetadataArgs[] = [];
56 4
    readonly inheritances: InheritanceMetadataArgs[] = [];
57 4
    readonly discriminatorValues: DiscriminatorValueMetadataArgs[] = [];
58

59
    // -------------------------------------------------------------------------
60
    // Public Methods
61
    // -------------------------------------------------------------------------
62

63
    filterTables(target: Function|string): TableMetadataArgs[];
64
    filterTables(target: (Function|string)[]): TableMetadataArgs[];
65 4
    filterTables(target: (Function|string)|(Function|string)[]): TableMetadataArgs[] {
66 4
        return this.filterByTarget(this.tables, target);
67
    }
68

69
    filterColumns(target: Function|string): ColumnMetadataArgs[];
70
    filterColumns(target: (Function|string)[]): ColumnMetadataArgs[];
71 4
    filterColumns(target: (Function|string)|(Function|string)[]): ColumnMetadataArgs[] {
72 4
        return this.filterByTargetAndWithoutDuplicateProperties(this.columns, target);
73
    }
74

75
    findGenerated(target: Function|string, propertyName: string): GeneratedMetadataArgs|undefined;
76
    findGenerated(target: (Function|string)[], propertyName: string): GeneratedMetadataArgs|undefined;
77 4
    findGenerated(target: (Function|string)|(Function|string)[], propertyName: string): GeneratedMetadataArgs|undefined {
78 4
        return this.generations.find(generated => {
79 4
            return (Array.isArray(target) ? target.indexOf(generated.target) !== -1 : generated.target === target) && generated.propertyName === propertyName;
80
        });
81
    }
82

83 4
    findTree(target: (Function|string)|(Function|string)[]): TreeMetadataArgs|undefined {
84 4
        return this.trees.find(tree => {
85 4
            return (Array.isArray(target) ? target.indexOf(tree.target) !== -1 : tree.target === target);
86
        });
87
    }
88

89
    filterRelations(target: Function|string): RelationMetadataArgs[];
90
    filterRelations(target: (Function|string)[]): RelationMetadataArgs[];
91 4
    filterRelations(target: (Function|string)|(Function|string)[]): RelationMetadataArgs[] {
92 4
        return this.filterByTargetAndWithoutDuplicateRelationProperties(this.relations, target);
93
    }
94

95
    filterRelationIds(target: Function|string): RelationIdMetadataArgs[];
96
    filterRelationIds(target: (Function|string)[]): RelationIdMetadataArgs[];
97 4
    filterRelationIds(target: (Function|string)|(Function|string)[]): RelationIdMetadataArgs[] {
98 4
        return this.filterByTargetAndWithoutDuplicateProperties(this.relationIds, target);
99
    }
100

101
    filterRelationCounts(target: Function|string): RelationCountMetadataArgs[];
102
    filterRelationCounts(target: (Function|string)[]): RelationCountMetadataArgs[];
103 4
    filterRelationCounts(target: (Function|string)|(Function|string)[]): RelationCountMetadataArgs[] {
104 4
        return this.filterByTargetAndWithoutDuplicateProperties(this.relationCounts, target);
105
    }
106

107
    filterIndices(target: Function|string): IndexMetadataArgs[];
108
    filterIndices(target: (Function|string)[]): IndexMetadataArgs[];
109 4
    filterIndices(target: (Function|string)|(Function|string)[]): IndexMetadataArgs[] {
110
        // todo: implement parent-entity overrides?
111 4
        return this.indices.filter(index => {
112 4
            return Array.isArray(target) ? target.indexOf(index.target) !== -1 : index.target === target;
113
        });
114
    }
115

116
    filterUniques(target: Function|string): UniqueMetadataArgs[];
117
    filterUniques(target: (Function|string)[]): UniqueMetadataArgs[];
118 4
    filterUniques(target: (Function|string)|(Function|string)[]): UniqueMetadataArgs[] {
119 4
        return this.uniques.filter(unique => {
120 4
            return Array.isArray(target) ? target.indexOf(unique.target) !== -1 : unique.target === target;
121
        });
122
    }
123

124
    filterChecks(target: Function|string): CheckMetadataArgs[];
125
    filterChecks(target: (Function|string)[]): CheckMetadataArgs[];
126 4
    filterChecks(target: (Function|string)|(Function|string)[]): CheckMetadataArgs[] {
127 4
        return this.checks.filter(check => {
128 4
            return Array.isArray(target) ? target.indexOf(check.target) !== -1 : check.target === target;
129
        });
130
    }
131

132
    filterExclusions(target: Function|string): ExclusionMetadataArgs[];
133
    filterExclusions(target: (Function|string)[]): ExclusionMetadataArgs[];
134 4
    filterExclusions(target: (Function|string)|(Function|string)[]): ExclusionMetadataArgs[] {
135 2
        return this.exclusions.filter(exclusion => {
136 2
            return Array.isArray(target) ? target.indexOf(exclusion.target) !== -1 : exclusion.target === target;
137
        });
138
    }
139

140
    filterListeners(target: Function|string): EntityListenerMetadataArgs[];
141
    filterListeners(target: (Function|string)[]): EntityListenerMetadataArgs[];
142 4
    filterListeners(target: (Function|string)|(Function|string)[]): EntityListenerMetadataArgs[] {
143 4
        return this.filterByTarget(this.entityListeners, target);
144
    }
145

146
    filterEmbeddeds(target: Function|string): EmbeddedMetadataArgs[];
147
    filterEmbeddeds(target: (Function|string)[]): EmbeddedMetadataArgs[];
148 4
    filterEmbeddeds(target: (Function|string)|(Function|string)[]): EmbeddedMetadataArgs[] {
149 4
        return this.filterByTargetAndWithoutDuplicateEmbeddedProperties(this.embeddeds, target);
150
    }
151

152 4
    findJoinTable(target: Function|string, propertyName: string): JoinTableMetadataArgs|undefined {
153 4
        return this.joinTables.find(joinTable => {
154 4
            return joinTable.target === target && joinTable.propertyName === propertyName;
155
        });
156
    }
157

158 4
    filterJoinColumns(target: Function|string, propertyName: string): JoinColumnMetadataArgs[] {
159
        // todo: implement parent-entity overrides?
160 4
        return this.joinColumns.filter(joinColumn => {
161 4
            return joinColumn.target === target && joinColumn.propertyName === propertyName;
162
        });
163
    }
164

165
    filterSubscribers(target: Function|string): EntitySubscriberMetadataArgs[];
166
    filterSubscribers(target: (Function|string)[]): EntitySubscriberMetadataArgs[];
167 4
    filterSubscribers(target: (Function|string)|(Function|string)[]): EntitySubscriberMetadataArgs[] {
168 4
        return this.filterByTarget(this.entitySubscribers, target);
169
    }
170

171
    filterNamingStrategies(target: Function|string): NamingStrategyMetadataArgs[];
172
    filterNamingStrategies(target: (Function|string)[]): NamingStrategyMetadataArgs[];
173 4
    filterNamingStrategies(target: (Function|string)|(Function|string)[]): NamingStrategyMetadataArgs[] {
174 0
        return this.filterByTarget(this.namingStrategies, target);
175
    }
176

177 4
    filterTransactionEntityManagers(target: Function|string, propertyName: string): TransactionEntityMetadataArgs[] {
178 2
        return this.transactionEntityManagers.filter(transactionEm => {
179 2
            return (Array.isArray(target) ? target.indexOf(transactionEm.target) !== -1 : transactionEm.target === target) && transactionEm.methodName === propertyName;
180
        });
181
    }
182

183 4
    filterTransactionRepository(target: Function|string, propertyName: string): TransactionRepositoryMetadataArgs[] {
184 2
        return this.transactionRepositories.filter(transactionEm => {
185 2
            return (Array.isArray(target) ? target.indexOf(transactionEm.target) !== -1 : transactionEm.target === target) && transactionEm.methodName === propertyName;
186
        });
187
    }
188

189 4
    filterSingleTableChildren(target: Function|string): TableMetadataArgs[] {
190 4
        return this.tables.filter(table => {
191 4
            return table.target instanceof Function
192 4
                && target instanceof Function
193 4
                && MetadataUtils.isInherited(table.target, target)
194 4
                && table.type === "entity-child";
195
        });
196
    }
197

198 4
    findInheritanceType(target: Function|string): InheritanceMetadataArgs|undefined {
199 4
        return this.inheritances.find(inheritance => inheritance.target === target);
200
    }
201

202 4
    findDiscriminatorValue(target: Function|string): DiscriminatorValueMetadataArgs|undefined {
203 4
        return this.discriminatorValues.find(discriminatorValue => discriminatorValue.target === target);
204
    }
205

206
    // -------------------------------------------------------------------------
207
    // Protected Methods
208
    // -------------------------------------------------------------------------
209

210
    /**
211
     * Filters given array by a given target or targets.
212
     */
213 4
    protected filterByTarget<T extends { target: Function|string }>(array: T[], target: (Function|string)|(Function|string)[]): T[] {
214 4
        return array.filter(table => {
215 4
            return Array.isArray(target) ? target.indexOf(table.target) !== -1 : table.target === target;
216
        });
217
    }
218

219
    /**
220
     * Filters given array by a given target or targets and prevents duplicate property names.
221
     */
222 4
    protected filterByTargetAndWithoutDuplicateProperties<T extends { target: Function|string, propertyName: string }>(array: T[], target: (Function|string)|(Function|string)[]): T[] {
223 4
        const newArray: T[] = [];
224 4
        array.forEach(item => {
225 4
            const sameTarget = Array.isArray(target) ? target.indexOf(item.target) !== -1 : item.target === target;
226 4
            if (sameTarget) {
227 4
                if (!newArray.find(newItem => newItem.propertyName === item.propertyName))
228 4
                    newArray.push(item);
229
            }
230
        });
231 4
        return newArray;
232
    }
233

234
    /**
235
     * Filters given array by a given target or targets and prevents duplicate relation property names.
236
     */
237 4
    protected filterByTargetAndWithoutDuplicateRelationProperties<T extends RelationMetadataArgs>(array: T[], target: (Function|string)|(Function|string)[]): T[] {
238 4
        const newArray: T[] = [];
239 4
        array.forEach(item => {
240 4
            const sameTarget = target instanceof Array ? target.indexOf(item.target) !== -1 : item.target === target;
241 4
            if (sameTarget) {
242 4
                const existingIndex = newArray.findIndex(newItem => newItem.propertyName === item.propertyName);
243 4
                if (target instanceof Array && existingIndex !== -1 && target.indexOf(item.target) < target.indexOf(newArray[existingIndex].target)) {
244 4
                    const clone = Object.create(newArray[existingIndex]);
245 4
                    clone.type = item.type;
246 4
                    newArray[existingIndex] = clone;
247 4
                } else if (existingIndex === -1) {
248 4
                    newArray.push(item);
249
                }
250
            }
251
        });
252 4
        return newArray;
253
    }
254

255
    /**
256
     * Filters given array by a given target or targets and prevents duplicate embedded property names.
257
     */
258 4
    protected filterByTargetAndWithoutDuplicateEmbeddedProperties<T extends EmbeddedMetadataArgs>(array: T[], target: (Function|string)|(Function|string)[]): T[] {
259 4
        const newArray: T[] = [];
260 4
        array.forEach(item => {
261 4
            const sameTarget = Array.isArray(target) ? target.indexOf(item.target) !== -1 : item.target === target;
262 4
            if (sameTarget) {
263 4
                const isDuplicateEmbeddedProperty = newArray.find((newItem: EmbeddedMetadataArgs): boolean =>
264 4
                    newItem.prefix === item.prefix && newItem.propertyName === item.propertyName
265
                );
266 4
                if (!isDuplicateEmbeddedProperty)
267 4
                    newArray.push(item);
268
            }
269
        });
270 4
        return newArray;
271
    }
272

273 4
}

Read our documentation on viewing source code .

Loading