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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as tslib_1 from "tslib";
/**
 */
var LoadMapItem = /** @class */ (function () {
    function LoadMapItem(plainEntity, metadata, parentLoadMapItem, relation) {
        this.plainEntity = plainEntity;
        this.metadata = metadata;
        this.parentLoadMapItem = parentLoadMapItem;
        this.relation = relation;
    }
    Object.defineProperty(LoadMapItem.prototype, "target", {
        get: function () {
            return this.metadata.target;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadMapItem.prototype, "id", {
        get: function () {
            return this.metadata.getEntityIdMixedMap(this.plainEntity);
        },
        enumerable: true,
        configurable: true
    });
    return LoadMapItem;
}());
var LoadMap = /** @class */ (function () {
    function LoadMap() {
        this.loadMapItems = [];
    }
    Object.defineProperty(LoadMap.prototype, "mainLoadMapItem", {
        get: function () {
            return this.loadMapItems.find(function (item) { return !item.relation && !item.parentLoadMapItem; });
        },
        enumerable: true,
        configurable: true
    });
    LoadMap.prototype.addLoadMap = function (newLoadMap) {
        var item = this.loadMapItems.find(function (item) { return item.target === newLoadMap.target && item.id === newLoadMap.id; });
        if (!item)
            this.loadMapItems.push(newLoadMap);
    };
    LoadMap.prototype.fillEntities = function (target, entities) {
        var _this = this;
        entities.forEach(function (entity) {
            var item = _this.loadMapItems.find(function (loadMapItem) {
                return loadMapItem.target === target && loadMapItem.metadata.compareEntities(entity, loadMapItem.plainEntity);
            });
            if (item)
                item.entity = entity;
        });
    };
    LoadMap.prototype.groupByTargetIds = function () {
        var groups = [];
        this.loadMapItems.forEach(function (loadMapItem) {
            var group = groups.find(function (group) { return group.target === loadMapItem.target; });
            if (!group) {
                group = { target: loadMapItem.target, ids: [] };
                groups.push(group);
            }
            group.ids.push(loadMapItem.id);
        });
        return groups;
    };
    return LoadMap;
}());
/**
 * Transforms plain old javascript object
 * Entity is constructed based on its entity metadata.
 */
var PlainObjectToDatabaseEntityTransformer = /** @class */ (function () {
    function PlainObjectToDatabaseEntityTransformer(manager) {
        this.manager = manager;
    }
    // -------------------------------------------------------------------------
    // Public Methods
    // -------------------------------------------------------------------------
    PlainObjectToDatabaseEntityTransformer.prototype.transform = function (plainObject, metadata) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var loadMap, fillLoadMap;
            var _this = this;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        // if plain object does not have id then nothing to load really
                        if (!metadata.hasAllPrimaryKeys(plainObject))
                            return [2 /*return*/, Promise.reject("Given object does not have a primary column, cannot transform it to database entity.")];
                        loadMap = new LoadMap();
                        fillLoadMap = function (entity, entityMetadata, parentLoadMapItem, relation) {
                            var item = new LoadMapItem(entity, entityMetadata, parentLoadMapItem, relation);
                            loadMap.addLoadMap(item);
                            entityMetadata
                                .extractRelationValuesFromEntity(entity, metadata.relations)
                                .filter(function (value) { return value !== null && value !== undefined; })
                                .forEach(function (_a) {
                                var _b = tslib_1.__read(_a, 3), relation = _b[0], value = _b[1], inverseEntityMetadata = _b[2];
                                return fillLoadMap(value, inverseEntityMetadata, item, relation);
                            });
                        };
                        fillLoadMap(plainObject, metadata);
                        // load all entities and store them in the load map
                        return [4 /*yield*/, Promise.all(loadMap.groupByTargetIds().map(function (targetWithIds) {
                                return _this.manager
                                    .findByIds(targetWithIds.target, targetWithIds.ids)
                                    .then(function (entities) { return loadMap.fillEntities(targetWithIds.target, entities); });
                            }))];
                    case 1:
                        // load all entities and store them in the load map
                        _a.sent();
                        // go through each item in the load map and set their entity relationship using metadata stored in load map
                        loadMap.loadMapItems.forEach(function (loadMapItem) {
                            if (!loadMapItem.relation ||
                                !loadMapItem.entity ||
                                !loadMapItem.parentLoadMapItem ||
                                !loadMapItem.parentLoadMapItem.entity)
                                return;
                            if (loadMapItem.relation.isManyToMany || loadMapItem.relation.isOneToMany) {
                                if (!loadMapItem.parentLoadMapItem.entity[loadMapItem.relation.propertyName])
                                    loadMapItem.parentLoadMapItem.entity[loadMapItem.relation.propertyName] = [];
                                loadMapItem.parentLoadMapItem.entity[loadMapItem.relation.propertyName].push(loadMapItem.entity);
                            }
                            else {
                                loadMapItem.parentLoadMapItem.entity[loadMapItem.relation.propertyName] = loadMapItem.entity;
                            }
                        });
                        return [2 /*return*/, loadMap.mainLoadMapItem ? loadMap.mainLoadMapItem.entity : undefined];
                }
            });
        });
    };
    return PlainObjectToDatabaseEntityTransformer;
}());
export { PlainObjectToDatabaseEntityTransformer };
 
//# sourceMappingURL=PlainObjectToDatabaseEntityTransformer.js.map