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","sourcesContent":["import {Subject} from \"../Subject\";\nimport {OrmUtils} from \"../../util/OrmUtils\";\nimport {ObjectLiteral} from \"../../common/ObjectLiteral\";\nimport {EntityMetadata} from \"../../metadata/EntityMetadata\";\nimport {RelationMetadata} from \"../../metadata/RelationMetadata\";\n\n/**\n * Builds operations needs to be executed for one-to-many relations of the given subjects.\n *\n * by example: post contains one-to-many relation with category in the property called \"categories\", e.g.\n * @OneToMany(type => Category, category => category.post) categories: Category[]\n * If user adds categories 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 OneToOneInverseSideOperationBuilder, so when you change this class\n * make sure to reflect changes there as well.\n */\nexport class OneToManySubjectBuilder {\n\n // ---------------------------------------------------------------------\n // Constructor\n // ---------------------------------------------------------------------\n\n constructor(protected subjects: Subject[]) {\n }\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.oneToManyRelations.forEach(relation => {\n\n // skip relations for which persistence is disabled\n if (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 \"categories\" inside it here.\n */\n protected buildForSubjectRelation(subject: Subject, relation: RelationMetadata) {\n\n // prepare objects (relation id maps) for the database entity\n // note: subject.databaseEntity contains relations with loaded relation ids only\n // by example: since subject is a post, we are expecting to get all post's categories saved in the database here,\n // particularly their relation ids, e.g. category ids stored in the database\n let relatedEntityDatabaseRelationIds: ObjectLiteral[] = [];\n if (subject.databaseEntity) { // related entities in the database can exist only if this entity (post) is saved\n relatedEntityDatabaseRelationIds = relation.getEntityValue(subject.databaseEntity);\n }\n\n // get related entities of persisted entity\n // by example: get categories from the passed to persist post entity\n let relatedEntities: ObjectLiteral[] = relation.getEntityValue(subject.entity!);\n if (relatedEntities === null) // we treat relations set to null as removed, so we don't skip it\n relatedEntities = [] as ObjectLiteral[];\n if (relatedEntities === undefined) // if relation is undefined then nothing to update\n return;\n\n // extract only relation ids from the related entities, since we only need them for comparision\n // by example: extract from categories only relation ids (category id, or let's say category title, depend on join column options)\n const relatedPersistedEntityRelationIds: ObjectLiteral[] = [];\n relatedEntities.forEach(relatedEntity => { // by example: relatedEntity is a category here\n let relationIdMap = 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(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 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 categories which does not have ids yet (because they are new)\n // it means they are always newly inserted and relation update operation always must be created for them\n // it does not make sense to perform difference operation for them for both add and remove actions\n if (!relationIdMap) {\n\n // we decided to remove this error because it brings complications when saving object with non-saved entities\n // if (!relatedEntitySubject)\n // throw new Error(`One-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)\n 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 ManyToOne 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 return;\n }\n\n // check if this binding really exist in the database\n // by example: find our category if its already bind in the database\n const relationIdInDatabaseSubjectRelation = relatedEntityDatabaseRelationIds.find(relatedDatabaseEntityRelationId => {\n return OrmUtils.deepCompare(relationIdMap, relatedDatabaseEntityRelationId);\n });\n\n // if relationIdMap DOES NOT exist in the subject's relation in the database 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 (!relationIdInDatabaseSubjectRelation) {\n\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 parentSubject: subject,\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 // if related entity has relation id then we add it to the list of relation ids\n // this list will be used later to compare with database relation ids to find a difference\n // what exist in this array and does not exist in the database are newly inserted relations\n // what does not exist in this array, but exist in the database are removed relations\n // removed relations are set to null from inverse side of relation\n relatedPersistedEntityRelationIds.push(relationIdMap);\n });\n\n // find what related entities were added and what were removed based on difference between what we save and what database has\n EntityMetadata\n .difference(relatedEntityDatabaseRelationIds, relatedPersistedEntityRelationIds)\n .forEach(removedRelatedEntityRelationId => { // by example: removedRelatedEntityRelationId is category that was bind in the database before, but now its unbind\n\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 const removedRelatedEntitySubject = new Subject({\n metadata: relation.inverseEntityMetadata,\n parentSubject: subject,\n canBeUpdated: true,\n identifier: removedRelatedEntityRelationId,\n changeMaps: [{\n relation: relation.inverseRelation!,\n value: null\n }]\n });\n this.subjects.push(removedRelatedEntitySubject);\n });\n }\n\n}"],"sourceRoot":"../.."}
|