typeorm 0.3.13-dev.de1228d → 0.3.13-dev.f1330ad
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.
- package/browser/find-options/FindOptionsUtils.js +45 -15
- package/browser/find-options/FindOptionsUtils.js.map +1 -1
- package/browser/query-builder/JoinAttribute.d.ts +4 -0
- package/browser/query-builder/JoinAttribute.js +4 -0
- package/browser/query-builder/JoinAttribute.js.map +1 -1
- package/browser/query-builder/SelectQueryBuilder.d.ts +4 -3
- package/browser/query-builder/SelectQueryBuilder.js +10 -6
- package/browser/query-builder/SelectQueryBuilder.js.map +1 -1
- package/browser/util/StringUtils.js +3 -3
- package/browser/util/StringUtils.js.map +1 -1
- package/find-options/FindOptionsUtils.js +46 -16
- package/find-options/FindOptionsUtils.js.map +1 -1
- package/package.json +1 -1
- package/query-builder/JoinAttribute.d.ts +4 -0
- package/query-builder/JoinAttribute.js +4 -0
- package/query-builder/JoinAttribute.js.map +1 -1
- package/query-builder/SelectQueryBuilder.d.ts +4 -3
- package/query-builder/SelectQueryBuilder.js +10 -6
- package/query-builder/SelectQueryBuilder.js.map +1 -1
- package/util/StringUtils.js +3 -3
- package/util/StringUtils.js.map +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { FindRelationsNotFoundError } from "../error
|
|
1
|
+
import { FindRelationsNotFoundError } from "../error";
|
|
2
2
|
import { DriverUtils } from "../driver/DriverUtils";
|
|
3
|
+
import { EntityPropertyNotFoundError } from "../error";
|
|
3
4
|
/**
|
|
4
5
|
* Utilities to work with FindOptions.
|
|
5
6
|
*/
|
|
@@ -244,38 +245,66 @@ export class FindOptionsUtils {
|
|
|
244
245
|
const regexp = new RegExp("^" + prefix.replace(".", "\\.") + "\\.");
|
|
245
246
|
matchedBaseRelations = allRelations
|
|
246
247
|
.filter((relation) => relation.match(regexp))
|
|
247
|
-
.map((relation) => relation.replace(regexp, ""))
|
|
248
|
-
.filter((
|
|
248
|
+
.map((relation) => metadata.findRelationWithPropertyPath(relation.replace(regexp, "")))
|
|
249
|
+
.filter((entity) => entity);
|
|
249
250
|
}
|
|
250
251
|
else {
|
|
251
|
-
matchedBaseRelations = allRelations
|
|
252
|
+
matchedBaseRelations = allRelations
|
|
253
|
+
.map((relation) => metadata.findRelationWithPropertyPath(relation))
|
|
254
|
+
.filter((entity) => entity);
|
|
252
255
|
}
|
|
253
256
|
// go through all matched relations and add join for them
|
|
254
257
|
matchedBaseRelations.forEach((relation) => {
|
|
255
258
|
// generate a relation alias
|
|
256
|
-
let relationAlias = DriverUtils.buildAlias(qb.connection.driver, { joiner: "__" }, alias, relation);
|
|
259
|
+
let relationAlias = DriverUtils.buildAlias(qb.connection.driver, { joiner: "__" }, alias, relation.propertyPath);
|
|
257
260
|
// add a join for the found relation
|
|
258
|
-
const selection = alias + "." + relation;
|
|
259
|
-
qb.
|
|
261
|
+
const selection = alias + "." + relation.propertyPath;
|
|
262
|
+
if (qb.expressionMap.relationLoadStrategy === "query") {
|
|
263
|
+
qb.concatRelationMetadata(relation);
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
qb.leftJoinAndSelect(selection, relationAlias);
|
|
267
|
+
}
|
|
260
268
|
// remove added relations from the allRelations array, this is needed to find all not found relations at the end
|
|
261
|
-
allRelations.splice(allRelations.indexOf(prefix
|
|
269
|
+
allRelations.splice(allRelations.indexOf(prefix
|
|
270
|
+
? prefix + "." + relation.propertyPath
|
|
271
|
+
: relation.propertyPath), 1);
|
|
262
272
|
// try to find sub-relations
|
|
263
|
-
|
|
264
|
-
|
|
273
|
+
let relationMetadata;
|
|
274
|
+
let relationName;
|
|
275
|
+
if (qb.expressionMap.relationLoadStrategy === "query") {
|
|
276
|
+
relationMetadata = relation.inverseEntityMetadata;
|
|
277
|
+
relationName = relationAlias;
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
const join = qb.expressionMap.joinAttributes.find((join) => join.entityOrProperty === selection);
|
|
281
|
+
relationMetadata = join.metadata;
|
|
282
|
+
relationName = join.alias.name;
|
|
283
|
+
}
|
|
284
|
+
if (!relationName || !relationMetadata) {
|
|
285
|
+
throw new EntityPropertyNotFoundError(relation.propertyPath, metadata);
|
|
286
|
+
}
|
|
287
|
+
this.applyRelationsRecursively(qb, allRelations, relationName, relationMetadata, prefix
|
|
288
|
+
? prefix + "." + relation.propertyPath
|
|
289
|
+
: relation.propertyPath);
|
|
265
290
|
// join the eager relations of the found relation
|
|
266
|
-
|
|
267
|
-
if (
|
|
268
|
-
|
|
291
|
+
// Only supported for "join" relationLoadStrategy
|
|
292
|
+
if (qb.expressionMap.relationLoadStrategy === "join") {
|
|
293
|
+
const relMetadata = metadata.relations.find((metadata) => metadata.propertyName === relation.propertyPath);
|
|
294
|
+
if (relMetadata) {
|
|
295
|
+
this.joinEagerRelations(qb, relationAlias, relMetadata.inverseEntityMetadata);
|
|
296
|
+
}
|
|
269
297
|
}
|
|
270
298
|
});
|
|
271
299
|
}
|
|
272
300
|
static joinEagerRelations(qb, alias, metadata) {
|
|
273
301
|
metadata.eagerRelations.forEach((relation) => {
|
|
274
302
|
// generate a relation alias
|
|
275
|
-
let relationAlias = DriverUtils.buildAlias(qb.connection.driver,
|
|
303
|
+
let relationAlias = DriverUtils.buildAlias(qb.connection.driver, { joiner: "__" }, alias, relation.propertyPath);
|
|
276
304
|
// add a join for the relation
|
|
277
305
|
// Checking whether the relation wasn't joined yet.
|
|
278
306
|
let addJoin = true;
|
|
307
|
+
// TODO: Review this validation
|
|
279
308
|
for (const join of qb.expressionMap.joinAttributes) {
|
|
280
309
|
if (join.condition !== undefined ||
|
|
281
310
|
join.mapToProperty !== undefined ||
|
|
@@ -289,7 +318,8 @@ export class FindOptionsUtils {
|
|
|
289
318
|
relationAlias = join.alias.name;
|
|
290
319
|
break;
|
|
291
320
|
}
|
|
292
|
-
|
|
321
|
+
const joinAlreadyAdded = Boolean(qb.expressionMap.joinAttributes.find((joinAttribute) => joinAttribute.alias.name === relationAlias));
|
|
322
|
+
if (addJoin && !joinAlreadyAdded) {
|
|
293
323
|
qb.leftJoin(alias + "." + relation.propertyPath, relationAlias);
|
|
294
324
|
}
|
|
295
325
|
// Checking whether the relation wasn't selected yet.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../browser/src/find-options/FindOptionsUtils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,0BAA0B,EAAE,MAAM,qCAAqC,CAAA;AAEhF,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAInD;;GAEG;AACH,MAAM,OAAO,gBAAgB;IACzB,4EAA4E;IAC5E,wBAAwB;IACxB,4EAA4E;IAE5E;;OAEG;IACH,MAAM,CAAC,gBAAgB,CACnB,GAAQ;QAER,MAAM,eAAe,GAA2B,GAAG,CAAA;QACnD,OAAO,CACH,eAAe;YACf,CAAC,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC;gBAClC,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC,SAAS,CAAC;gBACxC,OAAO,eAAe,CAAC,MAAM,KAAK,QAAQ;gBAC1C,OAAO,eAAe,CAAC,SAAS,KAAK,QAAQ;gBAC7C,OAAO,eAAe,CAAC,KAAK,KAAK,QAAQ;gBACzC,+CAA+C;gBAC/C,OAAO,eAAe,CAAC,IAAI,KAAK,QAAQ;gBACxC,OAAO,eAAe,CAAC,KAAK,KAAK,QAAQ;gBACzC,OAAO,eAAe,CAAC,KAAK,KAAK,QAAQ;gBACzC,OAAO,eAAe,CAAC,KAAK,KAAK,SAAS;gBAC1C,OAAO,eAAe,CAAC,KAAK,KAAK,QAAQ;gBACzC,OAAO,eAAe,CAAC,OAAO,KAAK,QAAQ;gBAC3C,OAAO,eAAe,CAAC,IAAI,KAAK,QAAQ;gBACxC,OAAO,eAAe,CAAC,eAAe,KAAK,QAAQ;gBACnD,OAAO,eAAe,CAAC,eAAe,KAAK,SAAS;gBACpD,OAAO,eAAe,CAAC,kBAAkB,KAAK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9B;YAED,uCAAuC;YACvC,IAAI,CAAC,kBAAkB,CACnB,EAAE,EACF,aAAa,EACb,QAAQ,CAAC,qBAAqB,CACjC,CAAA;QACL,CAAC,CAAC,CAAA;IACN,CAAC;CACJ","file":"FindOptionsUtils.js","sourcesContent":["import { FindManyOptions } from \"./FindManyOptions\"\nimport { FindOneOptions } from \"./FindOneOptions\"\nimport { SelectQueryBuilder } from \"../query-builder/SelectQueryBuilder\"\nimport { FindRelationsNotFoundError } from \"../error/FindRelationsNotFoundError\"\nimport { EntityMetadata } from \"../metadata/EntityMetadata\"\nimport { DriverUtils } from \"../driver/DriverUtils\"\nimport { FindTreeOptions } from \"./FindTreeOptions\"\nimport { ObjectLiteral } from \"../common/ObjectLiteral\"\n\n/**\n * Utilities to work with FindOptions.\n */\nexport class FindOptionsUtils {\n // -------------------------------------------------------------------------\n // Public Static Methods\n // -------------------------------------------------------------------------\n\n /**\n * Checks if given object is really instance of FindOneOptions interface.\n */\n static isFindOneOptions<Entity = any>(\n obj: any,\n ): obj is FindOneOptions<Entity> {\n const possibleOptions: FindOneOptions<Entity> = obj\n return (\n possibleOptions &&\n (Array.isArray(possibleOptions.select) ||\n Array.isArray(possibleOptions.relations) ||\n typeof possibleOptions.select === \"object\" ||\n typeof possibleOptions.relations === \"object\" ||\n typeof possibleOptions.where === \"object\" ||\n // typeof possibleOptions.where === \"string\" ||\n typeof possibleOptions.join === \"object\" ||\n typeof possibleOptions.order === \"object\" ||\n typeof possibleOptions.cache === \"object\" ||\n typeof possibleOptions.cache === \"boolean\" ||\n typeof possibleOptions.cache === \"number\" ||\n typeof possibleOptions.comment === \"string\" ||\n typeof possibleOptions.lock === \"object\" ||\n typeof possibleOptions.loadRelationIds === \"object\" ||\n typeof possibleOptions.loadRelationIds === \"boolean\" ||\n typeof possibleOptions.loadEagerRelations === \"boolean\" ||\n typeof possibleOptions.withDeleted === \"boolean\" ||\n typeof possibleOptions.relationLoadStrategy === \"string\" ||\n typeof possibleOptions.transaction === \"boolean\")\n )\n }\n\n /**\n * Checks if given object is really instance of FindManyOptions interface.\n */\n static isFindManyOptions<Entity = any>(\n obj: any,\n ): obj is FindManyOptions<Entity> {\n const possibleOptions: FindManyOptions<Entity> = obj\n return (\n possibleOptions &&\n (this.isFindOneOptions(possibleOptions) ||\n typeof (possibleOptions as FindManyOptions<any>).skip ===\n \"number\" ||\n typeof (possibleOptions as FindManyOptions<any>).take ===\n \"number\" ||\n typeof (possibleOptions as FindManyOptions<any>).skip ===\n \"string\" ||\n typeof (possibleOptions as FindManyOptions<any>).take ===\n \"string\")\n )\n }\n\n /**\n * Checks if given object is really instance of FindOptions interface.\n */\n static extractFindManyOptionsAlias(object: any): string | undefined {\n if (this.isFindManyOptions(object) && object.join)\n return object.join.alias\n\n return undefined\n }\n\n /**\n * Applies give find many options to the given query builder.\n\n static applyFindManyOptionsOrConditionsToQueryBuilder<T>(qb: SelectQueryBuilder<T>, options: FindManyOptions<T>|Partial<T>|undefined): SelectQueryBuilder<T> {\n if (this.isFindManyOptions(options))\n return this.applyOptionsToQueryBuilder(qb, options);\n\n if (options)\n return qb.where(options);\n\n return qb;\n }*/\n\n /**\n * Applies give find options to the given query builder.\n\n static applyOptionsToQueryBuilder<T>(qb: SelectQueryBuilder<T>, options: FindOneOptions<T>|FindManyOptions<T>|undefined): SelectQueryBuilder<T> {\n\n // if options are not set then simply return query builder. This is made for simplicity of usage.\n if (!options || (!this.isFindOneOptions(options) && !this.isFindManyOptions(options)))\n return qb;\n\n if (options.transaction === true) {\n qb.expressionMap.useTransaction = true;\n }\n\n if (!qb.expressionMap.mainAlias || !qb.expressionMap.mainAlias.hasMetadata)\n return qb;\n\n const metadata = qb.expressionMap.mainAlias!.metadata;\n\n // apply all options from FindOptions\n if (options.comment) {\n qb.comment(options.comment);\n }\n\n if (options.withDeleted) {\n qb.withDeleted();\n }\n\n if (options.select) {\n qb.select([]);\n options.select.forEach(select => {\n if (!metadata.hasColumnWithPropertyPath(`${select}`))\n throw new TypeORMError(`${select} column was not found in the ${metadata.name} entity.`);\n\n const columns = metadata.findColumnsWithPropertyPath(`${select}`);\n\n for (const column of columns) {\n qb.addSelect(qb.alias + \".\" + column.propertyPath);\n }\n });\n }\n\n if (options.relations) {\n // Copy because `applyRelationsRecursively` modifies it\n const allRelations = [...options.relations];\n this.applyRelationsRecursively(qb, allRelations, qb.expressionMap.mainAlias!.name, qb.expressionMap.mainAlias!.metadata, \"\");\n // recursive removes found relations from allRelations array\n // if there are relations left in this array it means those relations were not found in the entity structure\n // so, we give an exception about not found relations\n if (allRelations.length > 0)\n throw new FindRelationsNotFoundError(allRelations);\n }\n\n if (options.join) {\n if (options.join.leftJoin)\n Object.keys(options.join.leftJoin).forEach(key => {\n qb.leftJoin(options.join!.leftJoin![key], key);\n });\n\n if (options.join.innerJoin)\n Object.keys(options.join.innerJoin).forEach(key => {\n qb.innerJoin(options.join!.innerJoin![key], key);\n });\n\n if (options.join.leftJoinAndSelect)\n Object.keys(options.join.leftJoinAndSelect).forEach(key => {\n qb.leftJoinAndSelect(options.join!.leftJoinAndSelect![key], key);\n });\n\n if (options.join.innerJoinAndSelect)\n Object.keys(options.join.innerJoinAndSelect).forEach(key => {\n qb.innerJoinAndSelect(options.join!.innerJoinAndSelect![key], key);\n });\n }\n\n if (options.cache) {\n if (options.cache instanceof Object) {\n const cache = options.cache as { id: any, milliseconds: number };\n qb.cache(cache.id, cache.milliseconds);\n } else {\n qb.cache(options.cache);\n }\n }\n\n if (options.lock) {\n if (options.lock.mode === \"optimistic\") {\n qb.setLock(options.lock.mode, options.lock.version);\n } else if (\n options.lock.mode === \"pessimistic_read\" ||\n options.lock.mode === \"pessimistic_write\" ||\n options.lock.mode === \"dirty_read\" ||\n options.lock.mode === \"pessimistic_partial_write\" ||\n options.lock.mode === \"pessimistic_write_or_fail\" ||\n options.lock.mode === \"for_no_key_update\" ||\n options.lock.mode === \"for_key_share\"\n ) {\n const tableNames = options.lock.tables ? options.lock.tables.map((table) => {\n const tableAlias = qb.expressionMap.aliases.find((alias) => {\n return alias.metadata.tableNameWithoutPrefix === table;\n });\n if (!tableAlias) {\n throw new TypeORMError(`\"${table}\" is not part of this query`);\n }\n return qb.escape(tableAlias.name);\n }) : undefined;\n qb.setLock(options.lock.mode, undefined, tableNames);\n }\n }\n\n if (options.loadRelationIds === true) {\n qb.loadAllRelationIds();\n\n } else if (options.loadRelationIds instanceof Object) {\n qb.loadAllRelationIds(options.loadRelationIds as any);\n }\n\n if (options.where)\n qb.where(options.where);\n\n if ((options as FindManyOptions<T>).skip)\n qb.skip((options as FindManyOptions<T>).skip!);\n\n if ((options as FindManyOptions<T>).take)\n qb.take((options as FindManyOptions<T>).take!);\n\n if (options.order)\n Object.keys(options.order).forEach(key => {\n const order = ((options as FindOneOptions<T>).order as any)[key as any];\n\n if (!metadata.findColumnWithPropertyPath(key))\n throw new Error(`${key} column was not found in the ${metadata.name} entity.`);\n\n switch (order) {\n case 1:\n qb.addOrderBy(qb.alias + \".\" + key, \"ASC\");\n break;\n case -1:\n qb.addOrderBy(qb.alias + \".\" + key, \"DESC\");\n break;\n case \"ASC\":\n qb.addOrderBy(qb.alias + \".\" + key, \"ASC\");\n break;\n case \"DESC\":\n qb.addOrderBy(qb.alias + \".\" + key, \"DESC\");\n break;\n }\n });\n\n return qb;\n }*/\n\n static applyOptionsToTreeQueryBuilder<T extends ObjectLiteral>(\n qb: SelectQueryBuilder<T>,\n options?: FindTreeOptions,\n ): SelectQueryBuilder<T> {\n if (options?.relations) {\n // Copy because `applyRelationsRecursively` modifies it\n const allRelations = [...options.relations]\n\n FindOptionsUtils.applyRelationsRecursively(\n qb,\n allRelations,\n qb.expressionMap.mainAlias!.name,\n qb.expressionMap.mainAlias!.metadata,\n \"\",\n )\n\n // recursive removes found relations from allRelations array\n // if there are relations left in this array it means those relations were not found in the entity structure\n // so, we give an exception about not found relations\n if (allRelations.length > 0)\n throw new FindRelationsNotFoundError(allRelations)\n }\n\n return qb\n }\n\n // -------------------------------------------------------------------------\n // Protected Static Methods\n // -------------------------------------------------------------------------\n\n /**\n * Adds joins for all relations and sub-relations of the given relations provided in the find options.\n */\n public static applyRelationsRecursively(\n qb: SelectQueryBuilder<any>,\n allRelations: string[],\n alias: string,\n metadata: EntityMetadata,\n prefix: string,\n ): void {\n // find all relations that match given prefix\n let matchedBaseRelations: string[] = []\n if (prefix) {\n const regexp = new RegExp(\"^\" + prefix.replace(\".\", \"\\\\.\") + \"\\\\.\")\n matchedBaseRelations = allRelations\n .filter((relation) => relation.match(regexp))\n .map((relation) => relation.replace(regexp, \"\"))\n .filter((relation) =>\n metadata.findRelationWithPropertyPath(relation),\n )\n } else {\n matchedBaseRelations = allRelations.filter((relation) =>\n metadata.findRelationWithPropertyPath(relation),\n )\n }\n\n // go through all matched relations and add join for them\n matchedBaseRelations.forEach((relation) => {\n // generate a relation alias\n let relationAlias: string = DriverUtils.buildAlias(\n qb.connection.driver,\n { joiner: \"__\" },\n alias,\n relation,\n )\n\n // add a join for the found relation\n const selection = alias + \".\" + relation\n qb.leftJoinAndSelect(selection, relationAlias)\n\n // remove added relations from the allRelations array, this is needed to find all not found relations at the end\n allRelations.splice(\n allRelations.indexOf(\n prefix ? prefix + \".\" + relation : relation,\n ),\n 1,\n )\n\n // try to find sub-relations\n const join = qb.expressionMap.joinAttributes.find(\n (join) => join.entityOrProperty === selection,\n )\n this.applyRelationsRecursively(\n qb,\n allRelations,\n join!.alias.name,\n join!.metadata!,\n prefix ? prefix + \".\" + relation : relation,\n )\n\n // join the eager relations of the found relation\n const relMetadata = metadata.relations.find(\n (metadata) => metadata.propertyName === relation,\n )\n if (relMetadata) {\n this.joinEagerRelations(\n qb,\n relationAlias,\n relMetadata.inverseEntityMetadata,\n )\n }\n })\n }\n\n public static joinEagerRelations(\n qb: SelectQueryBuilder<any>,\n alias: string,\n metadata: EntityMetadata,\n ) {\n metadata.eagerRelations.forEach((relation) => {\n // generate a relation alias\n let relationAlias = DriverUtils.buildAlias(\n qb.connection.driver,\n qb.connection.namingStrategy.eagerJoinRelationAlias(\n alias,\n relation.propertyPath,\n ),\n )\n\n // add a join for the relation\n // Checking whether the relation wasn't joined yet.\n let addJoin = true\n for (const join of qb.expressionMap.joinAttributes) {\n if (\n join.condition !== undefined ||\n join.mapToProperty !== undefined ||\n join.isMappingMany !== undefined ||\n join.direction !== \"LEFT\" ||\n join.entityOrProperty !==\n `${alias}.${relation.propertyPath}`\n ) {\n continue\n }\n addJoin = false\n relationAlias = join.alias.name\n break\n }\n\n if (addJoin) {\n qb.leftJoin(alias + \".\" + relation.propertyPath, relationAlias)\n }\n\n // Checking whether the relation wasn't selected yet.\n // This check shall be after the join check to detect relationAlias.\n let addSelect = true\n for (const select of qb.expressionMap.selects) {\n if (\n select.aliasName !== undefined ||\n select.virtual !== undefined ||\n select.selection !== relationAlias\n ) {\n continue\n }\n addSelect = false\n break\n }\n\n if (addSelect) {\n qb.addSelect(relationAlias)\n }\n\n // (recursive) join the eager relations\n this.joinEagerRelations(\n qb,\n relationAlias,\n relation.inverseEntityMetadata,\n )\n })\n }\n}\n"],"sourceRoot":".."}
|
|
1
|
+
{"version":3,"sources":["../browser/src/find-options/FindOptionsUtils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAA;AAErD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAInD,OAAO,EAAE,2BAA2B,EAAE,MAAM,UAAU,CAAA;AAEtD;;GAEG;AACH,MAAM,OAAO,gBAAgB;IACzB,4EAA4E;IAC5E,wBAAwB;IACxB,4EAA4E;IAE5E;;OAEG;IACH,MAAM,CAAC,gBAAgB,CACnB,GAAQ;QAER,MAAM,eAAe,GAA2B,GAAG,CAAA;QACnD,OAAO,CACH,eAAe;YACf,CAAC,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC;gBAClC,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC,SAAS,CAAC;gBACxC,OAAO,eAAe,CAAC,MAAM,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,EAAE,CAAC,aAAa,CAAC,OAAO,EAAE;gBAC3C,IACI,MAAM,CAAC,SAAS,KAAK,SAAS;oBAC9B,MAAM,CAAC,OAAO,KAAK,SAAS;oBAC5B,MAAM,CAAC,SAAS,KAAK,aAAa,EACpC;oBACE,SAAQ;iBACX;gBACD,SAAS,GAAG,KAAK,CAAA;gBACjB,MAAK;aACR;YAED,IAAI,SAAS,EAAE;gBACX,EAAE,CAAC,SAAS,CAAC,aAAa,CAAC,CAAA;aAC9B;YAED,uCAAuC;YACvC,IAAI,CAAC,kBAAkB,CACnB,EAAE,EACF,aAAa,EACb,QAAQ,CAAC,qBAAqB,CACjC,CAAA;QACL,CAAC,CAAC,CAAA;IACN,CAAC;CACJ","file":"FindOptionsUtils.js","sourcesContent":["import { FindManyOptions } from \"./FindManyOptions\"\nimport { FindOneOptions } from \"./FindOneOptions\"\nimport { SelectQueryBuilder } from \"../query-builder/SelectQueryBuilder\"\nimport { FindRelationsNotFoundError } from \"../error\"\nimport { EntityMetadata } from \"../metadata/EntityMetadata\"\nimport { DriverUtils } from \"../driver/DriverUtils\"\nimport { FindTreeOptions } from \"./FindTreeOptions\"\nimport { ObjectLiteral } from \"../common/ObjectLiteral\"\nimport { RelationMetadata } from \"../metadata/RelationMetadata\"\nimport { EntityPropertyNotFoundError } from \"../error\"\n\n/**\n * Utilities to work with FindOptions.\n */\nexport class FindOptionsUtils {\n // -------------------------------------------------------------------------\n // Public Static Methods\n // -------------------------------------------------------------------------\n\n /**\n * Checks if given object is really instance of FindOneOptions interface.\n */\n static isFindOneOptions<Entity = any>(\n obj: any,\n ): obj is FindOneOptions<Entity> {\n const possibleOptions: FindOneOptions<Entity> = obj\n return (\n possibleOptions &&\n (Array.isArray(possibleOptions.select) ||\n Array.isArray(possibleOptions.relations) ||\n typeof possibleOptions.select === \"object\" ||\n typeof possibleOptions.relations === \"object\" ||\n typeof possibleOptions.where === \"object\" ||\n // typeof possibleOptions.where === \"string\" ||\n typeof possibleOptions.join === \"object\" ||\n typeof possibleOptions.order === \"object\" ||\n typeof possibleOptions.cache === \"object\" ||\n typeof possibleOptions.cache === \"boolean\" ||\n typeof possibleOptions.cache === \"number\" ||\n typeof possibleOptions.comment === \"string\" ||\n typeof possibleOptions.lock === \"object\" ||\n typeof possibleOptions.loadRelationIds === \"object\" ||\n typeof possibleOptions.loadRelationIds === \"boolean\" ||\n typeof possibleOptions.loadEagerRelations === \"boolean\" ||\n typeof possibleOptions.withDeleted === \"boolean\" ||\n typeof possibleOptions.relationLoadStrategy === \"string\" ||\n typeof possibleOptions.transaction === \"boolean\")\n )\n }\n\n /**\n * Checks if given object is really instance of FindManyOptions interface.\n */\n static isFindManyOptions<Entity = any>(\n obj: any,\n ): obj is FindManyOptions<Entity> {\n const possibleOptions: FindManyOptions<Entity> = obj\n return (\n possibleOptions &&\n (this.isFindOneOptions(possibleOptions) ||\n typeof (possibleOptions as FindManyOptions<any>).skip ===\n \"number\" ||\n typeof (possibleOptions as FindManyOptions<any>).take ===\n \"number\" ||\n typeof (possibleOptions as FindManyOptions<any>).skip ===\n \"string\" ||\n typeof (possibleOptions as FindManyOptions<any>).take ===\n \"string\")\n )\n }\n\n /**\n * Checks if given object is really instance of FindOptions interface.\n */\n static extractFindManyOptionsAlias(object: any): string | undefined {\n if (this.isFindManyOptions(object) && object.join)\n return object.join.alias\n\n return undefined\n }\n\n /**\n * Applies give find many options to the given query builder.\n\n static applyFindManyOptionsOrConditionsToQueryBuilder<T>(qb: SelectQueryBuilder<T>, options: FindManyOptions<T>|Partial<T>|undefined): SelectQueryBuilder<T> {\n if (this.isFindManyOptions(options))\n return this.applyOptionsToQueryBuilder(qb, options);\n\n if (options)\n return qb.where(options);\n\n return qb;\n }*/\n\n /**\n * Applies give find options to the given query builder.\n\n static applyOptionsToQueryBuilder<T>(qb: SelectQueryBuilder<T>, options: FindOneOptions<T>|FindManyOptions<T>|undefined): SelectQueryBuilder<T> {\n\n // if options are not set then simply return query builder. This is made for simplicity of usage.\n if (!options || (!this.isFindOneOptions(options) && !this.isFindManyOptions(options)))\n return qb;\n\n if (options.transaction === true) {\n qb.expressionMap.useTransaction = true;\n }\n\n if (!qb.expressionMap.mainAlias || !qb.expressionMap.mainAlias.hasMetadata)\n return qb;\n\n const metadata = qb.expressionMap.mainAlias!.metadata;\n\n // apply all options from FindOptions\n if (options.comment) {\n qb.comment(options.comment);\n }\n\n if (options.withDeleted) {\n qb.withDeleted();\n }\n\n if (options.select) {\n qb.select([]);\n options.select.forEach(select => {\n if (!metadata.hasColumnWithPropertyPath(`${select}`))\n throw new TypeORMError(`${select} column was not found in the ${metadata.name} entity.`);\n\n const columns = metadata.findColumnsWithPropertyPath(`${select}`);\n\n for (const column of columns) {\n qb.addSelect(qb.alias + \".\" + column.propertyPath);\n }\n });\n }\n\n if (options.relations) {\n // Copy because `applyRelationsRecursively` modifies it\n const allRelations = [...options.relations];\n this.applyRelationsRecursively(qb, allRelations, qb.expressionMap.mainAlias!.name, qb.expressionMap.mainAlias!.metadata, \"\");\n // recursive removes found relations from allRelations array\n // if there are relations left in this array it means those relations were not found in the entity structure\n // so, we give an exception about not found relations\n if (allRelations.length > 0)\n throw new FindRelationsNotFoundError(allRelations);\n }\n\n if (options.join) {\n if (options.join.leftJoin)\n Object.keys(options.join.leftJoin).forEach(key => {\n qb.leftJoin(options.join!.leftJoin![key], key);\n });\n\n if (options.join.innerJoin)\n Object.keys(options.join.innerJoin).forEach(key => {\n qb.innerJoin(options.join!.innerJoin![key], key);\n });\n\n if (options.join.leftJoinAndSelect)\n Object.keys(options.join.leftJoinAndSelect).forEach(key => {\n qb.leftJoinAndSelect(options.join!.leftJoinAndSelect![key], key);\n });\n\n if (options.join.innerJoinAndSelect)\n Object.keys(options.join.innerJoinAndSelect).forEach(key => {\n qb.innerJoinAndSelect(options.join!.innerJoinAndSelect![key], key);\n });\n }\n\n if (options.cache) {\n if (options.cache instanceof Object) {\n const cache = options.cache as { id: any, milliseconds: number };\n qb.cache(cache.id, cache.milliseconds);\n } else {\n qb.cache(options.cache);\n }\n }\n\n if (options.lock) {\n if (options.lock.mode === \"optimistic\") {\n qb.setLock(options.lock.mode, options.lock.version);\n } else if (\n options.lock.mode === \"pessimistic_read\" ||\n options.lock.mode === \"pessimistic_write\" ||\n options.lock.mode === \"dirty_read\" ||\n options.lock.mode === \"pessimistic_partial_write\" ||\n options.lock.mode === \"pessimistic_write_or_fail\" ||\n options.lock.mode === \"for_no_key_update\" ||\n options.lock.mode === \"for_key_share\"\n ) {\n const tableNames = options.lock.tables ? options.lock.tables.map((table) => {\n const tableAlias = qb.expressionMap.aliases.find((alias) => {\n return alias.metadata.tableNameWithoutPrefix === table;\n });\n if (!tableAlias) {\n throw new TypeORMError(`\"${table}\" is not part of this query`);\n }\n return qb.escape(tableAlias.name);\n }) : undefined;\n qb.setLock(options.lock.mode, undefined, tableNames);\n }\n }\n\n if (options.loadRelationIds === true) {\n qb.loadAllRelationIds();\n\n } else if (options.loadRelationIds instanceof Object) {\n qb.loadAllRelationIds(options.loadRelationIds as any);\n }\n\n if (options.where)\n qb.where(options.where);\n\n if ((options as FindManyOptions<T>).skip)\n qb.skip((options as FindManyOptions<T>).skip!);\n\n if ((options as FindManyOptions<T>).take)\n qb.take((options as FindManyOptions<T>).take!);\n\n if (options.order)\n Object.keys(options.order).forEach(key => {\n const order = ((options as FindOneOptions<T>).order as any)[key as any];\n\n if (!metadata.findColumnWithPropertyPath(key))\n throw new Error(`${key} column was not found in the ${metadata.name} entity.`);\n\n switch (order) {\n case 1:\n qb.addOrderBy(qb.alias + \".\" + key, \"ASC\");\n break;\n case -1:\n qb.addOrderBy(qb.alias + \".\" + key, \"DESC\");\n break;\n case \"ASC\":\n qb.addOrderBy(qb.alias + \".\" + key, \"ASC\");\n break;\n case \"DESC\":\n qb.addOrderBy(qb.alias + \".\" + key, \"DESC\");\n break;\n }\n });\n\n return qb;\n }*/\n\n static applyOptionsToTreeQueryBuilder<T extends ObjectLiteral>(\n qb: SelectQueryBuilder<T>,\n options?: FindTreeOptions,\n ): SelectQueryBuilder<T> {\n if (options?.relations) {\n // Copy because `applyRelationsRecursively` modifies it\n const allRelations = [...options.relations]\n\n FindOptionsUtils.applyRelationsRecursively(\n qb,\n allRelations,\n qb.expressionMap.mainAlias!.name,\n qb.expressionMap.mainAlias!.metadata,\n \"\",\n )\n\n // recursive removes found relations from allRelations array\n // if there are relations left in this array it means those relations were not found in the entity structure\n // so, we give an exception about not found relations\n if (allRelations.length > 0)\n throw new FindRelationsNotFoundError(allRelations)\n }\n\n return qb\n }\n\n // -------------------------------------------------------------------------\n // Protected Static Methods\n // -------------------------------------------------------------------------\n\n /**\n * Adds joins for all relations and sub-relations of the given relations provided in the find options.\n */\n public static applyRelationsRecursively(\n qb: SelectQueryBuilder<any>,\n allRelations: string[],\n alias: string,\n metadata: EntityMetadata,\n prefix: string,\n ): void {\n // find all relations that match given prefix\n let matchedBaseRelations: RelationMetadata[] = []\n if (prefix) {\n const regexp = new RegExp(\"^\" + prefix.replace(\".\", \"\\\\.\") + \"\\\\.\")\n matchedBaseRelations = allRelations\n .filter((relation) => relation.match(regexp))\n .map((relation) =>\n metadata.findRelationWithPropertyPath(\n relation.replace(regexp, \"\"),\n ),\n )\n .filter((entity) => entity) as RelationMetadata[]\n } else {\n matchedBaseRelations = allRelations\n .map((relation) =>\n metadata.findRelationWithPropertyPath(relation),\n )\n .filter((entity) => entity) as RelationMetadata[]\n }\n\n // go through all matched relations and add join for them\n matchedBaseRelations.forEach((relation) => {\n // generate a relation alias\n let relationAlias: string = DriverUtils.buildAlias(\n qb.connection.driver,\n { joiner: \"__\" },\n alias,\n relation.propertyPath,\n )\n\n // add a join for the found relation\n const selection = alias + \".\" + relation.propertyPath\n if (qb.expressionMap.relationLoadStrategy === \"query\") {\n qb.concatRelationMetadata(relation)\n } else {\n qb.leftJoinAndSelect(selection, relationAlias)\n }\n\n // remove added relations from the allRelations array, this is needed to find all not found relations at the end\n allRelations.splice(\n allRelations.indexOf(\n prefix\n ? prefix + \".\" + relation.propertyPath\n : relation.propertyPath,\n ),\n 1,\n )\n\n // try to find sub-relations\n let relationMetadata: EntityMetadata | undefined\n let relationName: string | undefined\n\n if (qb.expressionMap.relationLoadStrategy === \"query\") {\n relationMetadata = relation.inverseEntityMetadata\n relationName = relationAlias\n } else {\n const join = qb.expressionMap.joinAttributes.find(\n (join) => join.entityOrProperty === selection,\n )\n relationMetadata = join!.metadata!\n relationName = join!.alias.name\n }\n\n if (!relationName || !relationMetadata) {\n throw new EntityPropertyNotFoundError(\n relation.propertyPath,\n metadata,\n )\n }\n\n this.applyRelationsRecursively(\n qb,\n allRelations,\n relationName,\n relationMetadata,\n prefix\n ? prefix + \".\" + relation.propertyPath\n : relation.propertyPath,\n )\n\n // join the eager relations of the found relation\n // Only supported for \"join\" relationLoadStrategy\n if (qb.expressionMap.relationLoadStrategy === \"join\") {\n const relMetadata = metadata.relations.find(\n (metadata) =>\n metadata.propertyName === relation.propertyPath,\n )\n if (relMetadata) {\n this.joinEagerRelations(\n qb,\n relationAlias,\n relMetadata.inverseEntityMetadata,\n )\n }\n }\n })\n }\n\n public static joinEagerRelations(\n qb: SelectQueryBuilder<any>,\n alias: string,\n metadata: EntityMetadata,\n ) {\n metadata.eagerRelations.forEach((relation) => {\n // generate a relation alias\n let relationAlias: string = DriverUtils.buildAlias(\n qb.connection.driver,\n { joiner: \"__\" },\n alias,\n relation.propertyPath,\n )\n\n // add a join for the relation\n // Checking whether the relation wasn't joined yet.\n let addJoin = true\n // TODO: Review this validation\n for (const join of qb.expressionMap.joinAttributes) {\n if (\n join.condition !== undefined ||\n join.mapToProperty !== undefined ||\n join.isMappingMany !== undefined ||\n join.direction !== \"LEFT\" ||\n join.entityOrProperty !==\n `${alias}.${relation.propertyPath}`\n ) {\n continue\n }\n addJoin = false\n relationAlias = join.alias.name\n break\n }\n\n const joinAlreadyAdded = Boolean(\n qb.expressionMap.joinAttributes.find(\n (joinAttribute) =>\n joinAttribute.alias.name === relationAlias,\n ),\n )\n\n if (addJoin && !joinAlreadyAdded) {\n qb.leftJoin(alias + \".\" + relation.propertyPath, relationAlias)\n }\n\n // Checking whether the relation wasn't selected yet.\n // This check shall be after the join check to detect relationAlias.\n let addSelect = true\n for (const select of qb.expressionMap.selects) {\n if (\n select.aliasName !== undefined ||\n select.virtual !== undefined ||\n select.selection !== relationAlias\n ) {\n continue\n }\n addSelect = false\n break\n }\n\n if (addSelect) {\n qb.addSelect(relationAlias)\n }\n\n // (recursive) join the eager relations\n this.joinEagerRelations(\n qb,\n relationAlias,\n relation.inverseEntityMetadata,\n )\n })\n }\n}\n"],"sourceRoot":".."}
|
|
@@ -33,6 +33,10 @@ export declare class JoinAttribute {
|
|
|
33
33
|
* Indicates if user maps one or many objects from the join.
|
|
34
34
|
*/
|
|
35
35
|
isMappingMany?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Useful when the joined expression is a custom query to support mapping.
|
|
38
|
+
*/
|
|
39
|
+
mapAsEntity?: Function | string;
|
|
36
40
|
constructor(connection: DataSource, queryExpressionMap: QueryExpressionMap, joinAttribute?: JoinAttribute);
|
|
37
41
|
get isMany(): boolean;
|
|
38
42
|
isSelectedCache: boolean;
|
|
@@ -119,6 +119,10 @@ export class JoinAttribute {
|
|
|
119
119
|
// entityOrProperty is Entity class
|
|
120
120
|
if (this.connection.hasMetadata(this.entityOrProperty))
|
|
121
121
|
return this.connection.getMetadata(this.entityOrProperty);
|
|
122
|
+
// Overriden mapping entity provided for leftJoinAndMapOne with custom query builder
|
|
123
|
+
if (this.mapAsEntity && this.connection.hasMetadata(this.mapAsEntity)) {
|
|
124
|
+
return this.connection.getMetadata(this.mapAsEntity);
|
|
125
|
+
}
|
|
122
126
|
return undefined;
|
|
123
127
|
/*if (typeof this.entityOrProperty === "string") { // entityOrProperty is a custom table
|
|
124
128
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../browser/src/query-builder/JoinAttribute.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,iBA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QAEL,IAAI,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE;YACxB,OAAO,WAAW,CAAC,UAAU,CACzB,IAAI,CAAC,UAAU,CAAC,MAAM,EACtB,IAAI,CAAC,WAAY,EACjB,IAAI,CAAC,KAAK,CAAC,IAAI,CAClB,CAAA;SACJ;aAAM;YACH,OAAO,WAAW,CAAC,UAAU,CACzB,IAAI,CAAC,UAAU,CAAC,MAAM,EACtB,IAAI,CAAC,KAAK,CAAC,IAAI,EACf,IAAI,CAAC,WAAY,CACpB,CAAA;SACJ;IACL,CAAC;IAED,IAAI,wBAAwB;QACxB,IAAI,CAAC,IAAI,CAAC,aAAa;YAAE,OAAO,SAAS,CAAA;QAEzC,OAAO,IAAI,CAAC,aAAc,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAC5C,CAAC;IAED,IAAI,yBAAyB;QACzB,IAAI,CAAC,IAAI,CAAC,aAAa;YAAE,OAAO,SAAS,CAAA;QAEzC,OAAO,IAAI,CAAC,aAAc,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAC5C,CAAC;CACJ","file":"JoinAttribute.js","sourcesContent":["import { EntityMetadata } from \"../metadata/EntityMetadata\"\nimport { DataSource } from \"../data-source/DataSource\"\nimport { RelationMetadata } from \"../metadata/RelationMetadata\"\nimport { QueryBuilderUtils } from \"./QueryBuilderUtils\"\nimport { QueryExpressionMap } from \"./QueryExpressionMap\"\nimport { Alias } from \"./Alias\"\nimport { ObjectUtils } from \"../util/ObjectUtils\"\nimport { TypeORMError } from \"../error\"\nimport { DriverUtils } from \"../driver/DriverUtils\"\n\n/**\n * Stores all join attributes which will be used to build a JOIN query.\n */\nexport class JoinAttribute {\n // -------------------------------------------------------------------------\n // Public Properties\n // -------------------------------------------------------------------------\n\n /**\n * Join direction.\n */\n direction: \"LEFT\" | \"INNER\"\n\n /**\n * Alias of the joined (destination) table.\n */\n alias: Alias\n\n /**\n * Joined table, entity target, or relation in \"post.category\" format.\n */\n entityOrProperty: Function | string\n\n /**\n * Extra condition applied to \"ON\" section of join.\n */\n condition?: string\n\n /**\n * Property + alias of the object where to joined data should be mapped.\n */\n mapToProperty?: string\n\n /**\n * Indicates if user maps one or many objects from the join.\n */\n isMappingMany?: boolean\n\n // -------------------------------------------------------------------------\n // Constructor\n // -------------------------------------------------------------------------\n\n constructor(\n private connection: DataSource,\n private queryExpressionMap: QueryExpressionMap,\n joinAttribute?: JoinAttribute,\n ) {\n ObjectUtils.assign(this, joinAttribute || {})\n }\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n get isMany(): boolean {\n if (this.isMappingMany !== undefined) return this.isMappingMany\n\n if (this.relation)\n return this.relation.isManyToMany || this.relation.isOneToMany\n\n return false\n }\n\n isSelectedCache: boolean\n isSelectedEvaluated: boolean = false\n /**\n * Indicates if this join is selected.\n */\n get isSelected(): boolean {\n if (!this.isSelectedEvaluated) {\n let getValue = () => {\n for (const select of this.queryExpressionMap.selects) {\n if (select.selection === this.alias.name) return true\n\n if (\n this.metadata &&\n !!this.metadata.columns.find(\n (column) =>\n select.selection ===\n this.alias.name + \".\" + column.propertyPath,\n )\n )\n return true\n }\n\n return false\n }\n this.isSelectedCache = getValue()\n this.isSelectedEvaluated = true\n }\n return this.isSelectedCache\n }\n\n /**\n * Name of the table which we should join.\n */\n get tablePath(): string {\n return this.metadata\n ? this.metadata.tablePath\n : (this.entityOrProperty as string)\n }\n\n /**\n * Alias of the parent of this join.\n * For example, if we join (\"post.category\", \"categoryAlias\") then \"post\" is a parent alias.\n * This value is extracted from entityOrProperty value.\n * This is available when join was made using \"post.category\" syntax.\n */\n get parentAlias(): string | undefined {\n if (!QueryBuilderUtils.isAliasProperty(this.entityOrProperty))\n return undefined\n\n return this.entityOrProperty.substr(\n 0,\n this.entityOrProperty.indexOf(\".\"),\n )\n }\n\n /**\n * Relation property name of the parent.\n * This is used to understand what is joined.\n * For example, if we join (\"post.category\", \"categoryAlias\") then \"category\" is a relation property.\n * This value is extracted from entityOrProperty value.\n * This is available when join was made using \"post.category\" syntax.\n */\n get relationPropertyPath(): string | undefined {\n if (!QueryBuilderUtils.isAliasProperty(this.entityOrProperty))\n return undefined\n\n return this.entityOrProperty.substr(\n this.entityOrProperty.indexOf(\".\") + 1,\n )\n }\n\n relationCache: RelationMetadata | undefined\n relationEvaluated: boolean = false\n /**\n * Relation of the parent.\n * This is used to understand what is joined.\n * This is available when join was made using \"post.category\" syntax.\n * Relation can be undefined if entityOrProperty is regular entity or custom table.\n */\n get relation(): RelationMetadata | undefined {\n if (!this.relationEvaluated) {\n let getValue = () => {\n if (!QueryBuilderUtils.isAliasProperty(this.entityOrProperty))\n return undefined\n\n const relationOwnerSelection =\n this.queryExpressionMap.findAliasByName(this.parentAlias!)\n let relation =\n relationOwnerSelection.metadata.findRelationWithPropertyPath(\n this.relationPropertyPath!,\n )\n\n if (relation) {\n return relation\n }\n\n if (relationOwnerSelection.metadata.parentEntityMetadata) {\n relation =\n relationOwnerSelection.metadata.parentEntityMetadata.findRelationWithPropertyPath(\n this.relationPropertyPath!,\n )\n if (relation) {\n return relation\n }\n }\n\n throw new TypeORMError(\n `Relation with property path ${this.relationPropertyPath} in entity was not found.`,\n )\n }\n this.relationCache = getValue.bind(this)()\n this.relationEvaluated = true\n }\n return this.relationCache\n }\n\n /**\n * Metadata of the joined entity.\n * If table without entity was joined, then it will return undefined.\n */\n get metadata(): EntityMetadata | undefined {\n // entityOrProperty is relation, e.g. \"post.category\"\n if (this.relation) return this.relation.inverseEntityMetadata\n\n // entityOrProperty is Entity class\n if (this.connection.hasMetadata(this.entityOrProperty))\n return this.connection.getMetadata(this.entityOrProperty)\n\n return undefined\n\n /*if (typeof this.entityOrProperty === \"string\") { // entityOrProperty is a custom table\n\n // first try to find entity with such name, this is needed when entity does not have a target class,\n // and its target is a string name (scenario when plain old javascript is used or entity schema is loaded from files)\n const metadata = this.connection.entityMetadatas.find(metadata => metadata.name === this.entityOrProperty);\n if (metadata)\n return metadata;\n\n // check if we have entity with such table name, and use its metadata if found\n return this.connection.entityMetadatas.find(metadata => metadata.tableName === this.entityOrProperty);\n }*/\n }\n\n /**\n * Generates alias of junction table, whose ids we get.\n */\n get junctionAlias(): string {\n if (!this.relation)\n throw new TypeORMError(\n `Cannot get junction table for join without relation.`,\n )\n\n if (this.relation.isOwning) {\n return DriverUtils.buildAlias(\n this.connection.driver,\n this.parentAlias!,\n this.alias.name,\n )\n } else {\n return DriverUtils.buildAlias(\n this.connection.driver,\n this.alias.name,\n this.parentAlias!,\n )\n }\n }\n\n get mapToPropertyParentAlias(): string | undefined {\n if (!this.mapToProperty) return undefined\n\n return this.mapToProperty!.split(\".\")[0]\n }\n\n get mapToPropertyPropertyName(): string | undefined {\n if (!this.mapToProperty) return undefined\n\n return this.mapToProperty!.split(\".\")[1]\n }\n}\n"],"sourceRoot":".."}
|
|
1
|
+
{"version":3,"sources":["../browser/src/query-builder/JoinAttribute.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAGvD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAEnD;;GAEG;AACH,MAAM,OAAO,aAAa;IAwCtB,4EAA4E;IAC5E,cAAc;IACd,4EAA4E;IAE5E,YACY,UAAsB,EACtB,kBAAsC,EAC9C,aAA6B;QAFrB,eAAU,GAAV,UAAU,CAAY;QACtB,uBAAkB,GAAlB,kBAAkB,CAAoB;QAoBlD,wBAAmB,GAAY,KAAK,CAAA;QAuEpC,sBAAiB,GAAY,KAAK,CAAA;QAxF9B,WAAW,CAAC,MAAM,CAAC,IAAI,EAAE,aAAa,IAAI,EAAE,CAAC,CAAA;IACjD,CAAC;IAED,4EAA4E;IAC5E,iBAAiB;IACjB,4EAA4E;IA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AI,CAAC,IAAI,CAAC,aAAa;YAAE,OAAO,SAAS,CAAA;QAEzC,OAAO,IAAI,CAAC,aAAc,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAC5C,CAAC;IAED,IAAI,yBAAyB;QACzB,IAAI,CAAC,IAAI,CAAC,aAAa;YAAE,OAAO,SAAS,CAAA;QAEzC,OAAO,IAAI,CAAC,aAAc,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAC5C,CAAC;CACJ","file":"JoinAttribute.js","sourcesContent":["import { EntityMetadata } from \"../metadata/EntityMetadata\"\nimport { DataSource } from \"../data-source/DataSource\"\nimport { RelationMetadata } from \"../metadata/RelationMetadata\"\nimport { QueryBuilderUtils } from \"./QueryBuilderUtils\"\nimport { QueryExpressionMap } from \"./QueryExpressionMap\"\nimport { Alias } from \"./Alias\"\nimport { ObjectUtils } from \"../util/ObjectUtils\"\nimport { TypeORMError } from \"../error\"\nimport { DriverUtils } from \"../driver/DriverUtils\"\n\n/**\n * Stores all join attributes which will be used to build a JOIN query.\n */\nexport class JoinAttribute {\n // -------------------------------------------------------------------------\n // Public Properties\n // -------------------------------------------------------------------------\n\n /**\n * Join direction.\n */\n direction: \"LEFT\" | \"INNER\"\n\n /**\n * Alias of the joined (destination) table.\n */\n alias: Alias\n\n /**\n * Joined table, entity target, or relation in \"post.category\" format.\n */\n entityOrProperty: Function | string\n\n /**\n * Extra condition applied to \"ON\" section of join.\n */\n condition?: string\n\n /**\n * Property + alias of the object where to joined data should be mapped.\n */\n mapToProperty?: string\n\n /**\n * Indicates if user maps one or many objects from the join.\n */\n isMappingMany?: boolean\n\n /**\n * Useful when the joined expression is a custom query to support mapping.\n */\n mapAsEntity?: Function | string\n\n // -------------------------------------------------------------------------\n // Constructor\n // -------------------------------------------------------------------------\n\n constructor(\n private connection: DataSource,\n private queryExpressionMap: QueryExpressionMap,\n joinAttribute?: JoinAttribute,\n ) {\n ObjectUtils.assign(this, joinAttribute || {})\n }\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n get isMany(): boolean {\n if (this.isMappingMany !== undefined) return this.isMappingMany\n\n if (this.relation)\n return this.relation.isManyToMany || this.relation.isOneToMany\n\n return false\n }\n\n isSelectedCache: boolean\n isSelectedEvaluated: boolean = false\n /**\n * Indicates if this join is selected.\n */\n get isSelected(): boolean {\n if (!this.isSelectedEvaluated) {\n let getValue = () => {\n for (const select of this.queryExpressionMap.selects) {\n if (select.selection === this.alias.name) return true\n\n if (\n this.metadata &&\n !!this.metadata.columns.find(\n (column) =>\n select.selection ===\n this.alias.name + \".\" + column.propertyPath,\n )\n )\n return true\n }\n\n return false\n }\n this.isSelectedCache = getValue()\n this.isSelectedEvaluated = true\n }\n return this.isSelectedCache\n }\n\n /**\n * Name of the table which we should join.\n */\n get tablePath(): string {\n return this.metadata\n ? this.metadata.tablePath\n : (this.entityOrProperty as string)\n }\n\n /**\n * Alias of the parent of this join.\n * For example, if we join (\"post.category\", \"categoryAlias\") then \"post\" is a parent alias.\n * This value is extracted from entityOrProperty value.\n * This is available when join was made using \"post.category\" syntax.\n */\n get parentAlias(): string | undefined {\n if (!QueryBuilderUtils.isAliasProperty(this.entityOrProperty))\n return undefined\n\n return this.entityOrProperty.substr(\n 0,\n this.entityOrProperty.indexOf(\".\"),\n )\n }\n\n /**\n * Relation property name of the parent.\n * This is used to understand what is joined.\n * For example, if we join (\"post.category\", \"categoryAlias\") then \"category\" is a relation property.\n * This value is extracted from entityOrProperty value.\n * This is available when join was made using \"post.category\" syntax.\n */\n get relationPropertyPath(): string | undefined {\n if (!QueryBuilderUtils.isAliasProperty(this.entityOrProperty))\n return undefined\n\n return this.entityOrProperty.substr(\n this.entityOrProperty.indexOf(\".\") + 1,\n )\n }\n\n relationCache: RelationMetadata | undefined\n relationEvaluated: boolean = false\n /**\n * Relation of the parent.\n * This is used to understand what is joined.\n * This is available when join was made using \"post.category\" syntax.\n * Relation can be undefined if entityOrProperty is regular entity or custom table.\n */\n get relation(): RelationMetadata | undefined {\n if (!this.relationEvaluated) {\n let getValue = () => {\n if (!QueryBuilderUtils.isAliasProperty(this.entityOrProperty))\n return undefined\n\n const relationOwnerSelection =\n this.queryExpressionMap.findAliasByName(this.parentAlias!)\n let relation =\n relationOwnerSelection.metadata.findRelationWithPropertyPath(\n this.relationPropertyPath!,\n )\n\n if (relation) {\n return relation\n }\n\n if (relationOwnerSelection.metadata.parentEntityMetadata) {\n relation =\n relationOwnerSelection.metadata.parentEntityMetadata.findRelationWithPropertyPath(\n this.relationPropertyPath!,\n )\n if (relation) {\n return relation\n }\n }\n\n throw new TypeORMError(\n `Relation with property path ${this.relationPropertyPath} in entity was not found.`,\n )\n }\n this.relationCache = getValue.bind(this)()\n this.relationEvaluated = true\n }\n return this.relationCache\n }\n\n /**\n * Metadata of the joined entity.\n * If table without entity was joined, then it will return undefined.\n */\n get metadata(): EntityMetadata | undefined {\n // entityOrProperty is relation, e.g. \"post.category\"\n if (this.relation) return this.relation.inverseEntityMetadata\n\n // entityOrProperty is Entity class\n if (this.connection.hasMetadata(this.entityOrProperty))\n return this.connection.getMetadata(this.entityOrProperty)\n\n // Overriden mapping entity provided for leftJoinAndMapOne with custom query builder\n if (this.mapAsEntity && this.connection.hasMetadata(this.mapAsEntity)) {\n return this.connection.getMetadata(this.mapAsEntity)\n }\n\n return undefined\n\n /*if (typeof this.entityOrProperty === \"string\") { // entityOrProperty is a custom table\n\n // first try to find entity with such name, this is needed when entity does not have a target class,\n // and its target is a string name (scenario when plain old javascript is used or entity schema is loaded from files)\n const metadata = this.connection.entityMetadatas.find(metadata => metadata.name === this.entityOrProperty);\n if (metadata)\n return metadata;\n\n // check if we have entity with such table name, and use its metadata if found\n return this.connection.entityMetadatas.find(metadata => metadata.tableName === this.entityOrProperty);\n }*/\n }\n\n /**\n * Generates alias of junction table, whose ids we get.\n */\n get junctionAlias(): string {\n if (!this.relation)\n throw new TypeORMError(\n `Cannot get junction table for join without relation.`,\n )\n\n if (this.relation.isOwning) {\n return DriverUtils.buildAlias(\n this.connection.driver,\n this.parentAlias!,\n this.alias.name,\n )\n } else {\n return DriverUtils.buildAlias(\n this.connection.driver,\n this.alias.name,\n this.parentAlias!,\n )\n }\n }\n\n get mapToPropertyParentAlias(): string | undefined {\n if (!this.mapToProperty) return undefined\n\n return this.mapToProperty!.split(\".\")[0]\n }\n\n get mapToPropertyPropertyName(): string | undefined {\n if (!this.mapToProperty) return undefined\n\n return this.mapToProperty!.split(\".\")[1]\n }\n}\n"],"sourceRoot":".."}
|
|
@@ -258,7 +258,7 @@ export declare class SelectQueryBuilder<Entity extends ObjectLiteral> extends Qu
|
|
|
258
258
|
* You also need to specify an alias of the joined data.
|
|
259
259
|
* Optionally, you can add condition and parameters used in condition.
|
|
260
260
|
*/
|
|
261
|
-
innerJoinAndMapOne(mapToProperty: string, subQueryFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>, alias: string, condition?: string, parameters?: ObjectLiteral): this;
|
|
261
|
+
innerJoinAndMapOne(mapToProperty: string, subQueryFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>, alias: string, condition?: string, parameters?: ObjectLiteral, mapAsEntity?: Function | string): this;
|
|
262
262
|
/**
|
|
263
263
|
* INNER JOINs entity's property, SELECTs the data returned by a join and MAPs all that data to some entity's property.
|
|
264
264
|
* This is extremely useful when you want to select some data and map it to some virtual property.
|
|
@@ -326,7 +326,7 @@ export declare class SelectQueryBuilder<Entity extends ObjectLiteral> extends Qu
|
|
|
326
326
|
* You also need to specify an alias of the joined data.
|
|
327
327
|
* Optionally, you can add condition and parameters used in condition.
|
|
328
328
|
*/
|
|
329
|
-
leftJoinAndMapOne(mapToProperty: string, subQueryFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>, alias: string, condition?: string, parameters?: ObjectLiteral): this;
|
|
329
|
+
leftJoinAndMapOne(mapToProperty: string, subQueryFactory: (qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>, alias: string, condition?: string, parameters?: ObjectLiteral, mapAsEntity?: Function | string): this;
|
|
330
330
|
/**
|
|
331
331
|
* LEFT JOINs entity's property, SELECTs the data returned by a join and MAPs all that data to some entity's property.
|
|
332
332
|
* This is extremely useful when you want to select some data and map it to some virtual property.
|
|
@@ -610,7 +610,7 @@ export declare class SelectQueryBuilder<Entity extends ObjectLiteral> extends Qu
|
|
|
610
610
|
* Sets extra options that can be used to configure how query builder works.
|
|
611
611
|
*/
|
|
612
612
|
setOption(option: SelectQueryBuilderOption): this;
|
|
613
|
-
protected join(direction: "INNER" | "LEFT", entityOrProperty: Function | string | ((qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>), aliasName: string, condition?: string, parameters?: ObjectLiteral, mapToProperty?: string, isMappingMany?: boolean): void;
|
|
613
|
+
protected join(direction: "INNER" | "LEFT", entityOrProperty: Function | string | ((qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>), aliasName: string, condition?: string, parameters?: ObjectLiteral, mapToProperty?: string, isMappingMany?: boolean, mapAsEntity?: Function | string): void;
|
|
614
614
|
/**
|
|
615
615
|
* Creates "SELECT FROM" part of SQL query.
|
|
616
616
|
*/
|
|
@@ -658,6 +658,7 @@ export declare class SelectQueryBuilder<Entity extends ObjectLiteral> extends Qu
|
|
|
658
658
|
protected executeCountQuery(queryRunner: QueryRunner): Promise<number>;
|
|
659
659
|
protected executeExistsQuery(queryRunner: QueryRunner): Promise<boolean>;
|
|
660
660
|
protected applyFindOptions(): void;
|
|
661
|
+
concatRelationMetadata(relationMetadata: RelationMetadata): void;
|
|
661
662
|
/**
|
|
662
663
|
* Executes sql generated by query builder and returns object with raw results and entities created from them.
|
|
663
664
|
*/
|
|
@@ -230,9 +230,9 @@ export class SelectQueryBuilder extends QueryBuilder {
|
|
|
230
230
|
* You also need to specify an alias of the joined data.
|
|
231
231
|
* Optionally, you can add condition and parameters used in condition.
|
|
232
232
|
*/
|
|
233
|
-
innerJoinAndMapOne(mapToProperty, entityOrProperty, alias, condition, parameters) {
|
|
233
|
+
innerJoinAndMapOne(mapToProperty, entityOrProperty, alias, condition, parameters, mapAsEntity) {
|
|
234
234
|
this.addSelect(alias);
|
|
235
|
-
this.join("INNER", entityOrProperty, alias, condition, parameters, mapToProperty, false);
|
|
235
|
+
this.join("INNER", entityOrProperty, alias, condition, parameters, mapToProperty, false, mapAsEntity);
|
|
236
236
|
return this;
|
|
237
237
|
}
|
|
238
238
|
/**
|
|
@@ -254,9 +254,9 @@ export class SelectQueryBuilder extends QueryBuilder {
|
|
|
254
254
|
* You also need to specify an alias of the joined data.
|
|
255
255
|
* Optionally, you can add condition and parameters used in condition.
|
|
256
256
|
*/
|
|
257
|
-
leftJoinAndMapOne(mapToProperty, entityOrProperty, alias, condition, parameters) {
|
|
257
|
+
leftJoinAndMapOne(mapToProperty, entityOrProperty, alias, condition, parameters, mapAsEntity) {
|
|
258
258
|
this.addSelect(alias);
|
|
259
|
-
this.join("LEFT", entityOrProperty, alias, condition, parameters, mapToProperty, false);
|
|
259
|
+
this.join("LEFT", entityOrProperty, alias, condition, parameters, mapToProperty, false, mapAsEntity);
|
|
260
260
|
return this;
|
|
261
261
|
}
|
|
262
262
|
/**
|
|
@@ -942,10 +942,11 @@ export class SelectQueryBuilder extends QueryBuilder {
|
|
|
942
942
|
// -------------------------------------------------------------------------
|
|
943
943
|
// Protected Methods
|
|
944
944
|
// -------------------------------------------------------------------------
|
|
945
|
-
join(direction, entityOrProperty, aliasName, condition, parameters, mapToProperty, isMappingMany) {
|
|
945
|
+
join(direction, entityOrProperty, aliasName, condition, parameters, mapToProperty, isMappingMany, mapAsEntity) {
|
|
946
946
|
this.setParameters(parameters || {});
|
|
947
947
|
const joinAttribute = new JoinAttribute(this.connection, this.expressionMap);
|
|
948
948
|
joinAttribute.direction = direction;
|
|
949
|
+
joinAttribute.mapAsEntity = mapAsEntity;
|
|
949
950
|
joinAttribute.mapToProperty = mapToProperty;
|
|
950
951
|
joinAttribute.isMappingMany = isMappingMany;
|
|
951
952
|
joinAttribute.entityOrProperty = entityOrProperty; // relationName
|
|
@@ -1899,6 +1900,9 @@ export class SelectQueryBuilder extends QueryBuilder {
|
|
|
1899
1900
|
// }
|
|
1900
1901
|
}
|
|
1901
1902
|
}
|
|
1903
|
+
concatRelationMetadata(relationMetadata) {
|
|
1904
|
+
this.relationMetadatas.push(relationMetadata);
|
|
1905
|
+
}
|
|
1902
1906
|
/**
|
|
1903
1907
|
* Executes sql generated by query builder and returns object with raw results and entities created from them.
|
|
1904
1908
|
*/
|
|
@@ -2251,7 +2255,7 @@ export class SelectQueryBuilder extends QueryBuilder {
|
|
|
2251
2255
|
if (relationValue === true ||
|
|
2252
2256
|
typeof relationValue === "object") {
|
|
2253
2257
|
if (this.expressionMap.relationLoadStrategy === "query") {
|
|
2254
|
-
this.
|
|
2258
|
+
this.concatRelationMetadata(relation);
|
|
2255
2259
|
}
|
|
2256
2260
|
else {
|
|
2257
2261
|
// join
|