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

typeorm / typeorm / 19549987525

20 Nov 2025 08:11PM UTC coverage: 80.769% (+4.3%) from 76.433%
19549987525

push

github

web-flow
ci: run tests on commits to master and next (#11783)

Co-authored-by: Oleg "OSA413" Sokolov <OSA413@users.noreply.github.com>

26500 of 32174 branches covered (82.36%)

Branch coverage included in aggregate %.

91252 of 113615 relevant lines covered (80.32%)

88980.79 hits per line

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

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

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

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

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

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

8✔
42
        return this.manager.getRepository<Entity>(target)
8✔
43
    }
8✔
44

26✔
45
    /**
26✔
46
     * Gets the original ORM tree repository for the entity that is managed by this repository.
26✔
47
     * If current repository does not manage any entity, then exception will be thrown.
26✔
48
     */
26✔
49
    protected get treeRepository(): TreeRepository<Entity> {
26✔
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

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

26✔
61
    /**
26✔
62
     * Creates a new query builder for the repository's entity that can be used to build a SQL query.
26✔
63
     * If current repository does not manage any entity, then exception will be thrown.
26✔
64
     */
26✔
65
    protected createQueryBuilder(alias: string): SelectQueryBuilder<Entity> {
26✔
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

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

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

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

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

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

8✔
126
        return entityRepositoryMetadataArgs.entity
8✔
127
    }
8✔
128
}
26✔
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

© 2026 Coveralls, Inc