• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

typeorm / typeorm / 15219332477

23 May 2025 09:13PM UTC coverage: 17.216% (-59.1%) from 76.346%
15219332477

Pull #11332

github

naorpeled
cr comments - move if block
Pull Request #11332: feat: add new undefined and null behavior flags

1603 of 12759 branches covered (12.56%)

Branch coverage included in aggregate %.

0 of 31 new or added lines in 3 files covered. (0.0%)

14132 existing lines in 166 files now uncovered.

4731 of 24033 relevant lines covered (19.69%)

60.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.33
/src/repository/AbstractRepository.ts
1
import { ObjectLiteral } from "../common/ObjectLiteral"
2
import { EntityManager } from "../entity-manager/EntityManager"
3
import { Repository } from "./Repository"
4
import { TreeRepository } from "./TreeRepository"
5
import { EntityTarget } from "../common/EntityTarget"
6
import { ObjectType } from "../common/ObjectType"
7
import { CustomRepositoryDoesNotHaveEntityError } from "../error/CustomRepositoryDoesNotHaveEntityError"
1✔
8
import { getMetadataArgsStorage } from "../globals"
1✔
9
import { CustomRepositoryNotFoundError } from "../error/CustomRepositoryNotFoundError"
1✔
10
import { SelectQueryBuilder } from "../query-builder/SelectQueryBuilder"
11

12
/**
13
 * Provides abstract class for custom repositories that do not inherit from original orm Repository.
14
 * Contains all most-necessary methods to simplify code in the custom repository.
15
 * All methods are protected thus not exposed and it allows to create encapsulated custom repository.
16
 *
17
 * @deprecated use Repository.extend function to create a custom repository
18
 */
19
export class AbstractRepository<Entity extends ObjectLiteral> {
1✔
20
    // -------------------------------------------------------------------------
21
    // Protected Methods Set Dynamically
22
    // -------------------------------------------------------------------------
23

24
    /**
25
     * Gets entity manager that allows to perform repository operations with any entity.
26
     */
27
    protected manager: EntityManager
28

29
    // -------------------------------------------------------------------------
30
    // Protected Accessors
31
    // -------------------------------------------------------------------------
32

33
    /**
34
     * Gets the original ORM repository for the entity that is managed by this repository.
35
     * If current repository does not manage any entity, then exception will be thrown.
36
     */
37
    protected get repository(): Repository<Entity> {
UNCOV
38
        const target = this.getCustomRepositoryTarget(this as any)
×
UNCOV
39
        if (!target)
×
40
            throw new CustomRepositoryDoesNotHaveEntityError(this.constructor)
×
41

UNCOV
42
        return this.manager.getRepository<Entity>(target)
×
43
    }
44

45
    /**
46
     * Gets the original ORM tree repository for the entity that is managed by this repository.
47
     * If current repository does not manage any entity, then exception will be thrown.
48
     */
49
    protected get treeRepository(): TreeRepository<Entity> {
50
        const target = this.getCustomRepositoryTarget(this as any)
×
51
        if (!target)
×
52
            throw new CustomRepositoryDoesNotHaveEntityError(this.constructor)
×
53

54
        return this.manager.getTreeRepository<Entity>(target)
×
55
    }
56

57
    // -------------------------------------------------------------------------
58
    // Protected Methods
59
    // -------------------------------------------------------------------------
60

61
    /**
62
     * Creates a new query builder for the repository's entity that can be used to build a SQL query.
63
     * If current repository does not manage any entity, then exception will be thrown.
64
     */
65
    protected createQueryBuilder(alias: string): SelectQueryBuilder<Entity> {
66
        const target = this.getCustomRepositoryTarget(this.constructor)
×
67
        if (!target)
×
68
            throw new CustomRepositoryDoesNotHaveEntityError(this.constructor)
×
69

70
        return this.manager
×
71
            .getRepository<Entity>(target)
72
            .createQueryBuilder(alias)
73
    }
74

75
    /**
76
     * Creates a new query builder for the given entity that can be used to build a SQL query.
77
     */
78
    protected createQueryBuilderFor<T extends ObjectLiteral>(
79
        entity: ObjectType<T>,
80
        alias: string,
81
    ): SelectQueryBuilder<T> {
82
        return this.getRepositoryFor(entity).createQueryBuilder(alias)
×
83
    }
84

85
    /**
86
     * Gets the original ORM repository for the given entity class.
87
     */
88
    protected getRepositoryFor<T extends ObjectLiteral>(
89
        entity: ObjectType<T>,
90
    ): Repository<T> {
91
        return this.manager.getRepository(entity)
×
92
    }
93

94
    /**
95
     * Gets the original ORM tree repository for the given entity class.
96
     */
97
    protected getTreeRepositoryFor<T extends ObjectLiteral>(
98
        entity: ObjectType<T>,
99
    ): TreeRepository<T> {
100
        return this.manager.getTreeRepository(entity)
×
101
    }
102

103
    // -------------------------------------------------------------------------
104
    // Private Methods
105
    // -------------------------------------------------------------------------
106

107
    /**
108
     * Gets custom repository's managed entity.
109
     * If given custom repository does not manage any entity then undefined will be returned.
110
     */
111
    private getCustomRepositoryTarget(
112
        customRepository: any,
113
    ): EntityTarget<any> | undefined {
114
        const entityRepositoryMetadataArgs =
UNCOV
115
            getMetadataArgsStorage().entityRepositories.find((repository) => {
×
UNCOV
116
                return (
×
117
                    repository.target ===
118
                    (typeof customRepository === "function"
×
119
                        ? customRepository
120
                        : (customRepository as any).constructor)
121
                )
122
            })
UNCOV
123
        if (!entityRepositoryMetadataArgs)
×
124
            throw new CustomRepositoryNotFoundError(customRepository)
×
125

UNCOV
126
        return entityRepositoryMetadataArgs.entity
×
127
    }
128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc