typeorm 0.3.6-dev.cf3efec → 0.3.6-dev.ef025bd

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.
Files changed (87) hide show
  1. package/browser/driver/aurora-mysql/AuroraMysqlDriver.d.ts +1 -1
  2. package/browser/driver/aurora-mysql/AuroraMysqlDriver.js.map +1 -1
  3. package/browser/driver/mongodb/typings.d.ts +5 -5
  4. package/browser/driver/mongodb/typings.js.map +1 -1
  5. package/browser/driver/sqlserver/authentication/AzureActiveDirectoryAccessTokenAuthentication.d.ts +1 -1
  6. package/browser/driver/sqlserver/authentication/AzureActiveDirectoryAccessTokenAuthentication.js.map +1 -1
  7. package/browser/driver/sqlserver/authentication/AzureActiveDirectoryMsiAppServiceAuthentication.d.ts +2 -2
  8. package/browser/driver/sqlserver/authentication/AzureActiveDirectoryMsiAppServiceAuthentication.js.map +1 -1
  9. package/browser/driver/sqlserver/authentication/AzureActiveDirectoryMsiVmAuthentication.d.ts +2 -2
  10. package/browser/driver/sqlserver/authentication/AzureActiveDirectoryMsiVmAuthentication.js.map +1 -1
  11. package/browser/driver/sqlserver/authentication/AzureActiveDirectoryPasswordAuthentication.d.ts +2 -2
  12. package/browser/driver/sqlserver/authentication/AzureActiveDirectoryPasswordAuthentication.js.map +1 -1
  13. package/browser/entity-manager/EntityManager.js +1 -1
  14. package/browser/entity-manager/EntityManager.js.map +1 -1
  15. package/browser/metadata/EntityMetadata.js +1 -1
  16. package/browser/metadata/EntityMetadata.js.map +1 -1
  17. package/browser/migration/MigrationExecutor.js +1 -1
  18. package/browser/migration/MigrationExecutor.js.map +1 -1
  19. package/browser/persistence/SubjectDatabaseEntityLoader.d.ts +1 -1
  20. package/browser/persistence/SubjectDatabaseEntityLoader.js +1 -1
  21. package/browser/persistence/SubjectDatabaseEntityLoader.js.map +1 -1
  22. package/browser/persistence/SubjectExecutor.js +1 -1
  23. package/browser/persistence/SubjectExecutor.js.map +1 -1
  24. package/browser/persistence/subject-builder/CascadesSubjectBuilder.d.ts +1 -1
  25. package/browser/persistence/subject-builder/CascadesSubjectBuilder.js +1 -1
  26. package/browser/persistence/subject-builder/CascadesSubjectBuilder.js.map +1 -1
  27. package/browser/persistence/subject-builder/ManyToManySubjectBuilder.js +1 -1
  28. package/browser/persistence/subject-builder/ManyToManySubjectBuilder.js.map +1 -1
  29. package/browser/persistence/subject-builder/OneToOneInverseSideSubjectBuilder.js +1 -1
  30. package/browser/persistence/subject-builder/OneToOneInverseSideSubjectBuilder.js.map +1 -1
  31. package/browser/query-builder/QueryBuilder.d.ts +1 -1
  32. package/browser/query-builder/QueryBuilder.js +2 -2
  33. package/browser/query-builder/QueryBuilder.js.map +1 -1
  34. package/browser/query-builder/QueryExpressionMap.d.ts +1 -1
  35. package/browser/query-builder/QueryExpressionMap.js +1 -1
  36. package/browser/query-builder/QueryExpressionMap.js.map +1 -1
  37. package/browser/query-builder/ReturningResultsEntityUpdator.js +2 -2
  38. package/browser/query-builder/ReturningResultsEntityUpdator.js.map +1 -1
  39. package/browser/schema-builder/options/TableForeignKeyOptions.d.ts +1 -1
  40. package/browser/schema-builder/options/TableForeignKeyOptions.js.map +1 -1
  41. package/browser/util/OrmUtils.d.ts +1 -1
  42. package/browser/util/OrmUtils.js +4 -4
  43. package/browser/util/OrmUtils.js.map +1 -1
  44. package/driver/aurora-mysql/AuroraMysqlDriver.d.ts +1 -1
  45. package/driver/aurora-mysql/AuroraMysqlDriver.js.map +1 -1
  46. package/driver/mongodb/typings.d.ts +5 -5
  47. package/driver/mongodb/typings.js.map +1 -1
  48. package/driver/sqlserver/authentication/AzureActiveDirectoryAccessTokenAuthentication.d.ts +1 -1
  49. package/driver/sqlserver/authentication/AzureActiveDirectoryAccessTokenAuthentication.js.map +1 -1
  50. package/driver/sqlserver/authentication/AzureActiveDirectoryMsiAppServiceAuthentication.d.ts +2 -2
  51. package/driver/sqlserver/authentication/AzureActiveDirectoryMsiAppServiceAuthentication.js.map +1 -1
  52. package/driver/sqlserver/authentication/AzureActiveDirectoryMsiVmAuthentication.d.ts +2 -2
  53. package/driver/sqlserver/authentication/AzureActiveDirectoryMsiVmAuthentication.js.map +1 -1
  54. package/driver/sqlserver/authentication/AzureActiveDirectoryPasswordAuthentication.d.ts +2 -2
  55. package/driver/sqlserver/authentication/AzureActiveDirectoryPasswordAuthentication.js.map +1 -1
  56. package/entity-manager/EntityManager.js +1 -1
  57. package/entity-manager/EntityManager.js.map +1 -1
  58. package/metadata/EntityMetadata.js +1 -1
  59. package/metadata/EntityMetadata.js.map +1 -1
  60. package/migration/MigrationExecutor.js +1 -1
  61. package/migration/MigrationExecutor.js.map +1 -1
  62. package/package.json +1 -1
  63. package/persistence/SubjectDatabaseEntityLoader.d.ts +1 -1
  64. package/persistence/SubjectDatabaseEntityLoader.js +1 -1
  65. package/persistence/SubjectDatabaseEntityLoader.js.map +1 -1
  66. package/persistence/SubjectExecutor.js +1 -1
  67. package/persistence/SubjectExecutor.js.map +1 -1
  68. package/persistence/subject-builder/CascadesSubjectBuilder.d.ts +1 -1
  69. package/persistence/subject-builder/CascadesSubjectBuilder.js +1 -1
  70. package/persistence/subject-builder/CascadesSubjectBuilder.js.map +1 -1
  71. package/persistence/subject-builder/ManyToManySubjectBuilder.js +1 -1
  72. package/persistence/subject-builder/ManyToManySubjectBuilder.js.map +1 -1
  73. package/persistence/subject-builder/OneToOneInverseSideSubjectBuilder.js +1 -1
  74. package/persistence/subject-builder/OneToOneInverseSideSubjectBuilder.js.map +1 -1
  75. package/query-builder/QueryBuilder.d.ts +1 -1
  76. package/query-builder/QueryBuilder.js +2 -2
  77. package/query-builder/QueryBuilder.js.map +1 -1
  78. package/query-builder/QueryExpressionMap.d.ts +1 -1
  79. package/query-builder/QueryExpressionMap.js +1 -1
  80. package/query-builder/QueryExpressionMap.js.map +1 -1
  81. package/query-builder/ReturningResultsEntityUpdator.js +2 -2
  82. package/query-builder/ReturningResultsEntityUpdator.js.map +1 -1
  83. package/schema-builder/options/TableForeignKeyOptions.d.ts +1 -1
  84. package/schema-builder/options/TableForeignKeyOptions.js.map +1 -1
  85. package/util/OrmUtils.d.ts +1 -1
  86. package/util/OrmUtils.js +4 -4
  87. package/util/OrmUtils.js.map +1 -1
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/persistence/subject-builder/ManyToManySubjectBuilder.ts"],"names":[],"mappings":";;;AAAA,wCAAoC;AACpC,kDAA8C;AAI9C;;;;;;;GAOG;AACH,MAAa,wBAAwB;IACjC,wEAAwE;IACxE,cAAc;IACd,wEAAwE;IAExE,YAAsB,QAAmB;QAAnB,aAAQ,GAAR,QAAQ,CAAW;IAAG,CAAC;IAE7C,wEAAwE;IACxE,iBAAiB;IACjB,wEAAwE;IAExE;;OAEG;IACH,KAAK;QACD,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC9B,mGAAmG;YACnG,IAAI,CAAC,OAAO,CAAC,MAAM;gBAAE,OAAM;YAE3B,kGAAkG;YAClG,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;gBACtD,mDAAmD;gBACnD,IAAI,QAAQ,CAAC,kBAAkB,KAAK,KAAK;oBAAE,OAAM;gBAEjD,IAAI,CAAC,uBAAuB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAA;YACnD,CAAC,CAAC,CAAA;QACN,CAAC,CAAC,CAAA;IACN,CAAC;IAED;;OAEG;IACH,kBAAkB,CAAC,OAAgB;QAC/B,6FAA6F;QAC7F,gFAAgF;QAChF,IAAI,CAAC,OAAO,CAAC,cAAc;YAAE,OAAM;QAEnC,kGAAkG;QAClG,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;YACtD,mDAAmD;YACnD,IAAI,QAAQ,CAAC,kBAAkB,KAAK,KAAK;gBAAE,OAAM;YAEjD,8FAA8F;YAC9F,sGAAsG;YACtG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A;QACL,CAAC,CAAC,CAAA;QACF,QAAQ,CAAC,sBAAuB,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE;YAC/D,mBAAQ,CAAC,SAAS,CACd,UAAU,EACV,MAAM,CAAC,cAAc,CACjB,MAAM,CAAC,gBAAiB,CAAC,cAAc,CAAC,gBAAgB,CAAC,CAC5D,CACJ,CAAA;QACL,CAAC,CAAC,CAAA;QACF,OAAO,UAAU,CAAA;IACrB,CAAC;CACJ;AA9QD,4DA8QC","file":"ManyToManySubjectBuilder.js","sourcesContent":["import { Subject } from \"../Subject\"\nimport { OrmUtils } from \"../../util/OrmUtils\"\nimport { ObjectLiteral } from \"../../common/ObjectLiteral\"\nimport { RelationMetadata } from \"../../metadata/RelationMetadata\"\n\n/**\n * Builds operations needs to be executed for many-to-many relations of the given subjects.\n *\n * by example: post contains owner many-to-many relation with categories in the property called \"categories\", e.g.\n * @ManyToMany(type => Category, category => category.posts) categories: Category[]\n * If user adds categories into the post and saves post we need to bind them.\n * This operation requires updation of junction table.\n */\nexport class ManyToManySubjectBuilder {\n // ---------------------------------------------------------------------\n // Constructor\n // ---------------------------------------------------------------------\n\n constructor(protected subjects: Subject[]) {}\n\n // ---------------------------------------------------------------------\n // Public Methods\n // ---------------------------------------------------------------------\n\n /**\n * Builds operations for any changes in the many-to-many relations of the subjects.\n */\n build(): void {\n this.subjects.forEach((subject) => {\n // if subject doesn't have entity then no need to find something that should be inserted or removed\n if (!subject.entity) return\n\n // go through all persistence enabled many-to-many relations and build subject operations for them\n subject.metadata.manyToManyRelations.forEach((relation) => {\n // skip relations for which persistence is disabled\n if (relation.persistenceEnabled === false) return\n\n this.buildForSubjectRelation(subject, relation)\n })\n })\n }\n\n /**\n * Builds operations for removal of all many-to-many records of all many-to-many relations of the given subject.\n */\n buildForAllRemoval(subject: Subject) {\n // if subject does not have a database entity then it means it does not exist in the database\n // if it does not exist in the database then we don't have anything for deletion\n if (!subject.databaseEntity) return\n\n // go through all persistence enabled many-to-many relations and build subject operations for them\n subject.metadata.manyToManyRelations.forEach((relation) => {\n // skip relations for which persistence is disabled\n if (relation.persistenceEnabled === false) return\n\n // get all related entities (actually related entity relation ids) bind to this subject entity\n // by example: returns category ids of the post we are currently working with (subject.entity is post)\n const relatedEntityRelationIdsInDatabase: ObjectLiteral[] =\n relation.getEntityValue(subject.databaseEntity!)\n\n // go through all related entities and create a new junction subject for each row in junction table\n relatedEntityRelationIdsInDatabase.forEach((relationId) => {\n const junctionSubject = new Subject({\n metadata: relation.junctionEntityMetadata!,\n parentSubject: subject,\n mustBeRemoved: true,\n identifier: this.buildJunctionIdentifier(\n subject,\n relation,\n relationId,\n ),\n })\n\n // we use unshift because we need to perform those operations before post deletion is performed\n // but post deletion was already added as an subject\n // this is temporary solution, later we need to implement proper sorting of subjects before their removal\n this.subjects.push(junctionSubject)\n })\n })\n }\n\n // ---------------------------------------------------------------------\n // Protected Methods\n // ---------------------------------------------------------------------\n\n /**\n * Builds operations for a given subject and relation.\n *\n * by example: subject is \"post\" entity we are saving here and relation is \"categories\" inside it here.\n */\n protected buildForSubjectRelation(\n subject: Subject,\n relation: RelationMetadata,\n ) {\n // load from db all relation ids of inverse entities that are \"bind\" to the subject's entity\n // this way we gonna check which relation ids are missing and which are new (e.g. inserted or removed)\n let databaseRelatedEntityIds: ObjectLiteral[] = []\n\n // if subject don't have database entity it means all related entities in persisted subject are new and must be bind\n // and we don't need to remove something that is not exist\n if (subject.databaseEntity)\n databaseRelatedEntityIds = relation.getEntityValue(\n subject.databaseEntity,\n )\n\n // extract entity's relation value\n // by example: categories inside our post (subject.entity is post)\n let relatedEntities: ObjectLiteral[] = relation.getEntityValue(\n subject.entity!,\n )\n if (relatedEntities === null)\n // if value set to null its equal if we set it to empty array - all items must be removed from the database\n relatedEntities = []\n if (!Array.isArray(relatedEntities)) return\n\n // from all related entities find only those which aren't found in the db - for them we will create operation subjects\n relatedEntities.forEach((relatedEntity) => {\n // by example: relatedEntity is category from categories saved with post\n\n // todo: check how it will work for entities which are saved by cascades, but aren't saved in the database yet\n\n // extract only relation id from the related entities, since we only need it for comparision\n // by example: extract from category only relation id (category id, or let's say category title, depend on join column options)\n let relatedEntityRelationIdMap =\n relation.inverseEntityMetadata!.getEntityIdMap(relatedEntity)\n\n // try to find a subject of this related entity, maybe it was loaded or was marked for persistence\n const relatedEntitySubject = this.subjects.find((subject) => {\n return subject.entity === relatedEntity\n })\n\n // if subject with entity was found take subject identifier as relation id map since it may contain extra properties resolved\n if (relatedEntitySubject)\n relatedEntityRelationIdMap = relatedEntitySubject.identifier\n\n // if related entity relation id map is empty it means related entity is newly persisted\n if (!relatedEntityRelationIdMap) {\n // we decided to remove this error because it brings complications when saving object with non-saved entities\n // if related entity does not have a subject then it means user tries to bind entity which wasn't saved\n // in this persistence because he didn't pass this entity for save or he did not set cascades\n // but without entity being inserted we cannot bind it in the relation operation, so we throw an exception here\n // we decided to remove this error because it brings complications when saving object with non-saved entities\n // if (!relatedEntitySubject)\n // throw new TypeORMError(`Many-to-many relation \"${relation.entityMetadata.name}.${relation.propertyPath}\" contains ` +\n // `entities which do not exist in the database yet, thus they cannot be bind in the database. ` +\n // `Please setup cascade insertion or save entities before binding it.`);\n if (!relatedEntitySubject) return\n }\n\n // try to find related entity in the database\n // by example: find post's category in the database post's categories\n const relatedEntityExistInDatabase = databaseRelatedEntityIds.find(\n (databaseRelatedEntityRelationId) => {\n return OrmUtils.compareIds(\n databaseRelatedEntityRelationId,\n relatedEntityRelationIdMap,\n )\n },\n )\n\n // if entity is found then don't do anything - it means binding in junction table already exist, we don't need to add anything\n if (relatedEntityExistInDatabase) return\n\n const ownerValue = relation.isOwning\n ? subject\n : relatedEntitySubject || relatedEntity // by example: ownerEntityMap is post from subject here\n const inverseValue = relation.isOwning\n ? relatedEntitySubject || relatedEntity\n : subject // by example: inverseEntityMap is category from categories array here\n\n // create a new subject for insert operation of junction rows\n const junctionSubject = new Subject({\n metadata: relation.junctionEntityMetadata!,\n parentSubject: subject,\n canBeInserted: true,\n })\n this.subjects.push(junctionSubject)\n\n relation.junctionEntityMetadata!.ownerColumns.forEach((column) => {\n junctionSubject.changeMaps.push({\n column: column,\n value: ownerValue,\n // valueFactory: (value) => column.referencedColumn!.getEntityValue(value) // column.referencedColumn!.getEntityValue(ownerEntityMap),\n })\n })\n\n relation.junctionEntityMetadata!.inverseColumns.forEach(\n (column) => {\n junctionSubject.changeMaps.push({\n column: column,\n value: inverseValue,\n // valueFactory: (value) => column.referencedColumn!.getEntityValue(value) // column.referencedColumn!.getEntityValue(inverseEntityMap),\n })\n },\n )\n })\n\n // get all inverse entities relation ids that are \"bind\" to the currently persisted entity\n const changedInverseEntityRelationIds: ObjectLiteral[] = []\n relatedEntities.forEach((relatedEntity) => {\n // relation.inverseEntityMetadata!.getEntityIdMap(relatedEntity)\n let relatedEntityRelationIdMap =\n relation.inverseEntityMetadata!.getEntityIdMap(relatedEntity)\n\n // try to find a subject of this related entity, maybe it was loaded or was marked for persistence\n const relatedEntitySubject = this.subjects.find((subject) => {\n return subject.entity === relatedEntity\n })\n\n // if subject with entity was found take subject identifier as relation id map since it may contain extra properties resolved\n if (relatedEntitySubject)\n relatedEntityRelationIdMap = relatedEntitySubject.identifier\n\n if (\n relatedEntityRelationIdMap !== undefined &&\n relatedEntityRelationIdMap !== null\n )\n changedInverseEntityRelationIds.push(relatedEntityRelationIdMap)\n })\n\n // now from all entities in the persisted entity find only those which aren't found in the db\n const removedJunctionEntityIds = databaseRelatedEntityIds.filter(\n (existRelationId) => {\n return !changedInverseEntityRelationIds.find(\n (changedRelationId) => {\n return OrmUtils.compareIds(\n changedRelationId,\n existRelationId,\n )\n },\n )\n },\n )\n\n // finally create a new junction remove operations for missing related entities\n removedJunctionEntityIds.forEach((removedEntityRelationId) => {\n const junctionSubject = new Subject({\n metadata: relation.junctionEntityMetadata!,\n parentSubject: subject,\n mustBeRemoved: true,\n identifier: this.buildJunctionIdentifier(\n subject,\n relation,\n removedEntityRelationId,\n ),\n })\n this.subjects.push(junctionSubject)\n })\n }\n\n /**\n * Creates identifiers for junction table.\n * Example: { postId: 1, categoryId: 2 }\n */\n protected buildJunctionIdentifier(\n subject: Subject,\n relation: RelationMetadata,\n relationId: ObjectLiteral,\n ) {\n const ownerEntityMap = relation.isOwning ? subject.entity! : relationId\n const inverseEntityMap = relation.isOwning\n ? relationId\n : subject.entity!\n\n const identifier: ObjectLiteral = {}\n relation.junctionEntityMetadata!.ownerColumns.forEach((column) => {\n OrmUtils.mergeDeep(\n identifier,\n column.createValueMap(\n column.referencedColumn!.getEntityValue(ownerEntityMap),\n ),\n )\n })\n relation.junctionEntityMetadata!.inverseColumns.forEach((column) => {\n OrmUtils.mergeDeep(\n identifier,\n column.createValueMap(\n column.referencedColumn!.getEntityValue(inverseEntityMap),\n ),\n )\n })\n return identifier\n }\n}\n"],"sourceRoot":"../.."}
1
+ {"version":3,"sources":["../../src/persistence/subject-builder/ManyToManySubjectBuilder.ts"],"names":[],"mappings":";;;AAAA,wCAAoC;AACpC,kDAA8C;AAI9C;;;;;;;GAOG;AACH,MAAa,wBAAwB;IACjC,wEAAwE;IACxE,cAAc;IACd,wEAAwE;IAExE,YAAsB,QAAmB;QAAnB,aAAQ,GAAR,QAAQ,CAAW;IAAG,CAAC;IAE7C,wEAAwE;IACxE,iBAAiB;IACjB,wEAAwE;IAExE;;OAEG;IACH,KAAK;QACD,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC9B,mGAAmG;YACnG,IAAI,CAAC,OAAO,CAAC,MAAM;gBAAE,OAAM;YAE3B,kGAAkG;YAClG,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;gBACtD,mDAAmD;gBACnD,IAAI,QAAQ,CAAC,kBAAkB,KAAK,KAAK;oBAAE,OAAM;gBAEjD,IAAI,CAAC,uBAAuB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAA;YACn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,UAAU,CAAA;QACvE,MAAM,gBAAgB,GAAG,QAAQ,CAAC,QAAQ;YACtC,CAAC,CAAC,UAAU;YACZ,CAAC,CAAC,OAAO,CAAC,MAAO,CAAA;QAErB,MAAM,UAAU,GAAkB,EAAE,CAAA;QACpC,QAAQ,CAAC,sBAAuB,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE;YAC7D,mBAAQ,CAAC,SAAS,CACd,UAAU,EACV,MAAM,CAAC,cAAc,CACjB,MAAM,CAAC,gBAAiB,CAAC,cAAc,CAAC,cAAc,CAAC,CAC1D,CACJ,CAAA;QACL,CAAC,CAAC,CAAA;QACF,QAAQ,CAAC,sBAAuB,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE;YAC/D,mBAAQ,CAAC,SAAS,CACd,UAAU,EACV,MAAM,CAAC,cAAc,CACjB,MAAM,CAAC,gBAAiB,CAAC,cAAc,CAAC,gBAAgB,CAAC,CAC5D,CACJ,CAAA;QACL,CAAC,CAAC,CAAA;QACF,OAAO,UAAU,CAAA;IACrB,CAAC;CACJ;AA9QD,4DA8QC","file":"ManyToManySubjectBuilder.js","sourcesContent":["import { Subject } from \"../Subject\"\nimport { OrmUtils } from \"../../util/OrmUtils\"\nimport { ObjectLiteral } from \"../../common/ObjectLiteral\"\nimport { RelationMetadata } from \"../../metadata/RelationMetadata\"\n\n/**\n * Builds operations needs to be executed for many-to-many relations of the given subjects.\n *\n * by example: post contains owner many-to-many relation with categories in the property called \"categories\", e.g.\n * @ManyToMany(type => Category, category => category.posts) categories: Category[]\n * If user adds categories into the post and saves post we need to bind them.\n * This operation requires updation of junction table.\n */\nexport class ManyToManySubjectBuilder {\n // ---------------------------------------------------------------------\n // Constructor\n // ---------------------------------------------------------------------\n\n constructor(protected subjects: Subject[]) {}\n\n // ---------------------------------------------------------------------\n // Public Methods\n // ---------------------------------------------------------------------\n\n /**\n * Builds operations for any changes in the many-to-many relations of the subjects.\n */\n build(): void {\n this.subjects.forEach((subject) => {\n // if subject doesn't have entity then no need to find something that should be inserted or removed\n if (!subject.entity) return\n\n // go through all persistence enabled many-to-many relations and build subject operations for them\n subject.metadata.manyToManyRelations.forEach((relation) => {\n // skip relations for which persistence is disabled\n if (relation.persistenceEnabled === false) return\n\n this.buildForSubjectRelation(subject, relation)\n })\n })\n }\n\n /**\n * Builds operations for removal of all many-to-many records of all many-to-many relations of the given subject.\n */\n buildForAllRemoval(subject: Subject) {\n // if subject does not have a database entity then it means it does not exist in the database\n // if it does not exist in the database then we don't have anything for deletion\n if (!subject.databaseEntity) return\n\n // go through all persistence enabled many-to-many relations and build subject operations for them\n subject.metadata.manyToManyRelations.forEach((relation) => {\n // skip relations for which persistence is disabled\n if (relation.persistenceEnabled === false) return\n\n // get all related entities (actually related entity relation ids) bind to this subject entity\n // by example: returns category ids of the post we are currently working with (subject.entity is post)\n const relatedEntityRelationIdsInDatabase: ObjectLiteral[] =\n relation.getEntityValue(subject.databaseEntity!)\n\n // go through all related entities and create a new junction subject for each row in junction table\n relatedEntityRelationIdsInDatabase.forEach((relationId) => {\n const junctionSubject = new Subject({\n metadata: relation.junctionEntityMetadata!,\n parentSubject: subject,\n mustBeRemoved: true,\n identifier: this.buildJunctionIdentifier(\n subject,\n relation,\n relationId,\n ),\n })\n\n // we use unshift because we need to perform those operations before post deletion is performed\n // but post deletion was already added as an subject\n // this is temporary solution, later we need to implement proper sorting of subjects before their removal\n this.subjects.push(junctionSubject)\n })\n })\n }\n\n // ---------------------------------------------------------------------\n // Protected Methods\n // ---------------------------------------------------------------------\n\n /**\n * Builds operations for a given subject and relation.\n *\n * by example: subject is \"post\" entity we are saving here and relation is \"categories\" inside it here.\n */\n protected buildForSubjectRelation(\n subject: Subject,\n relation: RelationMetadata,\n ) {\n // load from db all relation ids of inverse entities that are \"bind\" to the subject's entity\n // this way we gonna check which relation ids are missing and which are new (e.g. inserted or removed)\n let databaseRelatedEntityIds: ObjectLiteral[] = []\n\n // if subject don't have database entity it means all related entities in persisted subject are new and must be bind\n // and we don't need to remove something that is not exist\n if (subject.databaseEntity)\n databaseRelatedEntityIds = relation.getEntityValue(\n subject.databaseEntity,\n )\n\n // extract entity's relation value\n // by example: categories inside our post (subject.entity is post)\n let relatedEntities: ObjectLiteral[] = relation.getEntityValue(\n subject.entity!,\n )\n if (relatedEntities === null)\n // if value set to null its equal if we set it to empty array - all items must be removed from the database\n relatedEntities = []\n if (!Array.isArray(relatedEntities)) return\n\n // from all related entities find only those which aren't found in the db - for them we will create operation subjects\n relatedEntities.forEach((relatedEntity) => {\n // by example: relatedEntity is category from categories saved with post\n\n // todo: check how it will work for entities which are saved by cascades, but aren't saved in the database yet\n\n // extract only relation id from the related entities, since we only need it for comparison\n // by example: extract from category only relation id (category id, or let's say category title, depend on join column options)\n let relatedEntityRelationIdMap =\n relation.inverseEntityMetadata!.getEntityIdMap(relatedEntity)\n\n // try to find a subject of this related entity, maybe it was loaded or was marked for persistence\n const relatedEntitySubject = this.subjects.find((subject) => {\n return subject.entity === relatedEntity\n })\n\n // if subject with entity was found take subject identifier as relation id map since it may contain extra properties resolved\n if (relatedEntitySubject)\n relatedEntityRelationIdMap = relatedEntitySubject.identifier\n\n // if related entity relation id map is empty it means related entity is newly persisted\n if (!relatedEntityRelationIdMap) {\n // we decided to remove this error because it brings complications when saving object with non-saved entities\n // if related entity does not have a subject then it means user tries to bind entity which wasn't saved\n // in this persistence because he didn't pass this entity for save or he did not set cascades\n // but without entity being inserted we cannot bind it in the relation operation, so we throw an exception here\n // we decided to remove this error because it brings complications when saving object with non-saved entities\n // if (!relatedEntitySubject)\n // throw new TypeORMError(`Many-to-many relation \"${relation.entityMetadata.name}.${relation.propertyPath}\" contains ` +\n // `entities which do not exist in the database yet, thus they cannot be bind in the database. ` +\n // `Please setup cascade insertion or save entities before binding it.`);\n if (!relatedEntitySubject) return\n }\n\n // try to find related entity in the database\n // by example: find post's category in the database post's categories\n const relatedEntityExistInDatabase = databaseRelatedEntityIds.find(\n (databaseRelatedEntityRelationId) => {\n return OrmUtils.compareIds(\n databaseRelatedEntityRelationId,\n relatedEntityRelationIdMap,\n )\n },\n )\n\n // if entity is found then don't do anything - it means binding in junction table already exist, we don't need to add anything\n if (relatedEntityExistInDatabase) return\n\n const ownerValue = relation.isOwning\n ? subject\n : relatedEntitySubject || relatedEntity // by example: ownerEntityMap is post from subject here\n const inverseValue = relation.isOwning\n ? relatedEntitySubject || relatedEntity\n : subject // by example: inverseEntityMap is category from categories array here\n\n // create a new subject for insert operation of junction rows\n const junctionSubject = new Subject({\n metadata: relation.junctionEntityMetadata!,\n parentSubject: subject,\n canBeInserted: true,\n })\n this.subjects.push(junctionSubject)\n\n relation.junctionEntityMetadata!.ownerColumns.forEach((column) => {\n junctionSubject.changeMaps.push({\n column: column,\n value: ownerValue,\n // valueFactory: (value) => column.referencedColumn!.getEntityValue(value) // column.referencedColumn!.getEntityValue(ownerEntityMap),\n })\n })\n\n relation.junctionEntityMetadata!.inverseColumns.forEach(\n (column) => {\n junctionSubject.changeMaps.push({\n column: column,\n value: inverseValue,\n // valueFactory: (value) => column.referencedColumn!.getEntityValue(value) // column.referencedColumn!.getEntityValue(inverseEntityMap),\n })\n },\n )\n })\n\n // get all inverse entities relation ids that are \"bind\" to the currently persisted entity\n const changedInverseEntityRelationIds: ObjectLiteral[] = []\n relatedEntities.forEach((relatedEntity) => {\n // relation.inverseEntityMetadata!.getEntityIdMap(relatedEntity)\n let relatedEntityRelationIdMap =\n relation.inverseEntityMetadata!.getEntityIdMap(relatedEntity)\n\n // try to find a subject of this related entity, maybe it was loaded or was marked for persistence\n const relatedEntitySubject = this.subjects.find((subject) => {\n return subject.entity === relatedEntity\n })\n\n // if subject with entity was found take subject identifier as relation id map since it may contain extra properties resolved\n if (relatedEntitySubject)\n relatedEntityRelationIdMap = relatedEntitySubject.identifier\n\n if (\n relatedEntityRelationIdMap !== undefined &&\n relatedEntityRelationIdMap !== null\n )\n changedInverseEntityRelationIds.push(relatedEntityRelationIdMap)\n })\n\n // now from all entities in the persisted entity find only those which aren't found in the db\n const removedJunctionEntityIds = databaseRelatedEntityIds.filter(\n (existRelationId) => {\n return !changedInverseEntityRelationIds.find(\n (changedRelationId) => {\n return OrmUtils.compareIds(\n changedRelationId,\n existRelationId,\n )\n },\n )\n },\n )\n\n // finally create a new junction remove operations for missing related entities\n removedJunctionEntityIds.forEach((removedEntityRelationId) => {\n const junctionSubject = new Subject({\n metadata: relation.junctionEntityMetadata!,\n parentSubject: subject,\n mustBeRemoved: true,\n identifier: this.buildJunctionIdentifier(\n subject,\n relation,\n removedEntityRelationId,\n ),\n })\n this.subjects.push(junctionSubject)\n })\n }\n\n /**\n * Creates identifiers for junction table.\n * Example: { postId: 1, categoryId: 2 }\n */\n protected buildJunctionIdentifier(\n subject: Subject,\n relation: RelationMetadata,\n relationId: ObjectLiteral,\n ) {\n const ownerEntityMap = relation.isOwning ? subject.entity! : relationId\n const inverseEntityMap = relation.isOwning\n ? relationId\n : subject.entity!\n\n const identifier: ObjectLiteral = {}\n relation.junctionEntityMetadata!.ownerColumns.forEach((column) => {\n OrmUtils.mergeDeep(\n identifier,\n column.createValueMap(\n column.referencedColumn!.getEntityValue(ownerEntityMap),\n ),\n )\n })\n relation.junctionEntityMetadata!.inverseColumns.forEach((column) => {\n OrmUtils.mergeDeep(\n identifier,\n column.createValueMap(\n column.referencedColumn!.getEntityValue(inverseEntityMap),\n ),\n )\n })\n return identifier\n }\n}\n"],"sourceRoot":"../.."}
@@ -86,7 +86,7 @@ class OneToOneInverseSideSubjectBuilder {
86
86
  }
87
87
  return;
88
88
  } // else means entity is bind in the database
89
- // extract only relation id from the related entities, since we only need it for comparision
89
+ // extract only relation id from the related entities, since we only need it for comparison
90
90
  // by example: extract from category only relation id (category id, or let's say category title, depend on join column options)
91
91
  let relationIdMap = relation.inverseEntityMetadata.getEntityIdMap(relatedEntity); // by example: relationIdMap is category.id map here, e.g. { id: ... }
92
92
  // try to find a subject of this related entity, maybe it was loaded or was marked for persistence
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/persistence/subject-builder/OneToOneInverseSideSubjectBuilder.ts"],"names":[],"mappings":";;;AAAA,wCAAoC;AACpC,kDAA8C;AAI9C;;;;;;;;;;GAUG;AACH,MAAa,iCAAiC;IAC1C,wEAAwE;IACxE,cAAc;IACd,wEAAwE;IAExE,YAAsB,QAAmB;QAAnB,aAAQ,GAAR,QAAQ,CAAW;IAAG,CAAC;IAE7C,wEAAwE;IACxE,iBAAiB;IACjB,wEAAwE;IAExE;;OAEG;IACH,KAAK;QACD,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC9B,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;gBACpD,kGAAkG;gBAClG,mDAAmD;gBACnD,IAAI,QAAQ,CAAC,QAAQ,IAAI,QAAQ,CAAC,kBAAkB,KAAK,KAAK;oBAC1D,OAAM;gBAEV,IAAI,CAAC,uBAAuB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAA;YACnD,CAAC,CAAC,CAAA;QACN,CAAC,CAAC,CAAA;IACN,CAAC;IAED,wEAAwE;IACxE,oBAAoB;IACpB,wEAAwE;IAExE;;;;OAIG;IACO,uBAAuB,CAC7B,OAAgB,EAChB,QAA0B;QAE1B,4DAA4D;QAC5D,uFAAuF;QACvF,2GAA2G;QAC3G,oFAAoF;QACpF,IAAI,+BAA+B,GAC/B,SAAS,CAAA;QACb,IAAI,OAAO,CAAC,cAAc;YACtB,+EAA+E;YAC/E,+BAA+B,GAAG,QAAQ,CAAC,cAAc,CACrD,OAAO,CAAC,cAAc,CACzB,CAAA;QAEL,2CAA2C;QAC3C,kEAAkE;QAClE,IAAI,aAAa,GAAyB,QAAQ,CAAC,cAAc,CAC7D,OAAO,CAAC,MAAO,CAClB,CAAA,CAAC,+CAA+C;QACjD,IAAI,aAAa,KAAK,SAAS;YAC3B,kDAAkD;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CAAC,oBAAoB;gBAAE,OAAM;YAEjC,yFAAyF;YACzF,oGAAoG;YACpG,qEAAqE;YACrE,kFAAkF;YAClF,iEAAiE;YACjE,oBAAoB,CAAC,UAAU,CAAC,IAAI,CAAC;gBACjC,QAAQ,EAAE,QAAQ,CAAC,eAAgB;gBACnC,KAAK,EAAE,OAAO;aACjB,CAAC,CAAA;SACL;QAED,qDAAqD;QACrD,wHAAwH;QACxH,MAAM,6BAA6B,GAC/B,+BAA+B;YAC/B,mBAAQ,CAAC,UAAU,CAAC,aAAa,EAAE,+BAA+B,CAAC,CAAA;QAEvE,8EAA8E;QAC9E,oGAAoG;QACpG,qEAAqE;QACrE,6EAA6E;QAC7E,iEAAiE;QACjE,IAAI,CAAC,6BAA6B,EAAE;YAChC,iFAAiF;YACjF,4GAA4G;YAC5G,gCAAgC;YAChC,IAAI,CAAC,oBAAoB,EAAE;gBACvB,oBAAoB,GAAG,IAAI,iBAAO,CAAC;oBAC/B,QAAQ,EAAE,QAAQ,CAAC,qBAAqB;oBACxC,YAAY,EAAE,IAAI;oBAClB,UAAU,EAAE,aAAa;iBAC5B,CAAC,CAAA;gBACF,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAA;aAC3C;YAED,oBAAoB,CAAC,UAAU,CAAC,IAAI,CAAC;gBACjC,QAAQ,EAAE,QAAQ,CAAC,eAAgB;gBACnC,KAAK,EAAE,OAAO;aACjB,CAAC,CAAA;SACL;IACL,CAAC;CACJ;AAlKD,8EAkKC","file":"OneToOneInverseSideSubjectBuilder.js","sourcesContent":["import { Subject } from \"../Subject\"\nimport { OrmUtils } from \"../../util/OrmUtils\"\nimport { ObjectLiteral } from \"../../common/ObjectLiteral\"\nimport { RelationMetadata } from \"../../metadata/RelationMetadata\"\n\n/**\n * Builds operations needs to be executed for one-to-one non-owner relations of the given subjects.\n *\n * by example: post contains one-to-one non-owner relation with category in the property called \"category\", e.g.\n * @OneToOne(type => Category, category => category.post) category: Category\n * If user sets a category into the post and saves post we need to bind them.\n * This operation requires updation of category table since its owner of the relation and contains a join column.\n *\n * note: this class shares lot of things with OneToManyUpdateBuilder, so when you change this class\n * make sure to reflect changes there as well.\n */\nexport class OneToOneInverseSideSubjectBuilder {\n // ---------------------------------------------------------------------\n // Constructor\n // ---------------------------------------------------------------------\n\n constructor(protected subjects: Subject[]) {}\n\n // ---------------------------------------------------------------------\n // Public Methods\n // ---------------------------------------------------------------------\n\n /**\n * Builds all required operations.\n */\n build(): void {\n this.subjects.forEach((subject) => {\n subject.metadata.oneToOneRelations.forEach((relation) => {\n // we don't need owning relations, this operation is only for inverse side of one-to-one relations\n // skip relations for which persistence is disabled\n if (relation.isOwning || relation.persistenceEnabled === false)\n return\n\n this.buildForSubjectRelation(subject, relation)\n })\n })\n }\n\n // ---------------------------------------------------------------------\n // Protected Methods\n // ---------------------------------------------------------------------\n\n /**\n * Builds operations for a given subject and relation.\n *\n * by example: subject is \"post\" entity we are saving here and relation is \"category\" inside it here.\n */\n protected buildForSubjectRelation(\n subject: Subject,\n relation: RelationMetadata,\n ) {\n // prepare objects (relation id map) for the database entity\n // note: subject.databaseEntity contains relation with loaded relation id only (id map)\n // by example: since subject is a post, we are expecting to get post's category saved in the database here,\n // particularly its relation id, e.g. category id stored in the database\n let relatedEntityDatabaseRelationId: ObjectLiteral | undefined =\n undefined\n if (subject.databaseEntity)\n // related entity in the database can exist only if this entity (post) is saved\n relatedEntityDatabaseRelationId = relation.getEntityValue(\n subject.databaseEntity,\n )\n\n // get related entities of persisted entity\n // by example: get category from the passed to persist post entity\n let relatedEntity: ObjectLiteral | null = relation.getEntityValue(\n subject.entity!,\n ) // by example: relatedEntity is a category here\n if (relatedEntity === undefined)\n // if relation is undefined then nothing to update\n return\n\n // if related entity is null then we need to check if there a bind in the database and unset it\n // if there is no bind in the entity then we don't need to do anything\n // by example: if post.category = null and category has this post in the database then we unset it\n if (relatedEntity === null) {\n // it makes sense to update database only there is a previously set value in the database\n if (relatedEntityDatabaseRelationId) {\n // todo: probably we can improve this in the future by finding entity with column those values,\n // todo: maybe it was already in persistence process. This is possible due to unique requirements of join columns\n // we create a new subject which operations will be executed in subject operation executor\n\n const removedRelatedEntitySubject = new Subject({\n metadata: relation.inverseEntityMetadata,\n parentSubject: subject,\n canBeUpdated: true,\n identifier: relatedEntityDatabaseRelationId,\n changeMaps: [\n {\n relation: relation.inverseRelation!,\n value: null,\n },\n ],\n })\n this.subjects.push(removedRelatedEntitySubject)\n }\n\n return\n } // else means entity is bind in the database\n\n // extract only relation id from the related entities, since we only need it for comparision\n // by example: extract from category only relation id (category id, or let's say category title, depend on join column options)\n let relationIdMap =\n relation.inverseEntityMetadata!.getEntityIdMap(relatedEntity) // by example: relationIdMap is category.id map here, e.g. { id: ... }\n\n // try to find a subject of this related entity, maybe it was loaded or was marked for persistence\n let relatedEntitySubject = this.subjects.find((operateSubject) => {\n return (\n !!operateSubject.entity &&\n operateSubject.entity === relatedEntity\n )\n })\n\n // if subject with entity was found take subject identifier as relation id map since it may contain extra properties resolved\n if (relatedEntitySubject)\n relationIdMap = relatedEntitySubject.identifier\n\n // if relationIdMap is undefined then it means user binds object which is not saved in the database yet\n // by example: if post contains category which does not have id(s) yet (because its a new category)\n // it means its always newly inserted and relation update operation always must be created for it\n // it does not make sense to perform difference operation for it for both add and remove actions\n if (!relationIdMap) {\n // we decided to remove this error because it brings complications when saving object with non-saved entities\n // if related entity does not have a subject then it means user tries to bind entity which wasn't saved\n // in this persistence because he didn't pass this entity for save or he did not set cascades\n // but without entity being inserted we cannot bind it in the relation operation, so we throw an exception here\n // if (!relatedEntitySubject)\n // throw new TypeORMError(`One-to-one inverse relation \"${relation.entityMetadata.name}.${relation.propertyPath}\" contains ` +\n // `entity which does not exist in the database yet, thus cannot be bind in the database. ` +\n // `Please setup cascade insertion or save entity before binding it.`);\n if (!relatedEntitySubject) return\n\n // okay, so related subject exist and its marked for insertion, then add a new change map\n // by example: this will tell category to insert into its post relation our post we are working with\n // relatedEntitySubject is newly inserted CategorySubject\n // relation.inverseRelation is OneToOne owner relation inside Category\n // subject is Post needs to be inserted into Category\n relatedEntitySubject.changeMaps.push({\n relation: relation.inverseRelation!,\n value: subject,\n })\n }\n\n // check if this binding really exist in the database\n // by example: find our post if its already bind to category in the database and its not equal to what user tries to set\n const areRelatedIdEqualWithDatabase =\n relatedEntityDatabaseRelationId &&\n OrmUtils.compareIds(relationIdMap, relatedEntityDatabaseRelationId)\n\n // if they aren't equal it means its a new relation and we need to \"bind\" them\n // by example: this will tell category to insert into its post relation our post we are working with\n // relatedEntitySubject is newly inserted CategorySubject\n // relation.inverseRelation is ManyToOne relation inside Category\n // subject is Post needs to be inserted into Category\n if (!areRelatedIdEqualWithDatabase) {\n // if there is no relatedEntitySubject then it means \"category\" wasn't persisted,\n // but since we are going to update \"category\" table (since its an owning side of relation with join column)\n // we create a new subject here:\n if (!relatedEntitySubject) {\n relatedEntitySubject = new Subject({\n metadata: relation.inverseEntityMetadata,\n canBeUpdated: true,\n identifier: relationIdMap,\n })\n this.subjects.push(relatedEntitySubject)\n }\n\n relatedEntitySubject.changeMaps.push({\n relation: relation.inverseRelation!,\n value: subject,\n })\n }\n }\n}\n"],"sourceRoot":"../.."}
1
+ {"version":3,"sources":["../../src/persistence/subject-builder/OneToOneInverseSideSubjectBuilder.ts"],"names":[],"mappings":";;;AAAA,wCAAoC;AACpC,kDAA8C;AAI9C;;;;;;;;;;GAUG;AACH,MAAa,iCAAiC;IAC1C,wEAAwE;IACxE,cAAc;IACd,wEAAwE;IAExE,YAAsB,QAAmB;QAAnB,aAAQ,GAAR,QAAQ,CAAW;IAAG,CAAC;IAE7C,wEAAwE;IACxE,iBAAiB;IACjB,wEAAwE;IAExE;;OAEG;IACH,KAAK;QACD,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC9B,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;g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G;YAC5G,gCAAgC;YAChC,IAAI,CAAC,oBAAoB,EAAE;gBACvB,oBAAoB,GAAG,IAAI,iBAAO,CAAC;oBAC/B,QAAQ,EAAE,QAAQ,CAAC,qBAAqB;oBACxC,YAAY,EAAE,IAAI;oBAClB,UAAU,EAAE,aAAa;iBAC5B,CAAC,CAAA;gBACF,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAA;aAC3C;YAED,oBAAoB,CAAC,UAAU,CAAC,IAAI,CAAC;gBACjC,QAAQ,EAAE,QAAQ,CAAC,eAAgB;gBACnC,KAAK,EAAE,OAAO;aACjB,CAAC,CAAA;SACL;IACL,CAAC;CACJ;AAlKD,8EAkKC","file":"OneToOneInverseSideSubjectBuilder.js","sourcesContent":["import { Subject } from \"../Subject\"\nimport { OrmUtils } from \"../../util/OrmUtils\"\nimport { ObjectLiteral } from \"../../common/ObjectLiteral\"\nimport { RelationMetadata } from \"../../metadata/RelationMetadata\"\n\n/**\n * Builds operations needs to be executed for one-to-one non-owner relations of the given subjects.\n *\n * by example: post contains one-to-one non-owner relation with category in the property called \"category\", e.g.\n * @OneToOne(type => Category, category => category.post) category: Category\n * If user sets a category into the post and saves post we need to bind them.\n * This operation requires updation of category table since its owner of the relation and contains a join column.\n *\n * note: this class shares lot of things with OneToManyUpdateBuilder, so when you change this class\n * make sure to reflect changes there as well.\n */\nexport class OneToOneInverseSideSubjectBuilder {\n // ---------------------------------------------------------------------\n // Constructor\n // ---------------------------------------------------------------------\n\n constructor(protected subjects: Subject[]) {}\n\n // ---------------------------------------------------------------------\n // Public Methods\n // ---------------------------------------------------------------------\n\n /**\n * Builds all required operations.\n */\n build(): void {\n this.subjects.forEach((subject) => {\n subject.metadata.oneToOneRelations.forEach((relation) => {\n // we don't need owning relations, this operation is only for inverse side of one-to-one relations\n // skip relations for which persistence is disabled\n if (relation.isOwning || relation.persistenceEnabled === false)\n return\n\n this.buildForSubjectRelation(subject, relation)\n })\n })\n }\n\n // ---------------------------------------------------------------------\n // Protected Methods\n // ---------------------------------------------------------------------\n\n /**\n * Builds operations for a given subject and relation.\n *\n * by example: subject is \"post\" entity we are saving here and relation is \"category\" inside it here.\n */\n protected buildForSubjectRelation(\n subject: Subject,\n relation: RelationMetadata,\n ) {\n // prepare objects (relation id map) for the database entity\n // note: subject.databaseEntity contains relation with loaded relation id only (id map)\n // by example: since subject is a post, we are expecting to get post's category saved in the database here,\n // particularly its relation id, e.g. category id stored in the database\n let relatedEntityDatabaseRelationId: ObjectLiteral | undefined =\n undefined\n if (subject.databaseEntity)\n // related entity in the database can exist only if this entity (post) is saved\n relatedEntityDatabaseRelationId = relation.getEntityValue(\n subject.databaseEntity,\n )\n\n // get related entities of persisted entity\n // by example: get category from the passed to persist post entity\n let relatedEntity: ObjectLiteral | null = relation.getEntityValue(\n subject.entity!,\n ) // by example: relatedEntity is a category here\n if (relatedEntity === undefined)\n // if relation is undefined then nothing to update\n return\n\n // if related entity is null then we need to check if there a bind in the database and unset it\n // if there is no bind in the entity then we don't need to do anything\n // by example: if post.category = null and category has this post in the database then we unset it\n if (relatedEntity === null) {\n // it makes sense to update database only there is a previously set value in the database\n if (relatedEntityDatabaseRelationId) {\n // todo: probably we can improve this in the future by finding entity with column those values,\n // todo: maybe it was already in persistence process. This is possible due to unique requirements of join columns\n // we create a new subject which operations will be executed in subject operation executor\n\n const removedRelatedEntitySubject = new Subject({\n metadata: relation.inverseEntityMetadata,\n parentSubject: subject,\n canBeUpdated: true,\n identifier: relatedEntityDatabaseRelationId,\n changeMaps: [\n {\n relation: relation.inverseRelation!,\n value: null,\n },\n ],\n })\n this.subjects.push(removedRelatedEntitySubject)\n }\n\n return\n } // else means entity is bind in the database\n\n // extract only relation id from the related entities, since we only need it for comparison\n // by example: extract from category only relation id (category id, or let's say category title, depend on join column options)\n let relationIdMap =\n relation.inverseEntityMetadata!.getEntityIdMap(relatedEntity) // by example: relationIdMap is category.id map here, e.g. { id: ... }\n\n // try to find a subject of this related entity, maybe it was loaded or was marked for persistence\n let relatedEntitySubject = this.subjects.find((operateSubject) => {\n return (\n !!operateSubject.entity &&\n operateSubject.entity === relatedEntity\n )\n })\n\n // if subject with entity was found take subject identifier as relation id map since it may contain extra properties resolved\n if (relatedEntitySubject)\n relationIdMap = relatedEntitySubject.identifier\n\n // if relationIdMap is undefined then it means user binds object which is not saved in the database yet\n // by example: if post contains category which does not have id(s) yet (because its a new category)\n // it means its always newly inserted and relation update operation always must be created for it\n // it does not make sense to perform difference operation for it for both add and remove actions\n if (!relationIdMap) {\n // we decided to remove this error because it brings complications when saving object with non-saved entities\n // if related entity does not have a subject then it means user tries to bind entity which wasn't saved\n // in this persistence because he didn't pass this entity for save or he did not set cascades\n // but without entity being inserted we cannot bind it in the relation operation, so we throw an exception here\n // if (!relatedEntitySubject)\n // throw new TypeORMError(`One-to-one inverse relation \"${relation.entityMetadata.name}.${relation.propertyPath}\" contains ` +\n // `entity which does not exist in the database yet, thus cannot be bind in the database. ` +\n // `Please setup cascade insertion or save entity before binding it.`);\n if (!relatedEntitySubject) return\n\n // okay, so related subject exist and its marked for insertion, then add a new change map\n // by example: this will tell category to insert into its post relation our post we are working with\n // relatedEntitySubject is newly inserted CategorySubject\n // relation.inverseRelation is OneToOne owner relation inside Category\n // subject is Post needs to be inserted into Category\n relatedEntitySubject.changeMaps.push({\n relation: relation.inverseRelation!,\n value: subject,\n })\n }\n\n // check if this binding really exist in the database\n // by example: find our post if its already bind to category in the database and its not equal to what user tries to set\n const areRelatedIdEqualWithDatabase =\n relatedEntityDatabaseRelationId &&\n OrmUtils.compareIds(relationIdMap, relatedEntityDatabaseRelationId)\n\n // if they aren't equal it means its a new relation and we need to \"bind\" them\n // by example: this will tell category to insert into its post relation our post we are working with\n // relatedEntitySubject is newly inserted CategorySubject\n // relation.inverseRelation is ManyToOne relation inside Category\n // subject is Post needs to be inserted into Category\n if (!areRelatedIdEqualWithDatabase) {\n // if there is no relatedEntitySubject then it means \"category\" wasn't persisted,\n // but since we are going to update \"category\" table (since its an owning side of relation with join column)\n // we create a new subject here:\n if (!relatedEntitySubject) {\n relatedEntitySubject = new Subject({\n metadata: relation.inverseEntityMetadata,\n canBeUpdated: true,\n identifier: relationIdMap,\n })\n this.subjects.push(relatedEntitySubject)\n }\n\n relatedEntitySubject.changeMaps.push({\n relation: relation.inverseRelation!,\n value: subject,\n })\n }\n }\n}\n"],"sourceRoot":"../.."}
@@ -129,7 +129,7 @@ export declare abstract class QueryBuilder<Entity> {
129
129
  /**
130
130
  * Sets parameter name and its value.
131
131
  *
132
- * The key for this parametere may contain numbers, letters, underscores, or periods.
132
+ * The key for this parameter may contain numbers, letters, underscores, or periods.
133
133
  */
134
134
  setParameter(key: string, value: any): this;
135
135
  /**
@@ -184,7 +184,7 @@ class QueryBuilder {
184
184
  /**
185
185
  * Sets parameter name and its value.
186
186
  *
187
- * The key for this parametere may contain numbers, letters, underscores, or periods.
187
+ * The key for this parameter may contain numbers, letters, underscores, or periods.
188
188
  */
189
189
  setParameter(key, value) {
190
190
  if (typeof value === "function") {
@@ -453,7 +453,7 @@ class QueryBuilder {
453
453
  // * Relation Property Path to first join column key
454
454
  // * Relation Property Path + Column Path
455
455
  // * Column Database Name
456
- // * Column Propety Name
456
+ // * Column Property Name
457
457
  // * Column Property Path
458
458
  for (const relation of alias.metadata.relations) {
459
459
  if (relation.joinColumns.length > 0)