• 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

91.77
/src/metadata/EntityListenerMetadata.ts
1
import { EventListenerType } from "./types/EventListenerTypes"
26✔
2
import { EntityListenerMetadataArgs } from "../metadata-args/EntityListenerMetadataArgs"
26✔
3
import { ObjectLiteral } from "../common/ObjectLiteral"
26✔
4
import { EntityMetadata } from "./EntityMetadata"
26✔
5
import { EmbeddedMetadata } from "./EmbeddedMetadata"
26✔
6

26✔
7
/**
26✔
8
 * This metadata contains all information about entity's listeners.
26✔
9
 */
26✔
10
export class EntityListenerMetadata {
26✔
11
    // ---------------------------------------------------------------------
26✔
12
    // Properties
26✔
13
    // ---------------------------------------------------------------------
26✔
14

26✔
15
    /**
26✔
16
     * Entity metadata of the listener.
26✔
17
     */
26✔
18
    entityMetadata: EntityMetadata
26✔
19

26✔
20
    /**
26✔
21
     * Embedded metadata of the listener, in the case if listener is in embedded.
26✔
22
     */
26✔
23
    embeddedMetadata?: EmbeddedMetadata
26✔
24

26✔
25
    /**
26✔
26
     * Target class to which metadata is applied.
26✔
27
     * This can be different then entityMetadata.target in the case if listener is in the embedded.
26✔
28
     */
26✔
29
    target: Function | string
26✔
30

26✔
31
    /**
26✔
32
     * Target's property name to which this metadata is applied.
26✔
33
     */
26✔
34
    propertyName: string
26✔
35

26✔
36
    /**
26✔
37
     * The type of the listener.
26✔
38
     */
26✔
39
    type: EventListenerType
26✔
40

26✔
41
    // ---------------------------------------------------------------------
26✔
42
    // Constructor
26✔
43
    // ---------------------------------------------------------------------
26✔
44

26✔
45
    constructor(options: {
26✔
46
        entityMetadata: EntityMetadata
452✔
47
        embeddedMetadata?: EmbeddedMetadata
452✔
48
        args: EntityListenerMetadataArgs
452✔
49
    }) {
452✔
50
        this.entityMetadata = options.entityMetadata
452✔
51
        this.embeddedMetadata = options.embeddedMetadata
452✔
52
        this.target = options.args.target
452✔
53
        this.propertyName = options.args.propertyName
452✔
54
        this.type = options.args.type
452✔
55
    }
452✔
56

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

26✔
61
    /**
26✔
62
     * Checks if entity listener is allowed to be executed on the given entity.
26✔
63
     */
26✔
64
    isAllowed(entity: ObjectLiteral) {
26✔
65
        // todo: create in entity metadata method like isInherited?
544✔
66
        return (
544✔
67
            this.entityMetadata.target === entity.constructor || // todo: .constructor won't work for entity schemas, but there are no entity listeners in schemas since there are no objects, right?
544!
68
            (typeof this.entityMetadata.target === "function" &&
×
69
                entity.constructor.prototype instanceof
×
70
                    this.entityMetadata.target)
×
71
        ) // todo: also need to implement entity schema inheritance
544✔
72
    }
544✔
73

26✔
74
    /**
26✔
75
     * Executes listener method of the given entity.
26✔
76
     */
26✔
77
    execute(entity: ObjectLiteral) {
26✔
78
        // Check if the Embedded Metadata does not exist
544✔
79
        if (!this.embeddedMetadata) {
544✔
80
            // Get the Entity's Method
430✔
81
            const entityMethod = entity[this.propertyName]
430✔
82

430✔
83
            // Check if the Entity Method does not exist
430✔
84
            if (!entityMethod)
430✔
85
                throw new Error(
430!
86
                    `Entity listener method "${this.propertyName}" does not exist in entity "${entity.constructor.name}".`,
×
87
                )
×
88

430✔
89
            // Check if the Entity Method is not a function
430✔
90
            if (typeof entityMethod !== "function")
430✔
91
                throw new Error(
430!
92
                    `Entity listener method "${this.propertyName}" in entity "${
×
93
                        entity.constructor.name
×
94
                    }" must be a function but got "${typeof entityMethod}".`,
×
95
                )
×
96

430✔
97
            // Call and return the Entity Method
430✔
98
            return entityMethod.call(entity)
430✔
99
        }
430✔
100

114✔
101
        // Call the Embedded Method
114✔
102
        this.callEntityEmbeddedMethod(
114✔
103
            entity,
114✔
104
            this.embeddedMetadata.propertyPath.split("."),
114✔
105
        )
114✔
106
    }
114✔
107

26✔
108
    // ---------------------------------------------------------------------
26✔
109
    // Protected Methods
26✔
110
    // ---------------------------------------------------------------------
26✔
111

26✔
112
    /**
26✔
113
     * Calls embedded entity listener method no matter how nested it is.
26✔
114
     */
26✔
115
    protected callEntityEmbeddedMethod(
26✔
116
        entity: ObjectLiteral,
168✔
117
        propertyPaths: string[],
168✔
118
    ): void {
168✔
119
        const propertyPath = propertyPaths.shift()
168✔
120
        if (!propertyPath || !entity[propertyPath]) return
168✔
121

144✔
122
        if (propertyPaths.length === 0) {
168✔
123
            if (Array.isArray(entity[propertyPath])) {
90!
124
                entity[propertyPath].map((embedded: ObjectLiteral) =>
2✔
125
                    embedded[this.propertyName](),
2✔
126
                )
2✔
127
            } else {
90✔
128
                entity[propertyPath][this.propertyName]()
88✔
129
            }
88✔
130
        } else {
168✔
131
            if (entity[propertyPath])
54✔
132
                this.callEntityEmbeddedMethod(
54✔
133
                    entity[propertyPath],
54✔
134
                    propertyPaths,
54✔
135
                )
54✔
136
        }
54✔
137
    }
168✔
138
}
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