forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlainObjectToDatabaseEntityTransformer.ts
136 lines (110 loc) · 5.24 KB
/
PlainObjectToDatabaseEntityTransformer.ts
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
136
import {ObjectLiteral} from "../../common/ObjectLiteral";
import {EntityMetadata} from "../../metadata/EntityMetadata";
import {EntityManager} from "../../entity-manager/EntityManager";
import {RelationMetadata} from "../../metadata/RelationMetadata";
/**
*/
class LoadMapItem {
entity?: ObjectLiteral;
plainEntity: ObjectLiteral;
metadata: EntityMetadata;
parentLoadMapItem?: LoadMapItem;
relation?: RelationMetadata;
constructor(plainEntity: ObjectLiteral,
metadata: EntityMetadata,
parentLoadMapItem?: LoadMapItem,
relation?: RelationMetadata) {
this.plainEntity = plainEntity;
this.metadata = metadata;
this.parentLoadMapItem = parentLoadMapItem;
this.relation = relation;
}
get target(): Function|string {
return this.metadata.target;
}
get id(): any {
return this.metadata.getEntityIdMixedMap(this.plainEntity);
}
}
class LoadMap {
loadMapItems: LoadMapItem[] = [];
get mainLoadMapItem(): LoadMapItem|undefined {
return this.loadMapItems.find(item => !item.relation && !item.parentLoadMapItem);
}
addLoadMap(newLoadMap: LoadMapItem) {
const item = this.loadMapItems.find(item => item.target === newLoadMap.target && item.id === newLoadMap.id);
if (!item)
this.loadMapItems.push(newLoadMap);
}
fillEntities(target: Function|string, entities: any[]) {
entities.forEach(entity => {
const item = this.loadMapItems.find(loadMapItem => {
return loadMapItem.target === target && loadMapItem.metadata.compareEntities(entity, loadMapItem.plainEntity);
});
if (item)
item.entity = entity;
});
}
groupByTargetIds(): { target: Function|string, ids: any[] }[] {
const groups: { target: Function|string, ids: any[] }[] = [];
this.loadMapItems.forEach(loadMapItem => {
let group = groups.find(group => group.target === loadMapItem.target);
if (!group) {
group = { target: loadMapItem.target, ids: [] };
groups.push(group);
}
group.ids.push(loadMapItem.id);
});
return groups;
}
}
/**
* Transforms plain old javascript object
* Entity is constructed based on its entity metadata.
*/
export class PlainObjectToDatabaseEntityTransformer {
constructor(private manager: EntityManager) {
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
async transform(plainObject: ObjectLiteral, metadata: EntityMetadata): Promise<ObjectLiteral|undefined> {
// if plain object does not have id then nothing to load really
if (!metadata.hasAllPrimaryKeys(plainObject))
return Promise.reject("Given object does not have a primary column, cannot transform it to database entity.");
// create a special load map that will hold all metadata that will be used to operate with entities easily
const loadMap = new LoadMap();
const fillLoadMap = async (entity: ObjectLiteral, entityMetadata: EntityMetadata, parentLoadMapItem?: LoadMapItem, relation?: RelationMetadata) => {
const item = new LoadMapItem(entity, entityMetadata, parentLoadMapItem, relation);
loadMap.addLoadMap(item);
const relationValues = await entityMetadata
.extractRelationValuesFromEntity(entity, metadata.relations);
await Promise.all(relationValues
.filter(value => value !== null && value !== undefined)
.map(async ([relation, value, inverseEntityMetadata]) => await fillLoadMap(value, inverseEntityMetadata, item, relation)));
};
await fillLoadMap(plainObject, metadata);
// load all entities and store them in the load map
await Promise.all(loadMap.groupByTargetIds().map(targetWithIds => { // todo: fix type hinting
return this.manager
.findByIds<ObjectLiteral>(targetWithIds.target as any, targetWithIds.ids)
.then(entities => loadMap.fillEntities(targetWithIds.target, entities));
}));
// go through each item in the load map and set their entity relationship using metadata stored in load map
loadMap.loadMapItems.forEach(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 loadMap.mainLoadMapItem ? loadMap.mainLoadMapItem.entity : undefined;
}
}