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AU,CAAC,QAAQ,CAAA;YACvD,IAAI,QAAQ,CAAC,sBAAsB;gBAC/B,MAAM,IAAI,oBAAY,CAClB,8GAA8G,CACjH,CAAA;YAEL,EAAE,GAAG,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,EAAE,CAAC,CAAA;QACtD,CAAC;QAED,OAAO,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,IAAI,CACtC,IAAI,CAAC,aAAa,CAAC,gBAAgB,EACnC,EAAE,EACF,IAAI,CAAC,WAAW,CACnB,CAAA;IACL,CAAC;CACJ;AA/LD,oDA+LC","file":"RelationQueryBuilder.js","sourcesContent":["import { QueryBuilder } from \"./QueryBuilder\"\nimport { RelationUpdater } from \"./RelationUpdater\"\nimport { RelationRemover } from \"./RelationRemover\"\nimport { TypeORMError } from \"../error\"\nimport { ObjectUtils } from \"../util/ObjectUtils\"\nimport { ObjectLiteral } from \"../common/ObjectLiteral\"\n\n/**\n * Allows to work with entity relations and perform specific operations with those relations.\n *\n * todo: add transactions everywhere\n */\nexport class RelationQueryBuilder<\n Entity extends ObjectLiteral,\n> extends QueryBuilder {\n readonly \"@instanceof\" = Symbol.for(\"RelationQueryBuilder\")\n\n // -------------------------------------------------------------------------\n // Public Implemented Methods\n // -------------------------------------------------------------------------\n\n /**\n * Gets generated SQL query without parameters being replaced.\n */\n getQuery(): string {\n return \"\"\n }\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Sets entity (target) which relations will be updated.\n */\n of(entity: any | any[]): this {\n this.expressionMap.of = entity\n return this\n }\n\n /**\n * Sets entity relation's value.\n * Value can be entity, entity id or entity id map (if entity has composite ids).\n * Works only for many-to-one and one-to-one relations.\n * For many-to-many and one-to-many relations use #add and #remove methods instead.\n */\n async set(value: any): Promise {\n const relation = this.expressionMap.relationMetadata\n\n if (!this.expressionMap.of)\n // todo: move this check before relation query builder creation?\n throw new TypeORMError(\n `Entity whose relation needs to be set is not set. Use .of method to define whose relation you want to set.`,\n )\n\n if (relation.isManyToMany || relation.isOneToMany)\n throw new TypeORMError(\n `Set operation is only supported for many-to-one and one-to-one relations. ` +\n `However given \"${relation.propertyPath}\" has ${relation.relationType} relation. ` +\n `Use .add() method instead.`,\n )\n\n // if there are multiple join columns then user must send id map as \"value\" argument. check if he really did it\n if (\n relation.joinColumns &&\n relation.joinColumns.length > 1 &&\n (!ObjectUtils.isObject(value) ||\n Object.keys(value).length < relation.joinColumns.length)\n )\n throw new TypeORMError(\n `Value to be set into the relation must be a map of relation ids, for example: .set({ firstName: \"...\", lastName: \"...\" })`,\n )\n\n const updater = new RelationUpdater(this, this.expressionMap)\n return updater.update(value)\n }\n\n /**\n * Adds (binds) given value to entity relation.\n * Value can be entity, entity id or entity id map (if entity has composite ids).\n * Value also can be array of entities, array of entity ids or array of entity id maps (if entity has composite ids).\n * Works only for many-to-many and one-to-many relations.\n * For many-to-one and one-to-one use #set method instead.\n */\n async add(value: any | any[]): Promise {\n if (Array.isArray(value) && value.length === 0) return\n\n const relation = this.expressionMap.relationMetadata\n\n if (!this.expressionMap.of)\n // todo: move this check before relation query builder creation?\n throw new TypeORMError(\n `Entity whose relation needs to be set is not set. Use .of method to define whose relation you want to set.`,\n )\n\n if (relation.isManyToOne || relation.isOneToOne)\n throw new TypeORMError(\n `Add operation is only supported for many-to-many and one-to-many relations. ` +\n `However given \"${relation.propertyPath}\" has ${relation.relationType} relation. ` +\n `Use .set() method instead.`,\n )\n\n // if there are multiple join columns then user must send id map as \"value\" argument. check if he really did it\n if (\n relation.joinColumns &&\n relation.joinColumns.length > 1 &&\n (!ObjectUtils.isObject(value) ||\n Object.keys(value).length < relation.joinColumns.length)\n )\n throw new TypeORMError(\n `Value to be set into the relation must be a map of relation ids, for example: .set({ firstName: \"...\", lastName: \"...\" })`,\n )\n\n const updater = new RelationUpdater(this, this.expressionMap)\n return updater.update(value)\n }\n\n /**\n * Removes (unbinds) given value from entity relation.\n * Value can be entity, entity id or entity id map (if entity has composite ids).\n * Value also can be array of entities, array of entity ids or array of entity id maps (if entity has composite ids).\n * Works only for many-to-many and one-to-many relations.\n * For many-to-one and one-to-one use #set method instead.\n */\n async remove(value: any | any[]): Promise {\n if (Array.isArray(value) && value.length === 0) return\n\n const relation = this.expressionMap.relationMetadata\n\n if (!this.expressionMap.of)\n // todo: move this check before relation query builder creation?\n throw new TypeORMError(\n `Entity whose relation needs to be set is not set. Use .of method to define whose relation you want to set.`,\n )\n\n if (relation.isManyToOne || relation.isOneToOne)\n throw new TypeORMError(\n `Add operation is only supported for many-to-many and one-to-many relations. ` +\n `However given \"${relation.propertyPath}\" has ${relation.relationType} relation. ` +\n `Use .set(null) method instead.`,\n )\n\n const remover = new RelationRemover(this, this.expressionMap)\n return remover.remove(value)\n }\n\n /**\n * Adds (binds) and removes (unbinds) given values to/from entity relation.\n * Value can be entity, entity id or entity id map (if entity has composite ids).\n * Value also can be array of entities, array of entity ids or array of entity id maps (if entity has composite ids).\n * Works only for many-to-many and one-to-many relations.\n * For many-to-one and one-to-one use #set method instead.\n */\n async addAndRemove(\n added: any | any[],\n removed: any | any[],\n ): Promise {\n await this.remove(removed)\n await this.add(added)\n }\n\n /**\n * Gets entity's relation id.\n async getId(): Promise {\n\n }*/\n\n /**\n * Gets entity's relation ids.\n async getIds(): Promise {\n return [];\n }*/\n\n /**\n * Loads a single entity (relational) from the relation.\n * You can also provide id of relational entity to filter by.\n */\n async loadOne(): Promise {\n return this.loadMany().then((results) => results[0])\n }\n\n /**\n * Loads many entities (relational) from the relation.\n * You can also provide ids of relational entities to filter by.\n */\n async loadMany(): Promise {\n let of = this.expressionMap.of\n if (!ObjectUtils.isObject(of)) {\n const metadata = this.expressionMap.mainAlias!.metadata\n if (metadata.hasMultiplePrimaryKeys)\n throw new TypeORMError(\n `Cannot load entity because only one primary key was specified, however entity contains multiple primary keys`,\n )\n\n of = metadata.primaryColumns[0].createValueMap(of)\n }\n\n return this.connection.relationLoader.load(\n this.expressionMap.relationMetadata,\n of,\n this.queryRunner,\n )\n }\n}\n"],"sourceRoot":".."}