1 10
import {TreeRepository} from "./TreeRepository";
2
import {EntityMetadata} from "../metadata/EntityMetadata";
3 10
import {Repository} from "./Repository";
4 10
import {MongoDriver} from "../driver/mongodb/MongoDriver";
5 10
import {MongoRepository} from "./MongoRepository";
6
import {QueryRunner} from "../query-runner/QueryRunner";
7
import {EntityManager} from "../entity-manager/EntityManager";
8

9
/**
10
 * Factory used to create different types of repositories.
11
 */
12 10
export class RepositoryFactory {
13

14
    // -------------------------------------------------------------------------
15
    // Public Methods
16
    // -------------------------------------------------------------------------
17

18
    /**
19
     * Creates a repository.
20
     */
21 10
    create(manager: EntityManager, metadata: EntityMetadata, queryRunner?: QueryRunner): Repository<any> {
22

23 10
        if (metadata.treeType) {
24
            // NOTE: dynamic access to protected properties. We need this to prevent unwanted properties in those classes to be exposed,
25
            // however we need these properties for internal work of the class
26 10
            const repository = new TreeRepository<any>();
27 10
            Object.assign(repository, {
28
                manager: manager,
29
                metadata: metadata,
30
                queryRunner: queryRunner,
31
            });
32 10
            return repository;
33

34
        } else {
35
            // NOTE: dynamic access to protected properties. We need this to prevent unwanted properties in those classes to be exposed,
36
            // however we need these properties for internal work of the class
37 10
            let repository: Repository<any>;
38 10
            if (manager.connection.driver instanceof MongoDriver) {
39 6
                repository = new MongoRepository();
40
            } else {
41 10
                repository = new Repository<any>();
42
            }
43 10
            Object.assign(repository, {
44
                manager: manager,
45
                metadata: metadata,
46
                queryRunner: queryRunner,
47
            });
48

49 10
            return repository;
50
        }
51
    }
52

53 10
}

Read our documentation on viewing source code .

Loading