typeorm 0.2.39-dev.4638dea → 0.2.39-dev.4aaafdf
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/driver/cockroachdb/CockroachQueryRunner.js +1 -1
- package/browser/driver/cockroachdb/CockroachQueryRunner.js.map +1 -1
- package/browser/find-options/FindOptionsUtils.d.ts +2 -0
- package/browser/find-options/FindOptionsUtils.js +19 -1
- package/browser/find-options/FindOptionsUtils.js.map +1 -1
- package/browser/persistence/subject-builder/OneToManySubjectBuilder.js +10 -4
- package/browser/persistence/subject-builder/OneToManySubjectBuilder.js.map +1 -1
- package/browser/query-builder/SelectQueryBuilder.d.ts +1 -1
- package/browser/query-builder/SelectQueryBuilder.js +20 -4
- package/browser/query-builder/SelectQueryBuilder.js.map +1 -1
- package/browser/repository/TreeRepository.d.ts +3 -3
- package/browser/repository/TreeRepository.js +28 -39
- package/browser/repository/TreeRepository.js.map +1 -1
- package/commands/InitCommand.js +2 -1
- package/commands/InitCommand.js.map +1 -1
- package/driver/cockroachdb/CockroachQueryRunner.js +1 -1
- package/driver/cockroachdb/CockroachQueryRunner.js.map +1 -1
- package/find-options/FindOptionsUtils.d.ts +2 -0
- package/find-options/FindOptionsUtils.js +19 -1
- package/find-options/FindOptionsUtils.js.map +1 -1
- package/package.json +1 -1
- package/persistence/subject-builder/OneToManySubjectBuilder.js +10 -4
- package/persistence/subject-builder/OneToManySubjectBuilder.js.map +1 -1
- package/query-builder/SelectQueryBuilder.d.ts +1 -1
- package/query-builder/SelectQueryBuilder.js +20 -4
- package/query-builder/SelectQueryBuilder.js.map +1 -1
- package/repository/TreeRepository.d.ts +3 -3
- package/repository/TreeRepository.js +27 -38
- package/repository/TreeRepository.js.map +1 -1
|
@@ -2,6 +2,7 @@ import { FindManyOptions } from "./FindManyOptions";
|
|
|
2
2
|
import { FindOneOptions } from "./FindOneOptions";
|
|
3
3
|
import { SelectQueryBuilder } from "../query-builder/SelectQueryBuilder";
|
|
4
4
|
import { EntityMetadata } from "../metadata/EntityMetadata";
|
|
5
|
+
import { FindTreeOptions } from "./FindTreeOptions";
|
|
5
6
|
/**
|
|
6
7
|
* Utilities to work with FindOptions.
|
|
7
8
|
*/
|
|
@@ -26,6 +27,7 @@ export declare class FindOptionsUtils {
|
|
|
26
27
|
* Applies give find options to the given query builder.
|
|
27
28
|
*/
|
|
28
29
|
static applyOptionsToQueryBuilder<T>(qb: SelectQueryBuilder<T>, options: FindOneOptions<T> | FindManyOptions<T> | undefined): SelectQueryBuilder<T>;
|
|
30
|
+
static applyOptionsToTreeQueryBuilder<T>(qb: SelectQueryBuilder<T>, options?: FindTreeOptions): SelectQueryBuilder<T>;
|
|
29
31
|
/**
|
|
30
32
|
* Adds joins for all relations and sub-relations of the given relations provided in the find options.
|
|
31
33
|
*/
|
|
@@ -145,7 +145,12 @@ var FindOptionsUtils = /** @class */ (function () {
|
|
|
145
145
|
if (options.lock.mode === "optimistic") {
|
|
146
146
|
qb.setLock(options.lock.mode, options.lock.version);
|
|
147
147
|
}
|
|
148
|
-
else if (options.lock.mode === "pessimistic_read" ||
|
|
148
|
+
else if (options.lock.mode === "pessimistic_read" ||
|
|
149
|
+
options.lock.mode === "pessimistic_write" ||
|
|
150
|
+
options.lock.mode === "dirty_read" ||
|
|
151
|
+
options.lock.mode === "pessimistic_partial_write" ||
|
|
152
|
+
options.lock.mode === "pessimistic_write_or_fail" ||
|
|
153
|
+
options.lock.mode === "for_no_key_update") {
|
|
149
154
|
var tableNames = options.lock.tables ? options.lock.tables.map(function (table) {
|
|
150
155
|
var tableAlias = qb.expressionMap.aliases.find(function (alias) {
|
|
151
156
|
return alias.metadata.tableNameWithoutPrefix === table;
|
|
@@ -192,6 +197,19 @@ var FindOptionsUtils = /** @class */ (function () {
|
|
|
192
197
|
});
|
|
193
198
|
return qb;
|
|
194
199
|
};
|
|
200
|
+
FindOptionsUtils.applyOptionsToTreeQueryBuilder = function (qb, options) {
|
|
201
|
+
if (options === null || options === void 0 ? void 0 : options.relations) {
|
|
202
|
+
// Copy because `applyRelationsRecursively` modifies it
|
|
203
|
+
var allRelations = (0, tslib_1.__spreadArray)([], (0, tslib_1.__read)(options.relations), false);
|
|
204
|
+
FindOptionsUtils.applyRelationsRecursively(qb, allRelations, qb.expressionMap.mainAlias.name, qb.expressionMap.mainAlias.metadata, "");
|
|
205
|
+
// recursive removes found relations from allRelations array
|
|
206
|
+
// if there are relations left in this array it means those relations were not found in the entity structure
|
|
207
|
+
// so, we give an exception about not found relations
|
|
208
|
+
if (allRelations.length > 0)
|
|
209
|
+
throw new FindRelationsNotFoundError_1.FindRelationsNotFoundError(allRelations);
|
|
210
|
+
}
|
|
211
|
+
return qb;
|
|
212
|
+
};
|
|
195
213
|
// -------------------------------------------------------------------------
|
|
196
214
|
// Protected Static Methods
|
|
197
215
|
// -------------------------------------------------------------------------
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/find-options/FindOptionsUtils.ts"],"names":[],"mappings":";;;;AAGA,kFAA+E;AAE/E,qDAAkD;AAClD,kCAAwC;AAExC;;GAEG;AACH;IAAA;IA4SA,CAAC;IA1SG,4EAA4E;IAC5E,wBAAwB;IACxB,4EAA4E;IAE5E;;OAEG;IACI,iCAAgB,GAAvB,UAAsC,GAAQ;QAC1C,IAAM,eAAe,GAA2B,GAAG,CAAC;QACpD,OAAO,eAAe;YACd,CACI,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC;gBACrC,eAAe,CAAC,KAAK,YAAY,MAAM;gBACvC,OAAO,eAAe,CAAC,KAAK,KAAK,QAAQ;gBACzC,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC,SAAS,CAAC;gBACxC,eAAe,CAAC,IAAI,YAAY,MAAM;gBACtC,eAAe,CAAC,KAAK,YAAY,MAAM;gBACvC,eAAe,CAAC,KAAK,YAAY,MAAM;gBACvC,OAAO,eAAe,CAAC,KAAK,KAAK,SAAS;gBAC1C,OAAO,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;;;;;;YAED,IAAI,OAAO,EAAE;gBACT,EAAE,CAAC,QAAQ,CAAC,KAAK,GAAG,GAAG,GAAG,QAAQ,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC;aACnE;YAED,qDAAqD;YACrD,oEAAoE;YACpE,IAAI,SAAS,GAAG,IAAI,CAAC;;gBACrB,KAAqB,IAAA,KAAA,sBAAA,EAAE,CAAC,aAAa,CAAC,OAAO,CAAA,gBAAA,4BAAE;oBAA1C,IAAM,MAAM,WAAA;oBACb,IAAI,MAAM,CAAC,SAAS,KAAK,SAAS,IAAI,MAAM,CAAC,OAAO,KAAK,SAAS,IAAI,MAAM,CAAC,SAAS,KAAK,aAAa,EAAE;wBACtG,SAAS;qBACZ;oBACD,SAAS,GAAG,KAAK,CAAC;oBAClB,MAAM;iBACT;;;;;;;;;YAED,IAAI,SAAS,EAAE;gBACX,EAAE,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;aAC/B;YAED,uCAAuC;YACvC,KAAI,CAAC,kBAAkB,CAAC,EAAE,EAAE,aAAa,EAAE,QAAQ,CAAC,qBAAqB,CAAC,CAAC;QAC/E,CAAC,CAAC,CAAC;IACP,CAAC;IAEL,uBAAC;AAAD,CA5SA,AA4SC,IAAA;AA5SY,4CAAgB","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 { TypeORMError } from \"../error\";\n\n/**\n * Utilities to work with FindOptions.\n */\nexport class FindOptionsUtils {\n\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>(obj: any): obj is FindOneOptions<Entity> {\n const possibleOptions: FindOneOptions<Entity> = obj;\n return possibleOptions &&\n (\n Array.isArray(possibleOptions.select) ||\n possibleOptions.where instanceof Object ||\n typeof possibleOptions.where === \"string\" ||\n Array.isArray(possibleOptions.relations) ||\n possibleOptions.join instanceof Object ||\n possibleOptions.order instanceof Object ||\n possibleOptions.cache instanceof Object ||\n typeof possibleOptions.cache === \"boolean\" ||\n typeof possibleOptions.cache === \"number\" ||\n possibleOptions.lock instanceof Object ||\n possibleOptions.loadRelationIds instanceof Object ||\n typeof possibleOptions.loadRelationIds === \"boolean\" ||\n typeof possibleOptions.loadEagerRelations === \"boolean\" ||\n typeof possibleOptions.withDeleted === \"boolean\" ||\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>(obj: any): obj is FindManyOptions<Entity> {\n const possibleOptions: FindManyOptions<Entity> = obj;\n return possibleOptions && (\n this.isFindOneOptions(possibleOptions) ||\n typeof (possibleOptions as FindManyOptions<any>).skip === \"number\" ||\n typeof (possibleOptions as FindManyOptions<any>).take === \"number\" ||\n typeof (possibleOptions as FindManyOptions<any>).skip === \"string\" ||\n typeof (possibleOptions as FindManyOptions<any>).take === \"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.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 (options.lock.mode === \"pessimistic_read\" || options.lock.mode === \"pessimistic_write\" || options.lock.mode === \"dirty_read\" || options.lock.mode === \"pessimistic_partial_write\" || options.lock.mode === \"pessimistic_write_or_fail\") {\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 // -------------------------------------------------------------------------\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(qb: SelectQueryBuilder<any>, allRelations: string[], alias: string, metadata: EntityMetadata, prefix: string): void {\n\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 => metadata.findRelationWithPropertyPath(relation));\n } else {\n matchedBaseRelations = allRelations.filter(relation => metadata.findRelationWithPropertyPath(relation));\n }\n\n // go through all matched relations and add join for them\n matchedBaseRelations.forEach(relation => {\n\n // generate a relation alias\n let relationAlias: string = DriverUtils.buildAlias(qb.connection.driver, { shorten: true, joiner: \"__\" }, alias, relation);\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(allRelations.indexOf(prefix ? prefix + \".\" + relation : relation), 1);\n\n // try to find sub-relations\n const join = qb.expressionMap.joinAttributes.find(join => join.entityOrProperty === selection);\n this.applyRelationsRecursively(qb, allRelations, join!.alias.name, join!.metadata!, prefix ? prefix + \".\" + relation : relation);\n\n // join the eager relations of the found relation\n const relMetadata = metadata.relations.find(metadata => metadata.propertyName === relation);\n if (relMetadata) {\n this.joinEagerRelations(qb, relationAlias, relMetadata.inverseEntityMetadata);\n }\n });\n }\n\n public static joinEagerRelations(qb: SelectQueryBuilder<any>, alias: string, metadata: EntityMetadata) {\n metadata.eagerRelations.forEach(relation => {\n\n // generate a relation alias\n let relationAlias = DriverUtils.buildAlias(qb.connection.driver, { shorten: true }, qb.connection.namingStrategy.eagerJoinRelationAlias(alias, relation.propertyPath));\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 !== `${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 (select.aliasName !== undefined || select.virtual !== undefined || select.selection !== relationAlias) {\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(qb, relationAlias, relation.inverseEntityMetadata);\n });\n }\n\n}\n"],"sourceRoot":".."}
|
|
1
|
+
{"version":3,"sources":["../../src/find-options/FindOptionsUtils.ts"],"names":[],"mappings":";;;;AAGA,kFAA+E;AAE/E,qDAAkD;AAClD,kCAAwC;AAGxC;;GAEG;AACH;IAAA;IAoUA,CAAC;IAlUG,4EAA4E;IAC5E,wBAAwB;IACxB,4EAA4E;IAE5E;;OAEG;IACI,iCAAgB,GAAvB,UAAsC,GAAQ;QAC1C,IAAM,eAAe,GAA2B,GAAG,CAAC;QACpD,OAAO,eAAe;YAClB,CACI,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC;gBACrC,eAAe,CAAC,KAAK,YAAY,MAAM;gBACvC,OAAO,eAAe,CAAC,KAAK,KAAK,QAAQ;gBACzC,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC,SAAS,CAAC;gBACxC,eAAe,CAAC,IAAI,YAAY,MAAM;gBACtC,eAAe,CAAC,KAAK,YAAY,MAAM;gBACvC,eAAe,CAAC,KAAK,YAAY,MAAM;gBACvC,OAAO,eAAe,CAAC,KAAK,KAAK,SAAS;gBAC1C,OAAO,eAAe,CAAC,KAAK,KAAK,QAAQ;gBACzC,eAAe,CAAC,IAAI,YAAY,MAAM;gBACtC,eAAe,CAAC,eAAe,YAAY,MA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AD,CApUA,AAoUC,IAAA;AApUY,4CAAgB","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 { TypeORMError } from \"../error\";\nimport { FindTreeOptions } from \"./FindTreeOptions\";\n\n/**\n * Utilities to work with FindOptions.\n */\nexport class FindOptionsUtils {\n\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>(obj: any): obj is FindOneOptions<Entity> {\n const possibleOptions: FindOneOptions<Entity> = obj;\n return possibleOptions &&\n (\n Array.isArray(possibleOptions.select) ||\n possibleOptions.where instanceof Object ||\n typeof possibleOptions.where === \"string\" ||\n Array.isArray(possibleOptions.relations) ||\n possibleOptions.join instanceof Object ||\n possibleOptions.order instanceof Object ||\n possibleOptions.cache instanceof Object ||\n typeof possibleOptions.cache === \"boolean\" ||\n typeof possibleOptions.cache === \"number\" ||\n possibleOptions.lock instanceof Object ||\n possibleOptions.loadRelationIds instanceof Object ||\n typeof possibleOptions.loadRelationIds === \"boolean\" ||\n typeof possibleOptions.loadEagerRelations === \"boolean\" ||\n typeof possibleOptions.withDeleted === \"boolean\" ||\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>(obj: any): obj is FindManyOptions<Entity> {\n const possibleOptions: FindManyOptions<Entity> = obj;\n return possibleOptions && (\n this.isFindOneOptions(possibleOptions) ||\n typeof (possibleOptions as FindManyOptions<any>).skip === \"number\" ||\n typeof (possibleOptions as FindManyOptions<any>).take === \"number\" ||\n typeof (possibleOptions as FindManyOptions<any>).skip === \"string\" ||\n typeof (possibleOptions as FindManyOptions<any>).take === \"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.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 ) {\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>(qb: SelectQueryBuilder<T>, options?: FindTreeOptions): SelectQueryBuilder<T> {\n if (options?.relations) {\n // Copy because `applyRelationsRecursively` modifies it\n const allRelations = [...options.relations];\n\n FindOptionsUtils.applyRelationsRecursively(qb, allRelations, qb.expressionMap.mainAlias!.name, qb.expressionMap.mainAlias!.metadata, \"\");\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(qb: SelectQueryBuilder<any>, allRelations: string[], alias: string, metadata: EntityMetadata, prefix: string): void {\n\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 => metadata.findRelationWithPropertyPath(relation));\n } else {\n matchedBaseRelations = allRelations.filter(relation => metadata.findRelationWithPropertyPath(relation));\n }\n\n // go through all matched relations and add join for them\n matchedBaseRelations.forEach(relation => {\n\n // generate a relation alias\n let relationAlias: string = DriverUtils.buildAlias(qb.connection.driver, { shorten: true, joiner: \"__\" }, alias, relation);\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(allRelations.indexOf(prefix ? prefix + \".\" + relation : relation), 1);\n\n // try to find sub-relations\n const join = qb.expressionMap.joinAttributes.find(join => join.entityOrProperty === selection);\n this.applyRelationsRecursively(qb, allRelations, join!.alias.name, join!.metadata!, prefix ? prefix + \".\" + relation : relation);\n\n // join the eager relations of the found relation\n const relMetadata = metadata.relations.find(metadata => metadata.propertyName === relation);\n if (relMetadata) {\n this.joinEagerRelations(qb, relationAlias, relMetadata.inverseEntityMetadata);\n }\n });\n }\n\n public static joinEagerRelations(qb: SelectQueryBuilder<any>, alias: string, metadata: EntityMetadata) {\n metadata.eagerRelations.forEach(relation => {\n\n // generate a relation alias\n let relationAlias = DriverUtils.buildAlias(qb.connection.driver, { shorten: true }, qb.connection.namingStrategy.eagerJoinRelationAlias(alias, relation.propertyPath));\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 !== `${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 (select.aliasName !== undefined || select.virtual !== undefined || select.selection !== relationAlias) {\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(qb, relationAlias, relation.inverseEntityMetadata);\n });\n }\n\n}\n"],"sourceRoot":".."}
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{ "name": "typeorm", "private": false, "version": "0.2.39-dev.
|
|
1
|
+
{ "name": "typeorm", "private": false, "version": "0.2.39-dev.4aaafdf", "description": "Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.", "license": "MIT", "readmeFilename": "README.md", "author": { "name": "Umed Khudoiberdiev", "email": "pleerock.me@gmail.com" }, "main": "./index.js", "browser": { "./browser/driver/aurora-data-api/AuroraDataApiDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/cockroachdb/CockroachDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/postgres/PostgresDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/oracle/OracleDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sap/SapDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mysql/MysqlDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sqlserver/SqlServerDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/MongoDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/mongodb/MongoQueryRunner.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/entity-manager/MongoEntityManager.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/repository/MongoRepository.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/sqlite/SqliteDriver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/driver/better-sqlite3/BetterSqlite3Driver.js": "./browser/platform/BrowserDisabledDriversDummy.js", "./browser/util/DirectoryExportedClassesLoader.js": "./browser/platform/BrowserDirectoryExportedClassesLoader.js", "./browser/logger/FileLogger.js": "./browser/platform/BrowserFileLoggerDummy.js", "./browser/connection/ConnectionOptionsReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/connection/options-reader/ConnectionOptionsXmlReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/connection/options-reader/ConnectionOptionsYmlReader.js": "./browser/platform/BrowserConnectionOptionsReaderDummy.js", "./browser/platform/PlatformTools.js": "./browser/platform/BrowserPlatformTools.js", "./index.js": "./browser/index.js" }, "repository": { "type": "git", "url": "https://github.com/typeorm/typeorm.git" }, "bugs": { "url": "https://github.com/typeorm/typeorm/issues" }, "tags": [ "orm", "typescript", "typescript-orm", "mysql", "mysql-orm", "postgresql", "postgresql-orm", "mariadb", "mariadb-orm", "sqlite", "sqlite-orm", "sql-server", "sql-server-orm", "oracle", "oracle-orm" ], "devDependencies": { "@types/app-root-path": "^1.2.4", "@types/chai": "^4.2.15", "@types/chai-as-promised": "^7.1.3", "@types/debug": "^4.1.5", "@types/dotenv": "^8.2.0", "@types/js-yaml": "^4.0.0", "@types/mkdirp": "^1.0.1", "@types/mocha": "^8.2.1", "@types/node": "^14.14.31", "@types/rimraf": "^3.0.0", "@types/sha.js": "^2.4.0", "@types/sinon": "^9.0.10", "@types/source-map-support": "^0.5.3", "@types/xml2js": "^0.4.8", "@types/yargs": "^17.0.2", "@typescript-eslint/eslint-plugin": "^4.15.2", "@typescript-eslint/parser": "^4.15.2", "better-sqlite3": "^7.1.2", "chai": "^4.3.0", "chai-as-promised": "^7.1.1", "class-transformer": "^0.4.0", "conventional-changelog-angular": "^5.0.12", "conventional-changelog-cli": "^2.1.1", "del": "^6.0.0", "eslint": "^7.20.0", "gulp": "^4.0.2", "gulp-eslint": "^6.0.0", "gulp-istanbul": "^1.1.3", "gulp-mocha": "^8.0.0", "gulp-rename": "^2.0.0", "gulp-replace": "^1.0.0", "gulp-shell": "^0.8.0", "gulp-sourcemaps": "^3.0.0", "gulp-typescript": "^6.0.0-alpha.1", "gulpclass": "^0.2.0", "husky": "^5.1.1", "lint-staged": "^10.5.4", "mocha": "^8.3.0", "mongodb": "^3.6.4", "mssql": "^6.3.1", "mysql": "^2.18.1", "mysql2": "^2.2.5", "oracledb": "^5.1.0", "pg": "^8.5.1", "pg-query-stream": "^4.0.0", "redis": "^3.1.1", "remap-istanbul": "^0.13.0", "rimraf": "^3.0.2", "sinon": "^9.2.4", "sinon-chai": "^3.5.0", "source-map-support": "^0.5.19", "sql.js": "^1.4.0", "sqlite3": "^5.0.2", "ts-node": "^9.1.1", "typeorm-aurora-data-api-driver": "^2.0.0", "typescript": "^4.2.2" }, "peerDependencies": { "@sap/hana-client": "*", "better-sqlite3": "*", "hdb-pool": "*", "ioredis": "*", "mongodb": "^3.6.0", "mssql": "*", "mysql2": "*", "oracledb": "*", "pg": "*", "pg-native": "*", "pg-query-stream": "*", "redis": "*", "sql.js": "*", "sqlite3": "*", "typeorm-aurora-data-api-driver": "*" }, "peerDependenciesMeta": { "@sap/hana-client": { "optional": true }, "better-sqlite3": { "optional": true }, "hdb-pool": { "optional": true }, "ioredis": { "optional": true }, "mongodb": { "optional": true }, "mssql": { "optional": true }, "mysql2": { "optional": true }, "oracledb": { "optional": true }, "pg": { "optional": true }, "pg-native": { "optional": true }, "pg-query-stream": { "optional": true }, "redis": { "optional": true }, "sql.js": { "optional": true }, "sqlite3": { "optional": true }, "typeorm-aurora-data-api-driver": { "optional": true } }, "dependencies": { "@sqltools/formatter": "^1.2.2", "app-root-path": "^3.0.0", "buffer": "^6.0.3", "chalk": "^4.1.0", "cli-highlight": "^2.1.11", "debug": "^4.3.1", "dotenv": "^8.2.0", "glob": "^7.1.6", "js-yaml": "^4.0.0", "mkdirp": "^1.0.4", "reflect-metadata": "^0.1.13", "sha.js": "^2.4.11", "tslib": "^2.1.0", "xml2js": "^0.4.23", "yargs": "^17.0.1", "zen-observable-ts": "^1.0.0" }, "lint-staged": { "*.ts": [ "eslint --fix", "git add" ] }, "scripts": { "test": "rimraf ./build && tsc && mocha --file ./build/compiled/test/utils/test-setup.js --bail --recursive --timeout 60000 ./build/compiled/test", "test-fast": "mocha --file ./build/compiled/test/utils/test-setup.js --bail --recursive --timeout 60000 ./build/compiled/test", "compile": "rimraf ./build && tsc", "watch": "./node_modules/.bin/tsc -w", "package": "gulp package", "pack": "gulp pack", "lint": "eslint -c ./.eslintrc.js src/**/*.ts test/**/*.ts sample/**/*.ts", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1" }, "bin": { "typeorm": "./cli.js" }, "funding": "https://opencollective.com/typeorm", "collective": { "type": "opencollective", "url": "https://opencollective.com/typeorm", "logo": "https://opencollective.com/opencollective/logo.txt" }, "nyc": { "all": true, "cache": false, "exclude": [ "**/*.d.ts" ], "extension": [ ".ts" ], "include": [ "build/compiled/src/**", "src/**" ], "reporter": "json" } }
|
|
@@ -48,14 +48,20 @@ var OneToManySubjectBuilder = /** @class */ (function () {
|
|
|
48
48
|
* by example: subject is "post" entity we are saving here and relation is "categories" inside it here.
|
|
49
49
|
*/
|
|
50
50
|
OneToManySubjectBuilder.prototype.buildForSubjectRelation = function (subject, relation) {
|
|
51
|
-
var _this = this;
|
|
52
51
|
// prepare objects (relation id maps) for the database entity
|
|
53
|
-
// note: subject.databaseEntity contains relations with loaded relation ids only
|
|
54
52
|
// by example: since subject is a post, we are expecting to get all post's categories saved in the database here,
|
|
55
53
|
// particularly their relation ids, e.g. category ids stored in the database
|
|
54
|
+
var _this = this;
|
|
55
|
+
// in most cases relatedEntityDatabaseValues will contain only the entity key properties.
|
|
56
|
+
// this is because subject.databaseEntity contains relations with loaded relation ids only.
|
|
57
|
+
// however if the entity uses the afterLoad hook to calculate any properties, the fetched "key object" might include ADDITIONAL properties.
|
|
58
|
+
// to handle such situations, we pass the data to relation.inverseEntityMetadata.getEntityIdMap to extract the key without any other properties.
|
|
56
59
|
var relatedEntityDatabaseRelationIds = [];
|
|
57
60
|
if (subject.databaseEntity) { // related entities in the database can exist only if this entity (post) is saved
|
|
58
|
-
|
|
61
|
+
var relatedEntityDatabaseRelation = relation.getEntityValue(subject.databaseEntity);
|
|
62
|
+
if (relatedEntityDatabaseRelation) {
|
|
63
|
+
relatedEntityDatabaseRelationIds = relatedEntityDatabaseRelation.map(function (entity) { return relation.inverseEntityMetadata.getEntityIdMap(entity); });
|
|
64
|
+
}
|
|
59
65
|
}
|
|
60
66
|
// get related entities of persisted entity
|
|
61
67
|
// by example: get categories from the passed to persist post entity
|
|
@@ -64,7 +70,7 @@ var OneToManySubjectBuilder = /** @class */ (function () {
|
|
|
64
70
|
relatedEntities = [];
|
|
65
71
|
if (relatedEntities === undefined) // if relation is undefined then nothing to update
|
|
66
72
|
return;
|
|
67
|
-
// extract only relation ids from the related entities, since we only need them for
|
|
73
|
+
// extract only relation ids from the related entities, since we only need them for comparison
|
|
68
74
|
// by example: extract from categories only relation ids (category id, or let's say category title, depend on join column options)
|
|
69
75
|
var relatedPersistedEntityRelationIds = [];
|
|
70
76
|
relatedEntities.forEach(function (relatedEntity) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/persistence/subject-builder/OneToManySubjectBuilder.ts"],"names":[],"mappings":";;;AAAA,sCAAmC;AACnC,gDAA6C;AAE7C,gEAA6D;AAG7D;;;;;;;;;;GAUG;AACH;IAEI,wEAAwE;IACxE,cAAc;IACd,wEAAwE;IAExE,iCAAsB,QAAmB;QAAnB,aAAQ,GAAR,QAAQ,CAAW;IACzC,CAAC;IAED,wEAAwE;IACxE,iBAAiB;IACjB,wEAAwE;IAExE;;OAEG;IACH,uCAAK,GAAL;QAAA,iBAWC;QAVG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAA,OAAO;YACzB,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAC,OAAO,CAAC,UAAA,QAAQ;gBAEhD,mDAAmD;gB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+E;YAC/E,0FAA0F;YAC1F,2FAA2F;YAC3F,qFAAqF;YACrF,kEAAkE;YAClE,iCAAiC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;QAC1D,CAAC,CAAC,CAAC;QAEH,6HAA6H;QAC7H,+BAAc;aACT,UAAU,CAAC,gCAAgC,EAAE,iCAAiC,CAAC;aAC/E,OAAO,CAAC,UAAA,8BAA8B;YAEnC,+FAA+F;YAC/F,iHAAiH;YACjH,0FAA0F;YAC1F,IAAM,2BAA2B,GAAG,IAAI,iBAAO,CAAC;gBAC5C,QAAQ,EAAE,QAAQ,CAAC,qBAAqB;gBACxC,aAAa,EAAE,OAAO;gBACtB,UAAU,EAAE,8BAA8B;aAC7C,CAAC,CAAC;YAEH,IAAI,CAAC,QAAQ,CAAC,eAAe,IAAI,QAAQ,CAAC,eAAe,CAAC,iBAAiB,KAAK,SAAS,EAAE;gBACvF,2BAA2B,CAAC,YAAY,GAAG,IAAI,CAAC;gBAChD,2BAA2B,CAAC,UAAU,GAAG,CAAC;wBACtC,QAAQ,EAAE,QAAQ,CAAC,eAAgB;wBACnC,KAAK,EAAE,IAAI;qBACd,CAAC,CAAC;aACN;iBAAM,IAAI,QAAQ,CAAC,eAAe,CAAC,iBAAiB,KAAK,QAAQ,EAAE;gBAChE,2BAA2B,CAAC,aAAa,GAAG,IAAI,CAAC;aACpD;YAED,KAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;QACpD,CAAC,CAAC,CAAC;IACX,CAAC;IAEL,8BAAC;AAAD,CAvKA,AAuKC,IAAA;AAvKY,0DAAuB","file":"OneToManySubjectBuilder.js","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 TypeORMError(`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.compareIds(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 identifier: removedRelatedEntityRelationId,\n });\n\n if (!relation.inverseRelation || relation.inverseRelation.orphanedRowAction === \"nullify\") {\n removedRelatedEntitySubject.canBeUpdated = true;\n removedRelatedEntitySubject.changeMaps = [{\n relation: relation.inverseRelation!,\n value: null\n }];\n } else if (relation.inverseRelation.orphanedRowAction === \"delete\") {\n removedRelatedEntitySubject.mustBeRemoved = true;\n }\n\n this.subjects.push(removedRelatedEntitySubject);\n });\n }\n\n}\n"],"sourceRoot":"../.."}
|
|
1
|
+
{"version":3,"sources":["../../src/persistence/subject-builder/OneToManySubjectBuilder.ts"],"names":[],"mappings":";;;AAAA,sCAAmC;AACnC,gDAA6C;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BAA2B,CAAC,UAAU,GAAG,CAAC;wBACtC,QAAQ,EAAE,QAAQ,CAAC,eAAgB;wBACnC,KAAK,EAAE,IAAI;qBACd,CAAC,CAAC;aACN;iBAAM,IAAI,QAAQ,CAAC,eAAe,CAAC,iBAAiB,KAAK,QAAQ,EAAE;gBAChE,2BAA2B,CAAC,aAAa,GAAG,IAAI,CAAC;aACpD;YAED,KAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;QACpD,CAAC,CAAC,CAAC;IACX,CAAC;IAEL,8BAAC;AAAD,CA9KA,AA8KC,IAAA;AA9KY,0DAAuB","file":"OneToManySubjectBuilder.js","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 // prepare objects (relation id maps) for the database entity\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\n\t\t// in most cases relatedEntityDatabaseValues will contain only the entity key properties.\n\t\t// this is because subject.databaseEntity contains relations with loaded relation ids only.\n\t\t// however if the entity uses the afterLoad hook to calculate any properties, the fetched \"key object\" might include ADDITIONAL properties.\n\t\t// to handle such situations, we pass the data to relation.inverseEntityMetadata.getEntityIdMap to extract the key without any other properties.\n\n let relatedEntityDatabaseRelationIds: ObjectLiteral[] = [];\n if (subject.databaseEntity) { // related entities in the database can exist only if this entity (post) is saved\n const relatedEntityDatabaseRelation: ObjectLiteral[] | undefined = relation.getEntityValue(subject.databaseEntity)\n if (relatedEntityDatabaseRelation) {\n relatedEntityDatabaseRelationIds = relatedEntityDatabaseRelation.map((entity) => relation.inverseEntityMetadata.getEntityIdMap(entity)!);\n }\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 comparison\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 TypeORMError(`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.compareIds(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 identifier: removedRelatedEntityRelationId,\n });\n\n if (!relation.inverseRelation || relation.inverseRelation.orphanedRowAction === \"nullify\") {\n removedRelatedEntitySubject.canBeUpdated = true;\n removedRelatedEntitySubject.changeMaps = [{\n relation: relation.inverseRelation!,\n value: null\n }];\n } else if (relation.inverseRelation.orphanedRowAction === \"delete\") {\n removedRelatedEntitySubject.mustBeRemoved = true;\n }\n\n this.subjects.push(removedRelatedEntitySubject);\n });\n }\n\n}\n"],"sourceRoot":"../.."}
|
|
@@ -597,7 +597,7 @@ export declare class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> imp
|
|
|
597
597
|
entities: Entity[];
|
|
598
598
|
raw: any[];
|
|
599
599
|
}>;
|
|
600
|
-
protected createOrderByCombinedWithSelectExpression(parentAlias: string): [string, OrderByCondition];
|
|
600
|
+
protected createOrderByCombinedWithSelectExpression(parentAlias: string): [string, OrderByCondition, string];
|
|
601
601
|
/**
|
|
602
602
|
* Loads raw results from the database.
|
|
603
603
|
*/
|
|
@@ -1461,7 +1461,7 @@ var SelectQueryBuilder = /** @class */ (function (_super) {
|
|
|
1461
1461
|
*/
|
|
1462
1462
|
SelectQueryBuilder.prototype.executeEntitiesAndRawResults = function (queryRunner) {
|
|
1463
1463
|
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
1464
|
-
var metadata, relationIdLoader, relationCountLoader, relationIdMetadataTransformer, relationCountMetadataTransformer, rawResults, entities, _a, selects, orderBys_1, metadata_1, mainAliasName_1, querySelects, condition, parameters_1, alias_1, ids, areAllNumbers, rawRelationIdResults, rawRelationCountResults, transformer;
|
|
1464
|
+
var metadata, relationIdLoader, relationCountLoader, relationIdMetadataTransformer, relationCountMetadataTransformer, rawResults, entities, _a, selects, orderBys_1, subquerySelect, metadata_1, mainAliasName_1, querySelects, condition, parameters_1, alias_1, ids, areAllNumbers, rawRelationIdResults, rawRelationCountResults, transformer;
|
|
1465
1465
|
var _this = this;
|
|
1466
1466
|
return (0, tslib_1.__generator)(this, function (_b) {
|
|
1467
1467
|
switch (_b.label) {
|
|
@@ -1483,7 +1483,7 @@ var SelectQueryBuilder = /** @class */ (function (_super) {
|
|
|
1483
1483
|
relationCountMetadataTransformer.transform();
|
|
1484
1484
|
rawResults = [], entities = [];
|
|
1485
1485
|
if (!((this.expressionMap.skip || this.expressionMap.take) && this.expressionMap.joinAttributes.length > 0)) return [3 /*break*/, 4];
|
|
1486
|
-
_a = (0, tslib_1.__read)(this.createOrderByCombinedWithSelectExpression("distinctAlias"),
|
|
1486
|
+
_a = (0, tslib_1.__read)(this.createOrderByCombinedWithSelectExpression("distinctAlias"), 3), selects = _a[0], orderBys_1 = _a[1], subquerySelect = _a[2];
|
|
1487
1487
|
metadata_1 = this.expressionMap.mainAlias.metadata;
|
|
1488
1488
|
mainAliasName_1 = this.expressionMap.mainAlias.name;
|
|
1489
1489
|
querySelects = metadata_1.primaryColumns.map(function (primaryColumn) {
|
|
@@ -1497,7 +1497,7 @@ var SelectQueryBuilder = /** @class */ (function (_super) {
|
|
|
1497
1497
|
return [4 /*yield*/, new SelectQueryBuilder(this.connection, queryRunner)
|
|
1498
1498
|
.select("DISTINCT " + querySelects.join(", "))
|
|
1499
1499
|
.addSelect(selects)
|
|
1500
|
-
.from("(" + this.clone().orderBy().getQuery() + ")", "distinctAlias")
|
|
1500
|
+
.from("(" + this.clone().orderBy().addSelect(subquerySelect).getQuery() + ")", "distinctAlias")
|
|
1501
1501
|
.offset(this.expressionMap.skip)
|
|
1502
1502
|
.limit(this.expressionMap.take)
|
|
1503
1503
|
.orderBy(orderBys_1)
|
|
@@ -1607,7 +1607,23 @@ var SelectQueryBuilder = /** @class */ (function (_super) {
|
|
|
1607
1607
|
}
|
|
1608
1608
|
}
|
|
1609
1609
|
});
|
|
1610
|
-
|
|
1610
|
+
var subquerySelectString = Object.keys(orderBys)
|
|
1611
|
+
.filter(function (orderCriteria) { return orderCriteria.includes("."); })
|
|
1612
|
+
.map(function (orderCriteria) {
|
|
1613
|
+
var criteriaParts = orderCriteria.split(".");
|
|
1614
|
+
var aliasName = criteriaParts[0];
|
|
1615
|
+
var propertyPath = criteriaParts.slice(1).join(".");
|
|
1616
|
+
var alias = _this.expressionMap.findAliasByName(aliasName);
|
|
1617
|
+
if (alias.type !== "join") {
|
|
1618
|
+
return "";
|
|
1619
|
+
}
|
|
1620
|
+
var column = alias.metadata.findColumnWithPropertyPath(propertyPath);
|
|
1621
|
+
var property = _this.escape(alias.name) + "." + _this.escape(column.databaseName);
|
|
1622
|
+
var propertyAlias = _this.escape(DriverUtils_1.DriverUtils.buildAlias(_this.connection.driver, aliasName, column.databaseName));
|
|
1623
|
+
return [property, "AS", propertyAlias].join(" ");
|
|
1624
|
+
})
|
|
1625
|
+
.join(", ");
|
|
1626
|
+
return [selectString, orderByObject, subquerySelectString];
|
|
1611
1627
|
};
|
|
1612
1628
|
/**
|
|
1613
1629
|
* Loads raw results from the database.
|