1 | import { Connection, ObjectLiteral } from "../";
|
2 | import { RelationMetadata } from "../metadata/RelationMetadata";
|
3 | /**
|
4 | * Loads relation ids for the given entities.
|
5 | */
|
6 | export declare class RelationIdLoader {
|
7 | private connection;
|
8 | constructor(connection: Connection);
|
9 | /**
|
10 | * Loads relation ids of the given entity or entities.
|
11 | */
|
12 | load(target: Function | string, relation: string, entities: ObjectLiteral | ObjectLiteral[], relatedEntities?: ObjectLiteral | ObjectLiteral[]): Promise<any[]>;
|
13 | /**
|
14 | * Loads relation ids of the given entity or entities.
|
15 | */
|
16 | load(relation: RelationMetadata, entities: ObjectLiteral | ObjectLiteral[], relatedEntities?: ObjectLiteral | ObjectLiteral[]): Promise<any[]>;
|
17 | /**
|
18 | * Loads relation ids of the given entities and groups them into the object with parent and children.
|
19 | *
|
20 | * todo: extract this method?
|
21 | */
|
22 | loadManyToManyRelationIdsAndGroup<E1, E2>(relation: RelationMetadata, entitiesOrEntities: E1 | E1[], relatedEntityOrEntities?: E2 | E2[]): Promise<{
|
23 | entity: E1;
|
24 | related?: E2 | E2[];
|
25 | }[]>;
|
26 | /**
|
27 | * Loads relation ids of the given entities and maps them into the given entity property.
|
28 |
|
29 | async loadManyToManyRelationIdsAndMap(
|
30 | relation: RelationMetadata,
|
31 | entityOrEntities: ObjectLiteral|ObjectLiteral[],
|
32 | mapToEntityOrEntities: ObjectLiteral|ObjectLiteral[],
|
33 | propertyName: string
|
34 | ): Promise<void> {
|
35 |
|
36 | const relationIds = await this.loadManyToManyRelationIds(relation, entityOrEntities, mapToEntityOrEntities);
|
37 | const mapToEntities = mapToEntityOrEntities instanceof Array ? mapToEntityOrEntities : [mapToEntityOrEntities];
|
38 | const junctionMetadata = relation.junctionEntityMetadata!;
|
39 | const mainAlias = junctionMetadata.name;
|
40 | const columns = relation.isOwning ? junctionMetadata.inverseColumns : junctionMetadata.ownerColumns;
|
41 | const inverseColumns = relation.isOwning ? junctionMetadata.ownerColumns : junctionMetadata.inverseColumns;
|
42 |
|
43 | mapToEntities.forEach(mapToEntity => {
|
44 | mapToEntity[propertyName] = [];
|
45 | relationIds.forEach(relationId => {
|
46 | const match = inverseColumns.every(column => {
|
47 | return column.referencedColumn!.getEntityValue(mapToEntity) === relationId[mainAlias + "_" + column.propertyName];
|
48 | });
|
49 | if (match) {
|
50 | if (columns.length === 1) {
|
51 | mapToEntity[propertyName].push(relationId[mainAlias + "_" + columns[0].propertyName]);
|
52 |
|
53 | } else {
|
54 | const value = {};
|
55 | columns.forEach(column => {
|
56 | column.referencedColumn!.setEntityValue(value, relationId[mainAlias + "_" + column.propertyName]);
|
57 | });
|
58 | mapToEntity[propertyName].push(value);
|
59 | }
|
60 | }
|
61 | });
|
62 | });
|
63 | }*/
|
64 | /**
|
65 | * Loads relation ids for the many-to-many relation.
|
66 | */
|
67 | protected loadForManyToMany(relation: RelationMetadata, entities: ObjectLiteral[], relatedEntities?: ObjectLiteral[]): Promise<any[]>;
|
68 | /**
|
69 | * Loads relation ids for the many-to-one and one-to-one owner relations.
|
70 | */
|
71 | protected loadForManyToOneAndOneToOneOwner(relation: RelationMetadata, entities: ObjectLiteral[], relatedEntities?: ObjectLiteral[]): Promise<any[]>;
|
72 | /**
|
73 | * Loads relation ids for the one-to-many and one-to-one not owner relations.
|
74 | */
|
75 | protected loadForOneToManyAndOneToOneNotOwner(relation: RelationMetadata, entities: ObjectLiteral[], relatedEntities?: ObjectLiteral[]): Promise<any[]>;
|
76 | }
|