333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Subject_1 = require("../Subject");
/**
 * Finds all cascade operations of the given subject and cascade operations of the found cascaded subjects,
 * e.g. builds a cascade tree and creates a subjects for them.
 */
var CascadesSubjectBuilder = /** @class */ (function () {
    // ---------------------------------------------------------------------
    // Constructor
    // ---------------------------------------------------------------------
    function CascadesSubjectBuilder(allSubjects) {
        this.allSubjects = allSubjects;
    }
    // ---------------------------------------------------------------------
    // Public Methods
    // ---------------------------------------------------------------------
    /**
     * Builds a cascade subjects tree and pushes them in into the given array of subjects.
     */
    CascadesSubjectBuilder.prototype.build = function (subject) {
        var _this = this;
        subject.metadata
            .extractRelationValuesFromEntity(subject.entity, subject.metadata.relations) // todo: we can create EntityMetadata.cascadeRelations
            .forEach(function (_a) {
            var _b = tslib_1.__read(_a, 3), relation = _b[0], relationEntity = _b[1], relationEntityMetadata = _b[2];
            // we need only defined values and insert or update cascades of the relation should be set
            if (relationEntity === undefined ||
                relationEntity === null ||
                (!relation.isCascadeInsert && !relation.isCascadeUpdate))
                return;
            // if relation entity is just a relation id set (for example post.tag = 1)
            // then we don't really need to check cascades since there is no object to insert or update
            if (!(relationEntity instanceof Object))
                return;
            // if we already has this entity in list of operated subjects then skip it to avoid recursion
            var alreadyExistRelationEntitySubject = _this.findByPersistEntityLike(relationEntityMetadata.target, relationEntity);
            if (alreadyExistRelationEntitySubject) {
                if (alreadyExistRelationEntitySubject.canBeInserted === false) // if its not marked for insertion yet
                    alreadyExistRelationEntitySubject.canBeInserted = relation.isCascadeInsert === true;
                if (alreadyExistRelationEntitySubject.canBeUpdated === false) // if its not marked for update yet
                    alreadyExistRelationEntitySubject.canBeUpdated = relation.isCascadeUpdate === true;
                return;
            }
            // mark subject with what we can do with it
            // and add to the array of subjects to load only if there is no same entity there already
            var relationEntitySubject = new Subject_1.Subject({
                metadata: relationEntityMetadata,
                parentSubject: subject,
                entity: relationEntity,
                canBeInserted: relation.isCascadeInsert === true,
                canBeUpdated: relation.isCascadeUpdate === true
            });
            _this.allSubjects.push(relationEntitySubject);
            // go recursively and find other entities we need to insert/update
            _this.build(relationEntitySubject);
        });
    };
    // ---------------------------------------------------------------------
    // Protected Methods
    // ---------------------------------------------------------------------
    /**
     * Finds subject where entity like given subject's entity.
     * Comparision made by entity id.
     */
    CascadesSubjectBuilder.prototype.findByPersistEntityLike = function (entityTarget, entity) {
        return this.allSubjects.find(function (subject) {
            if (!subject.entity)
                return false;
            if (subject.entity === entity)
                return true;
            return subject.metadata.target === entityTarget && subject.metadata.compareEntities(subject.entityWithFulfilledIds, entity);
        });
    };
    return CascadesSubjectBuilder;
}());
exports.CascadesSubjectBuilder = CascadesSubjectBuilder;
 
//# sourceMappingURL=CascadesSubjectBuilder.js.map