typeorm 0.3.24-dev.15de733 → 0.3.24-dev.2168441

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  import { Subject } from "./Subject";
2
+ import { ObjectLiteral } from "../common/ObjectLiteral";
2
3
  import { QueryRunner } from "../query-runner/QueryRunner";
3
4
  /**
4
5
  * Loads database entities for all operate subjects which do not have database entity set.
@@ -17,6 +18,13 @@ export declare class SubjectDatabaseEntityLoader {
17
18
  * This option is used for deletion.
18
19
  */
19
20
  load(operationType: "save" | "remove" | "soft-remove" | "recover"): Promise<void>;
21
+ /**
22
+ * Finds subjects where entity like given subject's entity.
23
+ * Comparison made by entity id.
24
+ * Multiple subjects may be returned if duplicates are present in the subject array.
25
+ * This will likely result in the same row being updated multiple times during a transaction.
26
+ */
27
+ protected findByPersistEntityLike(entityTarget: Function | string, entity: ObjectLiteral): Subject[];
20
28
  /**
21
29
  * Groups given Subject objects into groups separated by entity targets.
22
30
  */
@@ -1,4 +1,3 @@
1
- import { OrmUtils } from "../util/OrmUtils";
2
1
  /**
3
2
  * Loads database entities for all operate subjects which do not have database entity set.
4
3
  * All entities that we load database entities for are marked as updated or inserted.
@@ -89,17 +88,17 @@ export class SubjectDatabaseEntityLoader {
89
88
  .whereInIds(allIds)
90
89
  .getMany();
91
90
  }
92
- // Now when we have entities we need to find subject of each entity
93
- // and insert that entity into database entity of the found subjects.
94
- // A single entity can be applied to many subjects as there might be duplicates.
95
- // This will likely result in the same row being updated multiple times during a transaction.
91
+ // now when we have entities we need to find subject of each entity
92
+ // and insert that entity into database entity of the found subjects
96
93
  entities.forEach((entity) => {
97
- const entityId = allSubjects[0].metadata.getEntityIdMap(entity);
98
- allSubjects.forEach((subject) => {
99
- if (subject.databaseEntity)
100
- return;
101
- if (OrmUtils.compareIds(subject.identifier, entityId))
102
- subject.databaseEntity = entity;
94
+ const subjects = this.findByPersistEntityLike(subjectGroup.target, entity);
95
+ subjects.forEach((subject) => {
96
+ subject.databaseEntity = entity;
97
+ if (!subject.identifier)
98
+ subject.identifier =
99
+ subject.metadata.hasAllPrimaryKeys(entity)
100
+ ? subject.metadata.getEntityIdMap(entity)
101
+ : undefined;
103
102
  });
104
103
  });
105
104
  // this way we tell what subjects we tried to load database entities of
@@ -112,6 +111,22 @@ export class SubjectDatabaseEntityLoader {
112
111
  // ---------------------------------------------------------------------
113
112
  // Protected Methods
114
113
  // ---------------------------------------------------------------------
114
+ /**
115
+ * Finds subjects where entity like given subject's entity.
116
+ * Comparison made by entity id.
117
+ * Multiple subjects may be returned if duplicates are present in the subject array.
118
+ * This will likely result in the same row being updated multiple times during a transaction.
119
+ */
120
+ findByPersistEntityLike(entityTarget, entity) {
121
+ return this.subjects.filter((subject) => {
122
+ if (!subject.entity)
123
+ return false;
124
+ if (subject.entity === entity)
125
+ return true;
126
+ return (subject.metadata.target === entityTarget &&
127
+ subject.metadata.compareEntities(subject.entityWithFulfilledIds, entity));
128
+ });
129
+ }
115
130
  /**
116
131
  * Groups given Subject objects into groups separated by entity targets.
117
132
  */
@@ -1 +1 @@
1
- {"version":3,"sources":["../browser/src/persistence/SubjectDatabaseEntityLoader.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAE3C;;;;;GAKG;AACH,MAAM,OAAO,2BAA2B;IACpC,wEAAwE;IACxE,cAAc;IACd,wEAAwE;IAExE,YACc,WAAwB,EACxB,QAAmB;QADnB,gBAAW,GAAX,WAAW,CAAa;QACxB,aAAQ,GAAR,QAAQ,CAAW;IAC9B,CAAC;IAEJ,wEAAwE;IACxE,iBAAiB;IACjB,wEAAwE;IAExE;;;;;OAKG;IACH,KAAK,CAAC,IAAI,CACN,aAA4D;QAE5D,+FAA+F;QAC/F,8FAA8F;QAC9F,MAAM,QAAQ,GAAG,IAAI,CAAC,oBAAoB,EAAE,CAAC,GAAG,CAC5C,KAAK,EAAE,YAAY,EAAE,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B,IAAI,OAAO,CAAC,cAAc;wBAAE,OAAM;oBAClC,IAAI,QAAQ,CAAC,UAAU,CAAC,OAAO,CAAC,UAAU,EAAE,QAAQ,CAAC;wBACjD,OAAO,CAAC,cAAc,GAAG,MAAM,CAAA;gBACvC,CAAC,CAAC,CAAA;YACN,CAAC,CAAC,CAAA;YAEF,uEAAuE;YACvE,KAAK,MAAM,OAAO,IAAI,WAAW,EAAE,CAAC;gBAChC,OAAO,CAAC,oBAAoB,GAAG,IAAI,CAAA;YACvC,CAAC;QACL,CAAC,CACJ,CAAA;QAED,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IAC/B,CAAC;IAED,wEAAwE;IACxE,oBAAoB;IACpB,wEAAwE;IAExE;;OAEG;IACO,oBAAoB;QAI1B,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,cAAc,EAAE,EAAE;YACnD,IAAI,KAAK,GAAG,MAAM,CAAC,IAAI,CACnB,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,KAAK,cAAc,CAAC,QAAQ,CAAC,MAAM,CAC7D,CAAA;YACD,IAAI,CAAC,KAAK,EAAE,CAAC;gBACT,KAAK,GAAG,EAAE,MAAM,EAAE,cAAc,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAA;gBAChE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YACtB,CAAC;YACD,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;YACnC,OAAO,MAAM,CAAA;QACjB,CAAC,EAAE,EAA0D,CAAC,CAAA;IAClE,CAAC;CACJ","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\"\nimport { MongoRepository } from \"../repository/MongoRepository\"\nimport { OrmUtils } from \"../util/OrmUtils\"\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 // Constructor\n // ---------------------------------------------------------------------\n\n constructor(\n 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(\n operationType: \"save\" | \"remove\" | \"soft-remove\" | \"recover\",\n ): Promise<void> {\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(\n async (subjectGroup) => {\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 // we don't load if subject already has a database entity loaded\n if (subject.databaseEntity || !subject.identifier) 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) return\n\n const loadRelationPropertyPaths: string[] = []\n\n // for the save, soft-remove and recover 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 (\n operationType === \"save\" ||\n operationType === \"soft-remove\" ||\n operationType === \"recover\"\n ) {\n subjectGroup.subjects.forEach((subject) => {\n // gets all relation property paths that exist in the persisted entity.\n subject.metadata.relations.forEach((relation) => {\n const value = relation.getEntityValue(\n subject.entityWithFulfilledIds!,\n )\n if (value === undefined) return\n\n if (\n loadRelationPropertyPaths.indexOf(\n relation.propertyPath,\n ) === -1\n )\n loadRelationPropertyPaths.push(\n relation.propertyPath,\n )\n })\n })\n } else {\n // 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(\n ...subjectGroup.subjects[0].metadata.manyToManyRelations.map(\n (relation) => relation.propertyPath,\n ),\n )\n }\n\n const findOptions: FindManyOptions<any> = {\n loadEagerRelations: false,\n loadRelationIds: {\n relations: loadRelationPropertyPaths,\n disableMixedMap: true,\n },\n // the soft-deleted entities should be included in the loaded entities for recover operation\n withDeleted: true,\n }\n\n // load database entities for all given ids\n let entities: any[] = []\n if (\n this.queryRunner.connection.driver.options.type ===\n \"mongodb\"\n ) {\n const mongoRepo =\n this.queryRunner.manager.getRepository<ObjectLiteral>(\n subjectGroup.target,\n ) as MongoRepository<ObjectLiteral>\n entities = await mongoRepo.findByIds(allIds, findOptions)\n } else {\n entities = await this.queryRunner.manager\n .getRepository<ObjectLiteral>(subjectGroup.target)\n .createQueryBuilder()\n .setFindOptions(findOptions)\n .whereInIds(allIds)\n .getMany()\n }\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 // A single entity can be applied to many subjects as there might be duplicates.\n // This will likely result in the same row being updated multiple times during a transaction.\n entities.forEach((entity) => {\n const entityId =\n allSubjects[0].metadata.getEntityIdMap(entity)\n allSubjects.forEach((subject) => {\n if (subject.databaseEntity) return\n if (OrmUtils.compareIds(subject.identifier, entityId))\n subject.databaseEntity = entity\n })\n })\n\n // this way we tell what subjects we tried to load database entities of\n for (const subject of allSubjects) {\n subject.databaseEntityLoaded = true\n }\n },\n )\n\n await Promise.all(promises)\n }\n\n // ---------------------------------------------------------------------\n // Protected Methods\n // ---------------------------------------------------------------------\n\n /**\n * Groups given Subject objects into groups separated by entity targets.\n */\n protected groupByEntityTargets(): {\n target: Function | string\n subjects: Subject[]\n }[] {\n return this.subjects.reduce((groups, operatedEntity) => {\n let group = groups.find(\n (group) => group.target === operatedEntity.metadata.target,\n )\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"],"sourceRoot":".."}
1
+ {"version":3,"sources":["../browser/src/persistence/SubjectDatabaseEntityLoader.ts"],"names":[],"mappings":"AAMA;;;;;GAKG;AACH,MAAM,OAAO,2BAA2B;IACpC,wEAAwE;IACxE,cAAc;IACd,wEAAwE;IAExE,YACc,WAAwB,EACxB,QAAmB;QADnB,gBAAW,GAAX,WAAW,CAAa;QACxB,aAAQ,GAAR,QAAQ,CAAW;IAC9B,CAAC;IAEJ,wEAAwE;IACxE,iBAAiB;IACjB,wEAAwE;IAExE;;;;;OAKG;IACH,KAAK,CAAC,IAAI,CACN,aAA4D;QAE5D,+FAA+F;QAC/F,8FAA8F;QAC9F,MAAM,QAAQ,GAAG,IAAI,CAAC,oBAAoB,EAAE,CAAC,GAAG,CAC5C,KAAK,EAAE,YAAY,EAAE,EAAE;YACnB,qDAAqD;YACrD,MAAM,MAAM,GAAoB,EAAE,CAAA;YAClC,MAAM,WAAW,GAAc,EAAE,CAAA;YACjC,YAAY,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;gBACtC,gEAAgE;gBAChE,IAAI,OAAO,CAAC,cAAc,IAAI,CAAC,OAAO,CAAC,UAAU;oBAAE,OAAM;gBAEzD,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;gBAC/B,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;YAC7B,CAAC,CAAC,CAAA;YAEF,yGAAyG;YACzG,IAAI,CAAC,MAAM,CAAC,MAAM;gBAAE,OAAM;YAE1B,MAAM,yBAAyB,GAAa,EAAE,CAAA;YAE9C,kDAAkD;YAClD,+EAA+E;YAC/E,sFAAsF;YACtF,sFAAsF;YACtF,6EAA6E;YAC7E,IACI,aAAa,KAAK,MAAM;gBACx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CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE;YACpC,IAAI,CAAC,OAAO,CAAC,MAAM;gBAAE,OAAO,KAAK,CAAA;YAEjC,IAAI,OAAO,CAAC,MAAM,KAAK,MAAM;gBAAE,OAAO,IAAI,CAAA;YAE1C,OAAO,CACH,OAAO,CAAC,QAAQ,CAAC,MAAM,KAAK,YAAY;gBACxC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAC5B,OAAO,CAAC,sBAAuB,EAC/B,MAAM,CACT,CACJ,CAAA;QACL,CAAC,CAAC,CAAA;IACN,CAAC;IAED;;OAEG;IACO,oBAAoB;QAI1B,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,cAAc,EAAE,EAAE;YACnD,IAAI,KAAK,GAAG,MAAM,CAAC,IAAI,CACnB,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,KAAK,cAAc,CAAC,QAAQ,CAAC,MAAM,CAC7D,CAAA;YACD,IAAI,CAAC,KAAK,EAAE,CAAC;gBACT,KAAK,GAAG,EAAE,MAAM,EAAE,cAAc,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAA;gBAChE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YACtB,CAAC;YACD,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;YACnC,OAAO,MAAM,CAAA;QACjB,CAAC,EAAE,EAA0D,CAAC,CAAA;IAClE,CAAC;CACJ","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\"\nimport { MongoRepository } from \"../repository/MongoRepository\"\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 // Constructor\n // ---------------------------------------------------------------------\n\n constructor(\n 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(\n operationType: \"save\" | \"remove\" | \"soft-remove\" | \"recover\",\n ): Promise<void> {\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(\n async (subjectGroup) => {\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 // we don't load if subject already has a database entity loaded\n if (subject.databaseEntity || !subject.identifier) 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) return\n\n const loadRelationPropertyPaths: string[] = []\n\n // for the save, soft-remove and recover 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 (\n operationType === \"save\" ||\n operationType === \"soft-remove\" ||\n operationType === \"recover\"\n ) {\n subjectGroup.subjects.forEach((subject) => {\n // gets all relation property paths that exist in the persisted entity.\n subject.metadata.relations.forEach((relation) => {\n const value = relation.getEntityValue(\n subject.entityWithFulfilledIds!,\n )\n if (value === undefined) return\n\n if (\n loadRelationPropertyPaths.indexOf(\n relation.propertyPath,\n ) === -1\n )\n loadRelationPropertyPaths.push(\n relation.propertyPath,\n )\n })\n })\n } else {\n // 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(\n ...subjectGroup.subjects[0].metadata.manyToManyRelations.map(\n (relation) => relation.propertyPath,\n ),\n )\n }\n\n const findOptions: FindManyOptions<any> = {\n loadEagerRelations: false,\n loadRelationIds: {\n relations: loadRelationPropertyPaths,\n disableMixedMap: true,\n },\n // the soft-deleted entities should be included in the loaded entities for recover operation\n withDeleted: true,\n }\n\n // load database entities for all given ids\n let entities: any[] = []\n if (\n this.queryRunner.connection.driver.options.type ===\n \"mongodb\"\n ) {\n const mongoRepo =\n this.queryRunner.manager.getRepository<ObjectLiteral>(\n subjectGroup.target,\n ) as MongoRepository<ObjectLiteral>\n entities = await mongoRepo.findByIds(allIds, findOptions)\n } else {\n entities = await this.queryRunner.manager\n .getRepository<ObjectLiteral>(subjectGroup.target)\n .createQueryBuilder()\n .setFindOptions(findOptions)\n .whereInIds(allIds)\n .getMany()\n }\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(\n subjectGroup.target,\n entity,\n )\n subjects.forEach((subject) => {\n subject.databaseEntity = entity\n if (!subject.identifier)\n subject.identifier =\n subject.metadata.hasAllPrimaryKeys(entity)\n ? subject.metadata.getEntityIdMap(entity)\n : undefined\n })\n })\n\n // this way we tell what subjects we tried to load database entities of\n for (const subject of allSubjects) {\n subject.databaseEntityLoaded = true\n }\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 * Comparison 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(\n entityTarget: Function | string,\n entity: ObjectLiteral,\n ): Subject[] {\n return this.subjects.filter((subject) => {\n if (!subject.entity) return false\n\n if (subject.entity === entity) return true\n\n return (\n subject.metadata.target === entityTarget &&\n subject.metadata.compareEntities(\n subject.entityWithFulfilledIds!,\n entity,\n )\n )\n })\n }\n\n /**\n * Groups given Subject objects into groups separated by entity targets.\n */\n protected groupByEntityTargets(): {\n target: Function | string\n subjects: Subject[]\n }[] {\n return this.subjects.reduce((groups, operatedEntity) => {\n let group = groups.find(\n (group) => group.target === operatedEntity.metadata.target,\n )\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"],"sourceRoot":".."}
package/package.json CHANGED
@@ -1 +1 @@
1
- { "name": "typeorm", "version": "0.3.24-dev.15de733", "description": "Data-Mapper ORM for TypeScript and ES2021+. Supports MySQL/MariaDB, PostgreSQL, MS SQL Server, Oracle, SAP HANA, SQLite, MongoDB databases.", "homepage": "https://typeorm.io", "bugs": { "url": "https://github.com/typeorm/typeorm/issues" }, "repository": { "type": "git", "url": "https://github.com/typeorm/typeorm.git" }, "funding": "https://opencollective.com/typeorm", "license": "MIT", "author": { "name": "Umed Khudoiberdiev", "email": "pleerock.me@gmail.com" }, "exports": { ".": { "types": "./index.d.ts", "node": { "import": "./index.mjs", "require": "./index.js", "types": "./index.d.ts" }, "browser": { "require": "./index.js", "import": "./browser/index.js", "default": "./index.js" } }, "./browser": { "types": "./index.d.ts", "default": "./browser/index.js" }, "./*.js": "./*.js", "./*": { "require": "./*.js", "import": "./*" } }, "main": "./index.js", "module": "./index.mjs", "browser": { "./browser/connection/ConnectionOptionsReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/connection/options-reader/ConnectionOptionsXmlReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/connection/options-reader/ConnectionOptionsYmlReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/driver/aurora-data-api/AuroraDataApiDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/better-sqlite3/BetterSqlite3Driver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/cockroachdb/CockroachDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/MongoDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/MongoQueryRunner.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/bson.typings.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/typings.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mysql/MysqlDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/oracle/OracleDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/postgres/PostgresDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sap/SapDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sqlite/SqliteDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sqlserver/SqlServerDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/entity-manager/MongoEntityManager.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/logger/FileLogger.js": "./browser/platform/BrowserFileLoggerDummy.js", "./browser/platform/PlatformTools.js": "./browser/platform/BrowserPlatformTools.js", "./browser/repository/MongoRepository.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/util/DirectoryExportedClassesLoader.js": "./browser/platform/BrowserDirectoryExportedClassesLoader.js", "./index.js": "./browser/index.js", "./index.mjs": "./browser/index.js" }, "types": "./index.d.ts", "bin": { "typeorm": "./cli.js", "typeorm-ts-node-commonjs": "./cli-ts-node-commonjs.js", "typeorm-ts-node-esm": "./cli-ts-node-esm.js" }, "scripts": { "changelog": "standard-changelog", "compile": "rimraf ./build && tsc", "format": "prettier --cache --write \"./**/*.ts\"", "format:ci": "prettier --check \"./**/*.ts\"", "lint": "eslint .", "pack": "gulp pack", "package": "gulp package", "pre-commit": "lint-staged", "prepare": "husky", "test": "npm run compile && npm run test:fast --", "test:ci": "mocha --bail", "test:fast": "mocha", "watch": "tsc --watch", "publish:preview": "pkg-pr-new publish './build/package' --pnpm --template='./sample/playground'" }, "dependencies": { "@sqltools/formatter": "^1.2.5", "ansis": "^3.17.0", "app-root-path": "^3.1.0", "buffer": "^6.0.3", "dayjs": "^1.11.13", "debug": "^4.4.0", "dedent": "^1.6.0", "dotenv": "^16.4.7", "glob": "^10.4.5", "sha.js": "^2.4.11", "sql-highlight": "^6.0.0", "tslib": "^2.8.1", "uuid": "^11.1.0", "yargs": "^17.7.2" }, "devDependencies": { "@eslint/js": "^9.22.0", "@sap/hana-client": "^2.24.21", "@tsconfig/node16": "^16.1.3", "@types/chai": "^4.3.20", "@types/chai-as-promised": "^7.1.8", "@types/debug": "^4.1.12", "@types/gulp-rename": "^2.0.6", "@types/gulp-sourcemaps": "^0.0.38", "@types/mocha": "^10.0.10", "@types/node": "^16.18.126", "@types/sha.js": "^2.4.4", "@types/sinon": "^10.0.20", "@types/sinon-chai": "^4.0.0", "@types/source-map-support": "^0.5.10", "@types/yargs": "^17.0.33", "better-sqlite3": "^8.7.0", "chai": "^4.5.0", "chai-as-promised": "^7.1.2", "class-transformer": "^0.5.1", "eslint": "^9.22.0", "globals": "^16.0.0", "gulp": "^4.0.2", "gulp-rename": "^2.0.0", "gulp-replace": "^1.1.4", "gulp-shell": "^0.8.0", "gulp-sourcemaps": "^3.0.0", "gulp-typescript": "^6.0.0-alpha.1", "gulpclass": "^0.2.0", "hdb-pool": "^0.1.6", "husky": "^9.1.7", "lint-staged": "^15.5.2", "mocha": "^10.8.2", "mongodb": "^6.15.0", "mssql": "^11.0.1", "mysql": "^2.18.1", "mysql2": "^3.14.0", "nyc": "^17.1.0", "oracledb": "^6.8.0", "pg": "^8.14.1", "pg-query-stream": "^4.8.1", "pkg-pr-new": "^0.0.43", "prettier": "^2.8.8", "redis": "^4.7.0", "remap-istanbul": "^0.13.0", "rimraf": "^5.0.10", "sinon": "^15.2.0", "sinon-chai": "^3.7.0", "source-map-support": "^0.5.21", "sql.js": "^1.13.0", "sqlite3": "^5.1.7", "standard-changelog": "^6.0.0", "ts-node": "^10.9.2", "typescript": "^5.8.2", "typescript-eslint": "^8.27.0" }, "peerDependencies": { "@google-cloud/spanner": "^5.18.0 || ^6.0.0 || ^7.0.0", "@sap/hana-client": "^2.12.25", "better-sqlite3": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0", "hdb-pool": "^0.1.6", "ioredis": "^5.0.4", "mongodb": "^5.8.0 || ^6.0.0", "mssql": "^9.1.1 || ^10.0.1 || ^11.0.1", "mysql2": "^2.2.5 || ^3.0.1", "oracledb": "^6.3.0", "pg": "^8.5.1", "pg-native": "^3.0.0", "pg-query-stream": "^4.0.0", "redis": "^3.1.1 || ^4.0.0", "reflect-metadata": "^0.1.14 || ^0.2.0", "sql.js": "^1.4.0", "sqlite3": "^5.0.3", "ts-node": "^10.7.0", "typeorm-aurora-data-api-driver": "^2.0.0 || ^3.0.0" }, "peerDependenciesMeta": { "@google-cloud/spanner": { "optional": true }, "@sap/hana-client": { "optional": true }, "better-sqlite3": { "optional": true }, "hdb-pool": { "optional": true }, "ioredis": { "optional": true }, "mongodb": { "optional": true }, "mssql": { "optional": true }, "mysql2": { "optional": true }, "oracledb": { "optional": true }, "pg": { "optional": true }, "pg-native": { "optional": true }, "pg-query-stream": { "optional": true }, "redis": { "optional": true }, "sql.js": { "optional": true }, "sqlite3": { "optional": true }, "ts-node": { "optional": true }, "typeorm-aurora-data-api-driver": { "optional": true } }, "engines": { "node": ">=16.13.0" }, "collective": { "type": "opencollective", "url": "https://opencollective.com/typeorm", "logo": "https://opencollective.com/opencollective/logo.txt" }, "readmeFilename": "README.md", "tags": [ "orm", "typescript", "typescript-orm", "mariadb", "mariadb-orm", "mysql", "mysql-orm", "oracle", "oracle-orm", "postgresql", "postgresql-orm", "sap-hana", "sap-hana-orm", "spanner", "cloud-spanner", "cloud-spanner-orm", "sqlite", "sqlite-orm", "sql-server", "sql-server-orm" ] }
1
+ { "name": "typeorm", "version": "0.3.24-dev.2168441", "description": "Data-Mapper ORM for TypeScript and ES2021+. Supports MySQL/MariaDB, PostgreSQL, MS SQL Server, Oracle, SAP HANA, SQLite, MongoDB databases.", "homepage": "https://typeorm.io", "bugs": { "url": "https://github.com/typeorm/typeorm/issues" }, "repository": { "type": "git", "url": "https://github.com/typeorm/typeorm.git" }, "funding": "https://opencollective.com/typeorm", "license": "MIT", "author": { "name": "Umed Khudoiberdiev", "email": "pleerock.me@gmail.com" }, "exports": { ".": { "types": "./index.d.ts", "node": { "import": "./index.mjs", "require": "./index.js", "types": "./index.d.ts" }, "browser": { "require": "./index.js", "import": "./browser/index.js", "default": "./index.js" } }, "./browser": { "types": "./index.d.ts", "default": "./browser/index.js" }, "./*.js": "./*.js", "./*": { "require": "./*.js", "import": "./*" } }, "main": "./index.js", "module": "./index.mjs", "browser": { "./browser/connection/ConnectionOptionsReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/connection/options-reader/ConnectionOptionsXmlReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/connection/options-reader/ConnectionOptionsYmlReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/driver/aurora-data-api/AuroraDataApiDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/better-sqlite3/BetterSqlite3Driver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/cockroachdb/CockroachDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/MongoDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/MongoQueryRunner.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/bson.typings.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/typings.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mysql/MysqlDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/oracle/OracleDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/postgres/PostgresDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sap/SapDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sqlite/SqliteDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sqlserver/SqlServerDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/entity-manager/MongoEntityManager.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/logger/FileLogger.js": "./browser/platform/BrowserFileLoggerDummy.js", "./browser/platform/PlatformTools.js": "./browser/platform/BrowserPlatformTools.js", "./browser/repository/MongoRepository.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/util/DirectoryExportedClassesLoader.js": "./browser/platform/BrowserDirectoryExportedClassesLoader.js", "./index.js": "./browser/index.js", "./index.mjs": "./browser/index.js" }, "types": "./index.d.ts", "bin": { "typeorm": "./cli.js", "typeorm-ts-node-commonjs": "./cli-ts-node-commonjs.js", "typeorm-ts-node-esm": "./cli-ts-node-esm.js" }, "scripts": { "changelog": "standard-changelog", "compile": "rimraf ./build && tsc", "format": "prettier --cache --write \"./**/*.ts\"", "format:ci": "prettier --check \"./**/*.ts\"", "lint": "eslint .", "pack": "gulp pack", "package": "gulp package", "pre-commit": "lint-staged", "prepare": "husky", "test": "npm run compile && npm run test:fast --", "test:ci": "mocha --bail", "test:fast": "mocha", "watch": "tsc --watch", "publish:preview": "pkg-pr-new publish './build/package' --pnpm --template='./sample/playground'" }, "dependencies": { "@sqltools/formatter": "^1.2.5", "ansis": "^3.17.0", "app-root-path": "^3.1.0", "buffer": "^6.0.3", "dayjs": "^1.11.13", "debug": "^4.4.0", "dedent": "^1.6.0", "dotenv": "^16.4.7", "glob": "^10.4.5", "sha.js": "^2.4.11", "sql-highlight": "^6.0.0", "tslib": "^2.8.1", "uuid": "^11.1.0", "yargs": "^17.7.2" }, "devDependencies": { "@eslint/js": "^9.22.0", "@sap/hana-client": "^2.24.21", "@tsconfig/node16": "^16.1.3", "@types/chai": "^4.3.20", "@types/chai-as-promised": "^7.1.8", "@types/debug": "^4.1.12", "@types/gulp-rename": "^2.0.6", "@types/gulp-sourcemaps": "^0.0.38", "@types/mocha": "^10.0.10", "@types/node": "^16.18.126", "@types/sha.js": "^2.4.4", "@types/sinon": "^10.0.20", "@types/sinon-chai": "^4.0.0", "@types/source-map-support": "^0.5.10", "@types/yargs": "^17.0.33", "better-sqlite3": "^8.7.0", "chai": "^4.5.0", "chai-as-promised": "^7.1.2", "class-transformer": "^0.5.1", "eslint": "^9.22.0", "globals": "^16.0.0", "gulp": "^4.0.2", "gulp-rename": "^2.0.0", "gulp-replace": "^1.1.4", "gulp-shell": "^0.8.0", "gulp-sourcemaps": "^3.0.0", "gulp-typescript": "^6.0.0-alpha.1", "gulpclass": "^0.2.0", "hdb-pool": "^0.1.6", "husky": "^9.1.7", "lint-staged": "^15.5.2", "mocha": "^10.8.2", "mongodb": "^6.15.0", "mssql": "^11.0.1", "mysql": "^2.18.1", "mysql2": "^3.14.0", "nyc": "^17.1.0", "oracledb": "^6.8.0", "pg": "^8.14.1", "pg-query-stream": "^4.8.1", "pkg-pr-new": "^0.0.43", "prettier": "^2.8.8", "redis": "^4.7.0", "remap-istanbul": "^0.13.0", "rimraf": "^5.0.10", "sinon": "^15.2.0", "sinon-chai": "^3.7.0", "source-map-support": "^0.5.21", "sql.js": "^1.13.0", "sqlite3": "^5.1.7", "standard-changelog": "^6.0.0", "ts-node": "^10.9.2", "typescript": "^5.8.2", "typescript-eslint": "^8.27.0" }, "peerDependencies": { "@google-cloud/spanner": "^5.18.0 || ^6.0.0 || ^7.0.0", "@sap/hana-client": "^2.12.25", "better-sqlite3": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0", "hdb-pool": "^0.1.6", "ioredis": "^5.0.4", "mongodb": "^5.8.0 || ^6.0.0", "mssql": "^9.1.1 || ^10.0.1 || ^11.0.1", "mysql2": "^2.2.5 || ^3.0.1", "oracledb": "^6.3.0", "pg": "^8.5.1", "pg-native": "^3.0.0", "pg-query-stream": "^4.0.0", "redis": "^3.1.1 || ^4.0.0", "reflect-metadata": "^0.1.14 || ^0.2.0", "sql.js": "^1.4.0", "sqlite3": "^5.0.3", "ts-node": "^10.7.0", "typeorm-aurora-data-api-driver": "^2.0.0 || ^3.0.0" }, "peerDependenciesMeta": { "@google-cloud/spanner": { "optional": true }, "@sap/hana-client": { "optional": true }, "better-sqlite3": { "optional": true }, "hdb-pool": { "optional": true }, "ioredis": { "optional": true }, "mongodb": { "optional": true }, "mssql": { "optional": true }, "mysql2": { "optional": true }, "oracledb": { "optional": true }, "pg": { "optional": true }, "pg-native": { "optional": true }, "pg-query-stream": { "optional": true }, "redis": { "optional": true }, "sql.js": { "optional": true }, "sqlite3": { "optional": true }, "ts-node": { "optional": true }, "typeorm-aurora-data-api-driver": { "optional": true } }, "engines": { "node": ">=16.13.0" }, "collective": { "type": "opencollective", "url": "https://opencollective.com/typeorm", "logo": "https://opencollective.com/opencollective/logo.txt" }, "readmeFilename": "README.md", "tags": [ "orm", "typescript", "typescript-orm", "mariadb", "mariadb-orm", "mysql", "mysql-orm", "oracle", "oracle-orm", "postgresql", "postgresql-orm", "sap-hana", "sap-hana-orm", "spanner", "cloud-spanner", "cloud-spanner-orm", "sqlite", "sqlite-orm", "sql-server", "sql-server-orm" ] }
@@ -1,4 +1,5 @@
1
1
  import { Subject } from "./Subject";
2
+ import { ObjectLiteral } from "../common/ObjectLiteral";
2
3
  import { QueryRunner } from "../query-runner/QueryRunner";
3
4
  /**
4
5
  * Loads database entities for all operate subjects which do not have database entity set.
@@ -17,6 +18,13 @@ export declare class SubjectDatabaseEntityLoader {
17
18
  * This option is used for deletion.
18
19
  */
19
20
  load(operationType: "save" | "remove" | "soft-remove" | "recover"): Promise<void>;
21
+ /**
22
+ * Finds subjects where entity like given subject's entity.
23
+ * Comparison made by entity id.
24
+ * Multiple subjects may be returned if duplicates are present in the subject array.
25
+ * This will likely result in the same row being updated multiple times during a transaction.
26
+ */
27
+ protected findByPersistEntityLike(entityTarget: Function | string, entity: ObjectLiteral): Subject[];
20
28
  /**
21
29
  * Groups given Subject objects into groups separated by entity targets.
22
30
  */
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SubjectDatabaseEntityLoader = void 0;
4
- const OrmUtils_1 = require("../util/OrmUtils");
5
4
  /**
6
5
  * Loads database entities for all operate subjects which do not have database entity set.
7
6
  * All entities that we load database entities for are marked as updated or inserted.
@@ -92,17 +91,17 @@ class SubjectDatabaseEntityLoader {
92
91
  .whereInIds(allIds)
93
92
  .getMany();
94
93
  }
95
- // Now when we have entities we need to find subject of each entity
96
- // and insert that entity into database entity of the found subjects.
97
- // A single entity can be applied to many subjects as there might be duplicates.
98
- // This will likely result in the same row being updated multiple times during a transaction.
94
+ // now when we have entities we need to find subject of each entity
95
+ // and insert that entity into database entity of the found subjects
99
96
  entities.forEach((entity) => {
100
- const entityId = allSubjects[0].metadata.getEntityIdMap(entity);
101
- allSubjects.forEach((subject) => {
102
- if (subject.databaseEntity)
103
- return;
104
- if (OrmUtils_1.OrmUtils.compareIds(subject.identifier, entityId))
105
- subject.databaseEntity = entity;
97
+ const subjects = this.findByPersistEntityLike(subjectGroup.target, entity);
98
+ subjects.forEach((subject) => {
99
+ subject.databaseEntity = entity;
100
+ if (!subject.identifier)
101
+ subject.identifier =
102
+ subject.metadata.hasAllPrimaryKeys(entity)
103
+ ? subject.metadata.getEntityIdMap(entity)
104
+ : undefined;
106
105
  });
107
106
  });
108
107
  // this way we tell what subjects we tried to load database entities of
@@ -115,6 +114,22 @@ class SubjectDatabaseEntityLoader {
115
114
  // ---------------------------------------------------------------------
116
115
  // Protected Methods
117
116
  // ---------------------------------------------------------------------
117
+ /**
118
+ * Finds subjects where entity like given subject's entity.
119
+ * Comparison made by entity id.
120
+ * Multiple subjects may be returned if duplicates are present in the subject array.
121
+ * This will likely result in the same row being updated multiple times during a transaction.
122
+ */
123
+ findByPersistEntityLike(entityTarget, entity) {
124
+ return this.subjects.filter((subject) => {
125
+ if (!subject.entity)
126
+ return false;
127
+ if (subject.entity === entity)
128
+ return true;
129
+ return (subject.metadata.target === entityTarget &&
130
+ subject.metadata.compareEntities(subject.entityWithFulfilledIds, entity));
131
+ });
132
+ }
118
133
  /**
119
134
  * Groups given Subject objects into groups separated by entity targets.
120
135
  */
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/persistence/SubjectDatabaseEntityLoader.ts"],"names":[],"mappings":";;;AAKA,+CAA2C;AAE3C;;;;;GAKG;AACH,MAAa,2BAA2B;IACpC,wEAAwE;IACxE,cAAc;IACd,wEAAwE;IAExE,YACc,WAAwB,EACxB,QAAmB;QADnB,gBAAW,GAAX,WAAW,CAAa;QACxB,aAAQ,GAAR,QAAQ,CAAW;IAC9B,CAAC;IAEJ,wEAAwE;IACxE,iBAAiB;IACjB,wEAAwE;IAExE;;;;;OAKG;IACH,KAAK,CAAC,IAAI,CACN,aAA4D;QAE5D,+FAA+F;QAC/F,8FAA8F;QAC9F,MAAM,QAAQ,GAAG,IAAI,CAAC,oBAAoB,EAAE,CAAC,GAAG,CAC5C,KAAK,EAAE,YAAY,EAAE,EAAE;YACnB,qDAAqD;YACrD,MAAM,MAAM,GAAoB,EAAE,CAAA;YAClC,MAAM,WAAW,GAAc,EAAE,CAAA;YACjC,YAAY,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;gBACtC,gEAAgE;gBAChE,IAAI,OAAO,CAAC,cAAc,IAAI,CAAC,OAAO,CAAC,UAAU;oBAAE,OAAM;gBAEzD,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;gBAC/B,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;YAC7B,CAAC,CAAC,CAAA;YAEF,yGAAyG;YACz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C/B,CAAC;IAED,wEAAwE;IACxE,oBAAoB;IACpB,wEAAwE;IAExE;;OAEG;IACO,oBAAoB;QAI1B,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,cAAc,EAAE,EAAE;YACnD,IAAI,KAAK,GAAG,MAAM,CAAC,IAAI,CACnB,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,KAAK,cAAc,CAAC,QAAQ,CAAC,MAAM,CAC7D,CAAA;YACD,IAAI,CAAC,KAAK,EAAE,CAAC;gBACT,KAAK,GAAG,EAAE,MAAM,EAAE,cAAc,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAA;gBAChE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YACtB,CAAC;YACD,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;YACnC,OAAO,MAAM,CAAA;QACjB,CAAC,EAAE,EAA0D,CAAC,CAAA;IAClE,CAAC;CACJ;AAhKD,kEAgKC","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\"\nimport { MongoRepository } from \"../repository/MongoRepository\"\nimport { OrmUtils } from \"../util/OrmUtils\"\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 // Constructor\n // ---------------------------------------------------------------------\n\n constructor(\n 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(\n operationType: \"save\" | \"remove\" | \"soft-remove\" | \"recover\",\n ): Promise<void> {\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(\n async (subjectGroup) => {\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 // we don't load if subject already has a database entity loaded\n if (subject.databaseEntity || !subject.identifier) 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) return\n\n const loadRelationPropertyPaths: string[] = []\n\n // for the save, soft-remove and recover 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 (\n operationType === \"save\" ||\n operationType === \"soft-remove\" ||\n operationType === \"recover\"\n ) {\n subjectGroup.subjects.forEach((subject) => {\n // gets all relation property paths that exist in the persisted entity.\n subject.metadata.relations.forEach((relation) => {\n const value = relation.getEntityValue(\n subject.entityWithFulfilledIds!,\n )\n if (value === undefined) return\n\n if (\n loadRelationPropertyPaths.indexOf(\n relation.propertyPath,\n ) === -1\n )\n loadRelationPropertyPaths.push(\n relation.propertyPath,\n )\n })\n })\n } else {\n // 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(\n ...subjectGroup.subjects[0].metadata.manyToManyRelations.map(\n (relation) => relation.propertyPath,\n ),\n )\n }\n\n const findOptions: FindManyOptions<any> = {\n loadEagerRelations: false,\n loadRelationIds: {\n relations: loadRelationPropertyPaths,\n disableMixedMap: true,\n },\n // the soft-deleted entities should be included in the loaded entities for recover operation\n withDeleted: true,\n }\n\n // load database entities for all given ids\n let entities: any[] = []\n if (\n this.queryRunner.connection.driver.options.type ===\n \"mongodb\"\n ) {\n const mongoRepo =\n this.queryRunner.manager.getRepository<ObjectLiteral>(\n subjectGroup.target,\n ) as MongoRepository<ObjectLiteral>\n entities = await mongoRepo.findByIds(allIds, findOptions)\n } else {\n entities = await this.queryRunner.manager\n .getRepository<ObjectLiteral>(subjectGroup.target)\n .createQueryBuilder()\n .setFindOptions(findOptions)\n .whereInIds(allIds)\n .getMany()\n }\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 // A single entity can be applied to many subjects as there might be duplicates.\n // This will likely result in the same row being updated multiple times during a transaction.\n entities.forEach((entity) => {\n const entityId =\n allSubjects[0].metadata.getEntityIdMap(entity)\n allSubjects.forEach((subject) => {\n if (subject.databaseEntity) return\n if (OrmUtils.compareIds(subject.identifier, entityId))\n subject.databaseEntity = entity\n })\n })\n\n // this way we tell what subjects we tried to load database entities of\n for (const subject of allSubjects) {\n subject.databaseEntityLoaded = true\n }\n },\n )\n\n await Promise.all(promises)\n }\n\n // ---------------------------------------------------------------------\n // Protected Methods\n // ---------------------------------------------------------------------\n\n /**\n * Groups given Subject objects into groups separated by entity targets.\n */\n protected groupByEntityTargets(): {\n target: Function | string\n subjects: Subject[]\n }[] {\n return this.subjects.reduce((groups, operatedEntity) => {\n let group = groups.find(\n (group) => group.target === operatedEntity.metadata.target,\n )\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"],"sourceRoot":".."}
1
+ {"version":3,"sources":["../../src/persistence/SubjectDatabaseEntityLoader.ts"],"names":[],"mappings":";;;AAMA;;;;;GAKG;AACH,MAAa,2BAA2B;IACpC,wEAAwE;IACxE,cAAc;IACd,wEAAwE;IAExE,YACc,WAAwB,EACxB,QAAmB;QADnB,gBAAW,GAAX,WAAW,CAAa;QACxB,aAAQ,GAAR,QAAQ,CAAW;IAC9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AoB;QAI1B,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,cAAc,EAAE,EAAE;YACnD,IAAI,KAAK,GAAG,MAAM,CAAC,IAAI,CACnB,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,KAAK,cAAc,CAAC,QAAQ,CAAC,MAAM,CAC7D,CAAA;YACD,IAAI,CAAC,KAAK,EAAE,CAAC;gBACT,KAAK,GAAG,EAAE,MAAM,EAAE,cAAc,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAA;gBAChE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YACtB,CAAC;YACD,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;YACnC,OAAO,MAAM,CAAA;QACjB,CAAC,EAAE,EAA0D,CAAC,CAAA;IAClE,CAAC;CACJ;AA5LD,kEA4LC","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\"\nimport { MongoRepository } from \"../repository/MongoRepository\"\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 // Constructor\n // ---------------------------------------------------------------------\n\n constructor(\n 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(\n operationType: \"save\" | \"remove\" | \"soft-remove\" | \"recover\",\n ): Promise<void> {\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(\n async (subjectGroup) => {\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 // we don't load if subject already has a database entity loaded\n if (subject.databaseEntity || !subject.identifier) 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) return\n\n const loadRelationPropertyPaths: string[] = []\n\n // for the save, soft-remove and recover 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 (\n operationType === \"save\" ||\n operationType === \"soft-remove\" ||\n operationType === \"recover\"\n ) {\n subjectGroup.subjects.forEach((subject) => {\n // gets all relation property paths that exist in the persisted entity.\n subject.metadata.relations.forEach((relation) => {\n const value = relation.getEntityValue(\n subject.entityWithFulfilledIds!,\n )\n if (value === undefined) return\n\n if (\n loadRelationPropertyPaths.indexOf(\n relation.propertyPath,\n ) === -1\n )\n loadRelationPropertyPaths.push(\n relation.propertyPath,\n )\n })\n })\n } else {\n // 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(\n ...subjectGroup.subjects[0].metadata.manyToManyRelations.map(\n (relation) => relation.propertyPath,\n ),\n )\n }\n\n const findOptions: FindManyOptions<any> = {\n loadEagerRelations: false,\n loadRelationIds: {\n relations: loadRelationPropertyPaths,\n disableMixedMap: true,\n },\n // the soft-deleted entities should be included in the loaded entities for recover operation\n withDeleted: true,\n }\n\n // load database entities for all given ids\n let entities: any[] = []\n if (\n this.queryRunner.connection.driver.options.type ===\n \"mongodb\"\n ) {\n const mongoRepo =\n this.queryRunner.manager.getRepository<ObjectLiteral>(\n subjectGroup.target,\n ) as MongoRepository<ObjectLiteral>\n entities = await mongoRepo.findByIds(allIds, findOptions)\n } else {\n entities = await this.queryRunner.manager\n .getRepository<ObjectLiteral>(subjectGroup.target)\n .createQueryBuilder()\n .setFindOptions(findOptions)\n .whereInIds(allIds)\n .getMany()\n }\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(\n subjectGroup.target,\n entity,\n )\n subjects.forEach((subject) => {\n subject.databaseEntity = entity\n if (!subject.identifier)\n subject.identifier =\n subject.metadata.hasAllPrimaryKeys(entity)\n ? subject.metadata.getEntityIdMap(entity)\n : undefined\n })\n })\n\n // this way we tell what subjects we tried to load database entities of\n for (const subject of allSubjects) {\n subject.databaseEntityLoaded = true\n }\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 * Comparison 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(\n entityTarget: Function | string,\n entity: ObjectLiteral,\n ): Subject[] {\n return this.subjects.filter((subject) => {\n if (!subject.entity) return false\n\n if (subject.entity === entity) return true\n\n return (\n subject.metadata.target === entityTarget &&\n subject.metadata.compareEntities(\n subject.entityWithFulfilledIds!,\n entity,\n )\n )\n })\n }\n\n /**\n * Groups given Subject objects into groups separated by entity targets.\n */\n protected groupByEntityTargets(): {\n target: Function | string\n subjects: Subject[]\n }[] {\n return this.subjects.reduce((groups, operatedEntity) => {\n let group = groups.find(\n (group) => group.target === operatedEntity.metadata.target,\n )\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"],"sourceRoot":".."}