• 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

8.47
/src/persistence/subject-builder/CascadesSubjectBuilder.ts
1
import { Subject } from "../Subject"
1✔
2
import { ObjectLiteral } from "../../common/ObjectLiteral"
3
import { ObjectUtils } from "../../util/ObjectUtils"
1✔
4

5
/**
6
 * Finds all cascade operations of the given subject and cascade operations of the found cascaded subjects,
7
 * e.g. builds a cascade tree and creates a subjects for them.
8
 */
9
export class CascadesSubjectBuilder {
1✔
10
    // ---------------------------------------------------------------------
11
    // Constructor
12
    // ---------------------------------------------------------------------
13

14
    constructor(protected allSubjects: Subject[]) {}
111✔
15

16
    // ---------------------------------------------------------------------
17
    // Public Methods
18
    // ---------------------------------------------------------------------
19

20
    /**
21
     * Builds a cascade subjects tree and pushes them in into the given array of subjects.
22
     */
23
    build(
24
        subject: Subject,
25
        operationType: "save" | "remove" | "soft-remove" | "recover",
26
    ) {
27
        subject.metadata
271✔
28
            .extractRelationValuesFromEntity(
29
                subject.entity!,
30
                subject.metadata.relations,
31
            ) // todo: we can create EntityMetadata.cascadeRelations
32
            .forEach(([relation, relationEntity, relationEntityMetadata]) => {
33
                // we need only defined values and insert, update, soft-remove or recover cascades of the relation should be set
UNCOV
34
                if (
×
35
                    relationEntity === undefined ||
×
36
                    relationEntity === null ||
37
                    (!relation.isCascadeInsert &&
38
                        !relation.isCascadeUpdate &&
39
                        !relation.isCascadeSoftRemove &&
40
                        !relation.isCascadeRecover)
41
                )
UNCOV
42
                    return
×
43

44
                // if relation entity is just a relation id set (for example post.tag = 1)
45
                // then we don't really need to check cascades since there is no object to insert or update
UNCOV
46
                if (!ObjectUtils.isObject(relationEntity)) return
×
47

48
                // if we already has this entity in list of operated subjects then skip it to avoid recursion
49
                const alreadyExistRelationEntitySubject =
UNCOV
50
                    this.findByPersistEntityLike(
×
51
                        relationEntityMetadata.target,
52
                        relationEntity,
53
                    )
UNCOV
54
                if (alreadyExistRelationEntitySubject) {
×
UNCOV
55
                    if (
×
56
                        alreadyExistRelationEntitySubject.canBeInserted ===
57
                        false
58
                    )
59
                        // if its not marked for insertion yet
UNCOV
60
                        alreadyExistRelationEntitySubject.canBeInserted =
×
61
                            relation.isCascadeInsert === true &&
×
62
                            operationType === "save"
UNCOV
63
                    if (
×
64
                        alreadyExistRelationEntitySubject.canBeUpdated === false
65
                    )
66
                        // if its not marked for update yet
UNCOV
67
                        alreadyExistRelationEntitySubject.canBeUpdated =
×
68
                            relation.isCascadeUpdate === true &&
×
69
                            operationType === "save"
UNCOV
70
                    if (
×
71
                        alreadyExistRelationEntitySubject.canBeSoftRemoved ===
72
                        false
73
                    )
74
                        // if its not marked for removal yet
UNCOV
75
                        alreadyExistRelationEntitySubject.canBeSoftRemoved =
×
76
                            relation.isCascadeSoftRemove === true &&
×
77
                            operationType === "soft-remove"
UNCOV
78
                    if (
×
79
                        alreadyExistRelationEntitySubject.canBeRecovered ===
80
                        false
81
                    )
82
                        // if its not marked for recovery yet
UNCOV
83
                        alreadyExistRelationEntitySubject.canBeRecovered =
×
84
                            relation.isCascadeRecover === true &&
×
85
                            operationType === "recover"
UNCOV
86
                    return
×
87
                }
88

89
                // mark subject with what we can do with it
90
                // and add to the array of subjects to load only if there is no same entity there already
UNCOV
91
                const relationEntitySubject = new Subject({
×
92
                    metadata: relationEntityMetadata,
93
                    parentSubject: subject,
94
                    entity: relationEntity,
95
                    canBeInserted:
96
                        relation.isCascadeInsert === true &&
×
97
                        operationType === "save",
98
                    canBeUpdated:
99
                        relation.isCascadeUpdate === true &&
×
100
                        operationType === "save",
101
                    canBeSoftRemoved:
102
                        relation.isCascadeSoftRemove === true &&
×
103
                        operationType === "soft-remove",
104
                    canBeRecovered:
105
                        relation.isCascadeRecover === true &&
×
106
                        operationType === "recover",
107
                })
UNCOV
108
                this.allSubjects.push(relationEntitySubject)
×
109

110
                // go recursively and find other entities we need to insert/update
UNCOV
111
                this.build(relationEntitySubject, operationType)
×
112
            })
113
    }
114

115
    // ---------------------------------------------------------------------
116
    // Protected Methods
117
    // ---------------------------------------------------------------------
118

119
    /**
120
     * Finds subject where entity like given subject's entity.
121
     * Comparison made by entity id.
122
     */
123
    protected findByPersistEntityLike(
124
        entityTarget: Function | string,
125
        entity: ObjectLiteral,
126
    ): Subject | undefined {
UNCOV
127
        return this.allSubjects.find((subject) => {
×
UNCOV
128
            if (!subject.entity) return false
×
129

UNCOV
130
            if (subject.entity === entity) return true
×
131

UNCOV
132
            return (
×
133
                subject.metadata.target === entityTarget &&
×
134
                subject.metadata.compareEntities(
135
                    subject.entityWithFulfilledIds!,
136
                    entity,
137
                )
138
            )
139
        })
140
    }
141
}
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