• 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

2.04
/src/util/TreeRepositoryUtils.ts
1
import { EntityManager } from "../entity-manager/EntityManager"
2
import { EntityMetadata } from "../metadata/EntityMetadata"
3
import { FindTreesOptions } from "../repository/FindTreesOptions"
4

5
/**
6
 * Provides utilities for manipulating tree structures.
7
 *
8
 */
9
export class TreeRepositoryUtils {
1✔
10
    // -------------------------------------------------------------------------
11
    // Public Methods
12
    // -------------------------------------------------------------------------
13

14
    static createRelationMaps(
15
        manager: EntityManager,
16
        metadata: EntityMetadata,
17
        alias: string,
18
        rawResults: any[],
19
    ): { id: any; parentId: any }[] {
UNCOV
20
        return rawResults.map((rawResult) => {
×
UNCOV
21
            const joinColumn = metadata.treeParentRelation!.joinColumns[0]
×
22
            const referencedColumn =
UNCOV
23
                joinColumn.referencedColumn ?? metadata.primaryColumns[0]
×
24
            // fixes issue #2518, default to databaseName property when givenDatabaseName is not set
25
            const joinColumnName =
UNCOV
26
                joinColumn.givenDatabaseName || joinColumn.databaseName
×
27
            const referencedColumnName =
UNCOV
28
                referencedColumn.givenDatabaseName ||
×
29
                referencedColumn.databaseName
UNCOV
30
            const id = rawResult[alias + "_" + referencedColumnName]
×
UNCOV
31
            const parentId = rawResult[alias + "_" + joinColumnName]
×
UNCOV
32
            return {
×
33
                id: manager.connection.driver.prepareHydratedValue(
34
                    id,
35
                    referencedColumn,
36
                ),
37
                parentId: manager.connection.driver.prepareHydratedValue(
38
                    parentId,
39
                    joinColumn,
40
                ),
41
            }
42
        })
43
    }
44

45
    static buildChildrenEntityTree(
46
        metadata: EntityMetadata,
47
        entity: any,
48
        entities: any[],
49
        relationMaps: { id: any; parentId: any }[],
50
        options: FindTreesOptions & { depth: number },
51
    ): void {
UNCOV
52
        const childProperty = metadata.treeChildrenRelation!.propertyName
×
UNCOV
53
        if (options.depth === 0) {
×
UNCOV
54
            entity[childProperty] = []
×
UNCOV
55
            return
×
56
        }
UNCOV
57
        const joinColumn = metadata.treeParentRelation!.joinColumns[0]
×
58
        const referencedColumn =
UNCOV
59
            joinColumn.referencedColumn ?? metadata.primaryColumns[0]
×
UNCOV
60
        const parentEntityId = referencedColumn.getEntityValue(entity)
×
UNCOV
61
        const childRelationMaps = relationMaps.filter(
×
UNCOV
62
            (relationMap) => relationMap.parentId === parentEntityId,
×
63
        )
UNCOV
64
        const childIds = new Set(
×
UNCOV
65
            childRelationMaps.map((relationMap) => relationMap.id),
×
66
        )
UNCOV
67
        entity[childProperty] = entities.filter((entity) =>
×
UNCOV
68
            childIds.has(referencedColumn.getEntityValue(entity)),
×
69
        )
UNCOV
70
        entity[childProperty].forEach((childEntity: any) => {
×
UNCOV
71
            TreeRepositoryUtils.buildChildrenEntityTree(
×
72
                metadata,
73
                childEntity,
74
                entities,
75
                relationMaps,
76
                {
77
                    ...options,
78
                    depth: options.depth - 1,
79
                },
80
            )
81
        })
82
    }
83

84
    static buildParentEntityTree(
85
        metadata: EntityMetadata,
86
        entity: any,
87
        entities: any[],
88
        relationMaps: { id: any; parentId: any }[],
89
    ): void {
UNCOV
90
        const parentProperty = metadata.treeParentRelation!.propertyName
×
UNCOV
91
        const joinColumn = metadata.treeParentRelation!.joinColumns[0]
×
92
        const referencedColumn =
UNCOV
93
            joinColumn.referencedColumn ?? metadata.primaryColumns[0]
×
UNCOV
94
        const entityId = referencedColumn.getEntityValue(entity)
×
UNCOV
95
        const parentRelationMap = relationMaps.find(
×
UNCOV
96
            (relationMap) => relationMap.id === entityId,
×
97
        )
UNCOV
98
        const parentEntity = entities.find((entity) => {
×
UNCOV
99
            if (!parentRelationMap) return false
×
100

UNCOV
101
            return (
×
102
                referencedColumn.getEntityValue(entity) ===
103
                parentRelationMap.parentId
104
            )
105
        })
UNCOV
106
        if (parentEntity) {
×
UNCOV
107
            entity[parentProperty] = parentEntity
×
UNCOV
108
            TreeRepositoryUtils.buildParentEntityTree(
×
109
                metadata,
110
                entity[parentProperty],
111
                entities,
112
                relationMaps,
113
            )
114
        }
115
    }
116
}
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