1 10
import {importClassesFromDirectories} from "../util/DirectoryExportedClassesLoader";
2 10
import {OrmUtils} from "../util/OrmUtils";
3 10
import {getFromContainer} from "../container";
4
import {MigrationInterface} from "../migration/MigrationInterface";
5 10
import {getMetadataArgsStorage} from "../index";
6 10
import {EntityMetadataBuilder} from "../metadata-builder/EntityMetadataBuilder";
7 10
import {EntitySchemaTransformer} from "../entity-schema/EntitySchemaTransformer";
8
import {Connection} from "./Connection";
9 10
import {EntitySchema} from "../entity-schema/EntitySchema";
10
import {EntityMetadata} from "../metadata/EntityMetadata";
11
import {EntitySubscriberInterface} from "../subscriber/EntitySubscriberInterface";
12

13
/**
14
 * Builds migration instances, subscriber instances and entity metadatas for the given classes.
15
 */
16 10
export class ConnectionMetadataBuilder {
17

18
    // -------------------------------------------------------------------------
19
    // Constructor
20
    // -------------------------------------------------------------------------
21

22 10
    constructor(protected connection: Connection) {
23
    }
24

25
    // -------------------------------------------------------------------------
26
    // Public Methods
27
    // -------------------------------------------------------------------------
28

29
    /**
30
     * Builds migration instances for the given classes or directories.
31
     */
32 10
    buildMigrations(migrations: (Function|string)[]): MigrationInterface[] {
33 10
        const [migrationClasses, migrationDirectories] = OrmUtils.splitClassesAndStrings(migrations);
34 10
        const allMigrationClasses = [...migrationClasses, ...importClassesFromDirectories(this.connection.logger, migrationDirectories)];
35 10
        return allMigrationClasses.map(migrationClass => getFromContainer<MigrationInterface>(migrationClass));
36
    }
37

38
    /**
39
     * Builds subscriber instances for the given classes or directories.
40
     */
41 10
    buildSubscribers(subscribers: (Function|string)[]): EntitySubscriberInterface<any>[] {
42 10
        const [subscriberClasses, subscriberDirectories] = OrmUtils.splitClassesAndStrings(subscribers || []);
43 10
        const allSubscriberClasses = [...subscriberClasses, ...importClassesFromDirectories(this.connection.logger, subscriberDirectories)];
44 10
        return getMetadataArgsStorage()
45
            .filterSubscribers(allSubscriberClasses)
46 10
            .map(metadata => getFromContainer<EntitySubscriberInterface<any>>(metadata.target));
47
    }
48

49
    /**
50
     * Builds entity metadatas for the given classes or directories.
51
     */
52 10
    buildEntityMetadatas(entities: (Function|EntitySchema<any>|string)[]): EntityMetadata[] {
53
        // todo: instead we need to merge multiple metadata args storages
54

55 10
        const [entityClassesOrSchemas, entityDirectories] = OrmUtils.splitClassesAndStrings(entities || []);
56 10
        const entityClasses: Function[] = entityClassesOrSchemas.filter(entityClass => (entityClass instanceof EntitySchema) === false) as any;
57 10
        const entitySchemas: EntitySchema<any>[] = entityClassesOrSchemas.filter(entityClass => entityClass instanceof EntitySchema) as any;
58

59 10
        const allEntityClasses = [...entityClasses, ...importClassesFromDirectories(this.connection.logger, entityDirectories)];
60 10
        allEntityClasses.forEach(entityClass => { // if we have entity schemas loaded from directories
61 10
            if (entityClass instanceof EntitySchema) {
62 0
                entitySchemas.push(entityClass);
63 0
                allEntityClasses.slice(allEntityClasses.indexOf(entityClass), 1);
64
            }
65
        });
66 10
        const decoratorEntityMetadatas = new EntityMetadataBuilder(this.connection, getMetadataArgsStorage()).build(allEntityClasses);
67

68 10
        const metadataArgsStorageFromSchema = new EntitySchemaTransformer().transform(entitySchemas);
69 10
        const schemaEntityMetadatas = new EntityMetadataBuilder(this.connection, metadataArgsStorageFromSchema).build();
70

71 10
        return [...decoratorEntityMetadatas, ...schemaEntityMetadatas];
72
    }
73

74 10
}

Read our documentation on viewing source code .

Loading