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MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC;gBACjE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;aACtB;YACD,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;YACpC,OAAO,MAAM,CAAC;QAClB,CAAC,EAAE,EAAwD,CAAC,CAAC;IACjE,CAAC;IAEL,kCAAC;AAAD,CA5IA,AA4IC,IAAA;AA5IY,kEAA2B","file":"SubjectDatabaseEntityLoader.js","sourcesContent":["import {Subject} from \"./Subject\";\nimport {ObjectLiteral} from \"../common/ObjectLiteral\";\nimport {QueryRunner} from \"../query-runner/QueryRunner\";\nimport {FindManyOptions} from \"../find-options/FindManyOptions\";\n\n/**\n * Loads database entities for all operate subjects which do not have database entity set.\n * All entities that we load database entities for are marked as updated or inserted.\n * To understand which of them really needs to be inserted or updated we need to load\n * their original representations from the database.\n */\nexport class SubjectDatabaseEntityLoader {\n\n // ---------------------------------------------------------------------\n // Constructor\n // ---------------------------------------------------------------------\n\n constructor(protected queryRunner: QueryRunner,\n protected subjects: Subject[]) {\n }\n\n // ---------------------------------------------------------------------\n // Public Methods\n // ---------------------------------------------------------------------\n\n /**\n * Loads database entities for all subjects.\n *\n * loadAllRelations flag is used to load all relation ids of the object, no matter if they present in subject entity or not.\n * This option is used for deletion.\n */\n async load(operationType: \"save\"|\"remove\"): Promise<void> {\n\n // we are grouping subjects by target to perform more optimized queries using WHERE IN operator\n // go through the groups and perform loading of database entities of each subject in the group\n const promises = this.groupByEntityTargets().map(async subjectGroup => {\n\n // prepare entity ids of the subjects we need to load\n const allIds: ObjectLiteral[] = [];\n const allSubjects: Subject[] = [];\n subjectGroup.subjects.forEach(subject => {\n\n // we don't load if subject already has a database entity loaded\n if (subject.databaseEntity || !subject.identifier)\n return;\n\n allIds.push(subject.identifier);\n allSubjects.push(subject);\n });\n\n // if there no ids found (means all entities are new and have generated ids) - then nothing to load there\n if (!allIds.length)\n return;\n\n const loadRelationPropertyPaths: string[] = [];\n\n // for the save operation\n // extract all property paths of the relations we need to load relation ids for\n // this is for optimization purpose - this way we don't load relation ids for entities\n // whose relations are undefined, and since they are undefined its really pointless to\n // load something for them, since undefined properties are skipped by the orm\n if (operationType === \"save\") {\n subjectGroup.subjects.forEach(subject => {\n\n // gets all relation property paths that exist in the persisted entity.\n subject.metadata.relations.forEach(relation => {\n const value = relation.getEntityValue(subject.entityWithFulfilledIds!);\n if (value === undefined)\n return;\n\n if (loadRelationPropertyPaths.indexOf(relation.propertyPath) === -1)\n loadRelationPropertyPaths.push(relation.propertyPath);\n });\n });\n } else { // remove\n\n // for remove operation\n // we only need to load junction relation ids since only they are removed by cascades\n loadRelationPropertyPaths.push(...subjectGroup.subjects[0].metadata.manyToManyRelations.map(relation => relation.propertyPath));\n }\n\n const findOptions: FindManyOptions<any> = {\n loadEagerRelations: false,\n loadRelationIds: {\n relations: loadRelationPropertyPaths,\n disableMixedMap: true\n }\n };\n\n // load database entities for all given ids\n const entities = await this.queryRunner.manager\n .getRepository<ObjectLiteral>(subjectGroup.target)\n .findByIds(allIds, findOptions);\n\n // now when we have entities we need to find subject of each entity\n // and insert that entity into database entity of the found subjects\n entities.forEach(entity => {\n const subjects = this.findByPersistEntityLike(subjectGroup.target, entity);\n subjects.forEach(subject => {\n subject.databaseEntity = entity;\n if (!subject.identifier)\n subject.identifier = subject.metadata.hasAllPrimaryKeys(entity) ? subject.metadata.getEntityIdMap(entity) : undefined;\n });\n });\n\n // this way we tell what subjects we tried to load database entities of\n for (let subject of allSubjects) {\n subject.databaseEntityLoaded = true;\n }\n });\n\n await Promise.all(promises);\n }\n\n // ---------------------------------------------------------------------\n // Protected Methods\n // ---------------------------------------------------------------------\n\n /**\n * Finds subjects where entity like given subject's entity.\n * Comparision made by entity id.\n * Multiple subjects may be returned if duplicates are present in the subject array.\n * This will likely result in the same row being updated multiple times during a transaction.\n */\n protected findByPersistEntityLike(entityTarget: Function|string, entity: ObjectLiteral): Subject[] {\n return this.subjects.filter(subject => {\n if (!subject.entity)\n return false;\n\n if (subject.entity === entity)\n return true;\n\n return subject.metadata.target === entityTarget && subject.metadata.compareEntities(subject.entityWithFulfilledIds!, entity);\n });\n }\n\n /**\n * Groups given Subject objects into groups separated by entity targets.\n */\n protected groupByEntityTargets(): { target: Function|string, subjects: Subject[] }[] {\n return this.subjects.reduce((groups, operatedEntity) => {\n let group = groups.find(group => group.target === operatedEntity.metadata.target);\n if (!group) {\n group = { target: operatedEntity.metadata.target, subjects: [] };\n groups.push(group);\n }\n group.subjects.push(operatedEntity);\n return groups;\n }, [] as { target: Function|string, subjects: Subject[] }[]);\n }\n\n}\n"],"sourceRoot":".."}
\No newline at end of file