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 5
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 5
export class MetadataArgsStorage {
31

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

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

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

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

69
    filterColumns(target: Function|string): ColumnMetadataArgs[];
70
    filterColumns(target: (Function|string)[]): ColumnMetadataArgs[];
71 5
    filterColumns(target: (Function|string)|(Function|string)[]): ColumnMetadataArgs[] {
72 5
        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 5
    findGenerated(target: (Function|string)|(Function|string)[], propertyName: string): GeneratedMetadataArgs|undefined {
78 5
        return this.generations.find(generated => {
79 5
            return (Array.isArray(target) ? target.indexOf(generated.target) !== -1 : generated.target === target) && generated.propertyName === propertyName;
80
        });
81
    }
82

83 5
    findTree(target: (Function|string)|(Function|string)[]): TreeMetadataArgs|undefined {
84 5
        return this.trees.find(tree => {
85 5
            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 5
    filterRelations(target: (Function|string)|(Function|string)[]): RelationMetadataArgs[] {
92 5
        return this.filterByTargetAndWithoutDuplicateProperties(this.relations, target);
93
    }
94

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

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

107
    filterIndices(target: Function|string): IndexMetadataArgs[];
108
    filterIndices(target: (Function|string)[]): IndexMetadataArgs[];
109 5
    filterIndices(target: (Function|string)|(Function|string)[]): IndexMetadataArgs[] {
110
        // todo: implement parent-entity overrides?
111 5
        return this.indices.filter(index => {
112 5
            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 5
    filterUniques(target: (Function|string)|(Function|string)[]): UniqueMetadataArgs[] {
119 5
        return this.uniques.filter(unique => {
120 5
            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 5
    filterChecks(target: (Function|string)|(Function|string)[]): CheckMetadataArgs[] {
127 5
        return this.checks.filter(check => {
128 5
            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 5
    filterExclusions(target: (Function|string)|(Function|string)[]): ExclusionMetadataArgs[] {
135 3
        return this.exclusions.filter(exclusion => {
136 3
            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 5
    filterListeners(target: (Function|string)|(Function|string)[]): EntityListenerMetadataArgs[] {
143 5
        return this.filterByTarget(this.entityListeners, target);
144
    }
145

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

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

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

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

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

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

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

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

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

202 5
    findDiscriminatorValue(target: Function|string): DiscriminatorValueMetadataArgs|undefined {
203 5
        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 5
    protected filterByTarget<T extends { target: Function|string }>(array: T[], target: (Function|string)|(Function|string)[]): T[] {
214 5
        return array.filter(table => {
215 5
            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 5
    protected filterByTargetAndWithoutDuplicateProperties<T extends { target: Function|string, propertyName: string }>(array: T[], target: (Function|string)|(Function|string)[]): T[] {
223 5
        const newArray: T[] = [];
224 5
        array.forEach(item => {
225 5
            const sameTarget = Array.isArray(target) ? target.indexOf(item.target) !== -1 : item.target === target;
226 5
            if (sameTarget) {
227 5
                if (!newArray.find(newItem => newItem.propertyName === item.propertyName))
228 5
                    newArray.push(item);
229
            }
230
        });
231 5
        return newArray;
232
    }
233

234
    /**
235
     * Filters given array by a given target or targets and prevents duplicate embedded property names.
236
     */
237 5
    protected filterByTargetAndWithoutDuplicateEmbeddedProperties<T extends EmbeddedMetadataArgs>(array: T[], target: (Function|string)|(Function|string)[]): T[] {
238 5
        const newArray: T[] = [];
239 5
        array.forEach(item => {
240 5
            const sameTarget = Array.isArray(target) ? target.indexOf(item.target) !== -1 : item.target === target;
241 5
            if (sameTarget) {
242 5
                const isDuplicateEmbeddedProperty = newArray.find((newItem: EmbeddedMetadataArgs): boolean =>
243 5
                    newItem.prefix === item.prefix && newItem.propertyName === item.propertyName
244
                );
245 5
                if (!isDuplicateEmbeddedProperty)
246 5
                    newArray.push(item);
247
            }
248
        });
249 5
        return newArray;
250
    }
251

252 5
}

Read our documentation on viewing source code .

Loading