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

typeorm / typeorm / 14796576772

02 May 2025 01:52PM UTC coverage: 45.367% (-30.9%) from 76.309%
14796576772

Pull #11434

github

web-flow
Merge ec4ce2d00 into fadad1a74
Pull Request #11434: feat: release PR releases using pkg.pr.new

5216 of 12761 branches covered (40.87%)

Branch coverage included in aggregate %.

11439 of 23951 relevant lines covered (47.76%)

15712.55 hits per line

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

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

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

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

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

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

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

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

41
    // ---------------------------------------------------------------------
42
    // Constructor
43
    // ---------------------------------------------------------------------
44

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

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

61
    /**
62
     * Checks if entity listener is allowed to be executed on the given entity.
63
     */
64
    isAllowed(entity: ObjectLiteral) {
65
        // todo: create in entity metadata method like isInherited?
66
        return (
64✔
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?
64!
68
            (typeof this.entityMetadata.target === "function" &&
69
                entity.constructor.prototype instanceof
70
                    this.entityMetadata.target)
71
        ) // todo: also need to implement entity schema inheritance
72
    }
73

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

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

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

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

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

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

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

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