typeorm 0.3.10-dev.f24b262 → 0.3.11-dev.2e1c9fd
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/README.md +3 -1
- package/browser/data-source/BaseDataSourceOptions.d.ts +4 -0
- package/browser/data-source/BaseDataSourceOptions.js.map +1 -1
- package/browser/decorator/columns/VirtualColumn.d.ts +10 -0
- package/browser/decorator/columns/VirtualColumn.js +46 -0
- package/browser/decorator/columns/VirtualColumn.js.map +1 -0
- package/browser/decorator/options/RelationOptions.d.ts +5 -2
- package/browser/decorator/options/RelationOptions.js.map +1 -1
- package/browser/decorator/options/VirtualColumnOptions.d.ts +27 -0
- package/browser/decorator/options/VirtualColumnOptions.js +3 -0
- package/browser/decorator/options/VirtualColumnOptions.js.map +1 -0
- package/browser/driver/aurora-mysql/AuroraMysqlConnectionOptions.d.ts +1 -0
- package/browser/driver/aurora-mysql/AuroraMysqlConnectionOptions.js.map +1 -1
- package/browser/driver/aurora-mysql/AuroraMysqlDriver.js +4 -0
- package/browser/driver/aurora-mysql/AuroraMysqlDriver.js.map +1 -1
- package/browser/driver/aurora-mysql/AuroraMysqlQueryRunner.d.ts +1 -1
- package/browser/driver/aurora-mysql/AuroraMysqlQueryRunner.js +5 -3
- package/browser/driver/aurora-mysql/AuroraMysqlQueryRunner.js.map +1 -1
- package/browser/driver/aurora-postgres/AuroraPostgresConnectionOptions.d.ts +1 -0
- package/browser/driver/aurora-postgres/AuroraPostgresConnectionOptions.js.map +1 -1
- package/browser/driver/better-sqlite3/BetterSqlite3ConnectionOptions.d.ts +1 -0
- package/browser/driver/better-sqlite3/BetterSqlite3ConnectionOptions.js.map +1 -1
- package/browser/driver/capacitor/CapacitorConnectionOptions.d.ts +1 -0
- package/browser/driver/capacitor/CapacitorConnectionOptions.js.map +1 -1
- package/browser/driver/cockroachdb/CockroachDriver.js +1 -0
- package/browser/driver/cockroachdb/CockroachDriver.js.map +1 -1
- package/browser/driver/cockroachdb/CockroachQueryRunner.d.ts +1 -1
- package/browser/driver/cockroachdb/CockroachQueryRunner.js +5 -3
- package/browser/driver/cockroachdb/CockroachQueryRunner.js.map +1 -1
- package/browser/driver/cordova/CordovaConnectionOptions.d.ts +1 -0
- package/browser/driver/cordova/CordovaConnectionOptions.js.map +1 -1
- package/browser/driver/expo/ExpoConnectionOptions.d.ts +1 -0
- package/browser/driver/expo/ExpoConnectionOptions.js.map +1 -1
- package/browser/driver/mongodb/MongoConnectionOptions.d.ts +0 -4
- package/browser/driver/mongodb/MongoConnectionOptions.js.map +1 -1
- package/browser/driver/mysql/MysqlDriver.js +9 -1
- package/browser/driver/mysql/MysqlDriver.js.map +1 -1
- package/browser/driver/mysql/MysqlQueryRunner.d.ts +1 -1
- package/browser/driver/mysql/MysqlQueryRunner.js +5 -3
- package/browser/driver/mysql/MysqlQueryRunner.js.map +1 -1
- package/browser/driver/nativescript/NativescriptConnectionOptions.d.ts +1 -0
- package/browser/driver/nativescript/NativescriptConnectionOptions.js.map +1 -1
- package/browser/driver/oracle/OracleDriver.js +6 -0
- package/browser/driver/oracle/OracleDriver.js.map +1 -1
- package/browser/driver/oracle/OracleQueryRunner.d.ts +1 -1
- package/browser/driver/oracle/OracleQueryRunner.js +5 -3
- package/browser/driver/oracle/OracleQueryRunner.js.map +1 -1
- package/browser/driver/postgres/PostgresDriver.js +5 -0
- package/browser/driver/postgres/PostgresDriver.js.map +1 -1
- package/browser/driver/postgres/PostgresQueryRunner.d.ts +1 -1
- package/browser/driver/postgres/PostgresQueryRunner.js +5 -3
- package/browser/driver/postgres/PostgresQueryRunner.js.map +1 -1
- package/browser/driver/react-native/ReactNativeConnectionOptions.d.ts +1 -0
- package/browser/driver/react-native/ReactNativeConnectionOptions.js.map +1 -1
- package/browser/driver/sap/SapConnectionOptions.d.ts +1 -0
- package/browser/driver/sap/SapConnectionOptions.js.map +1 -1
- package/browser/driver/sap/SapDriver.js +4 -0
- package/browser/driver/sap/SapDriver.js.map +1 -1
- package/browser/driver/sap/SapQueryRunner.d.ts +1 -1
- package/browser/driver/sap/SapQueryRunner.js +5 -3
- package/browser/driver/sap/SapQueryRunner.js.map +1 -1
- package/browser/driver/spanner/SpannerConnectionOptions.d.ts +1 -0
- package/browser/driver/spanner/SpannerConnectionOptions.js.map +1 -1
- package/browser/driver/spanner/SpannerDriver.js +4 -0
- package/browser/driver/spanner/SpannerDriver.js.map +1 -1
- package/browser/driver/sqlite/SqliteConnectionOptions.d.ts +1 -0
- package/browser/driver/sqlite/SqliteConnectionOptions.js.map +1 -1
- package/browser/driver/sqlite-abstract/AbstractSqliteDriver.js +4 -0
- package/browser/driver/sqlite-abstract/AbstractSqliteDriver.js.map +1 -1
- package/browser/driver/sqlite-abstract/AbstractSqliteQueryRunner.d.ts +1 -1
- package/browser/driver/sqlite-abstract/AbstractSqliteQueryRunner.js +5 -3
- package/browser/driver/sqlite-abstract/AbstractSqliteQueryRunner.js.map +1 -1
- package/browser/driver/sqljs/SqljsConnectionOptions.d.ts +1 -0
- package/browser/driver/sqljs/SqljsConnectionOptions.js.map +1 -1
- package/browser/driver/sqljs/SqljsDriver.d.ts +1 -0
- package/browser/driver/sqljs/SqljsDriver.js +3 -1
- package/browser/driver/sqljs/SqljsDriver.js.map +1 -1
- package/browser/driver/sqljs/SqljsQueryRunner.js +3 -1
- package/browser/driver/sqljs/SqljsQueryRunner.js.map +1 -1
- package/browser/driver/sqlserver/SqlServerConnectionOptions.d.ts +1 -0
- package/browser/driver/sqlserver/SqlServerConnectionOptions.js.map +1 -1
- package/browser/driver/sqlserver/SqlServerDriver.js +4 -0
- package/browser/driver/sqlserver/SqlServerDriver.js.map +1 -1
- package/browser/driver/sqlserver/SqlServerQueryRunner.d.ts +1 -1
- package/browser/driver/sqlserver/SqlServerQueryRunner.js +5 -3
- package/browser/driver/sqlserver/SqlServerQueryRunner.js.map +1 -1
- package/browser/entity-schema/EntitySchemaRelationOptions.d.ts +4 -2
- package/browser/entity-schema/EntitySchemaRelationOptions.js.map +1 -1
- package/browser/index.d.ts +1 -0
- package/browser/index.js +1 -0
- package/browser/index.js.map +1 -1
- package/browser/metadata/ColumnMetadata.d.ts +12 -0
- package/browser/metadata/ColumnMetadata.js +13 -0
- package/browser/metadata/ColumnMetadata.js.map +1 -1
- package/browser/metadata/RelationMetadata.d.ts +4 -2
- package/browser/metadata/RelationMetadata.js.map +1 -1
- package/browser/metadata-args/types/ColumnMode.d.ts +1 -1
- package/browser/metadata-args/types/ColumnMode.js.map +1 -1
- package/browser/metadata-builder/EntityMetadataValidator.js +9 -5
- package/browser/metadata-builder/EntityMetadataValidator.js.map +1 -1
- package/browser/migration/MigrationExecutor.js +13 -3
- package/browser/migration/MigrationExecutor.js.map +1 -1
- package/browser/persistence/subject-builder/OneToManySubjectBuilder.js +30 -27
- package/browser/persistence/subject-builder/OneToManySubjectBuilder.js.map +1 -1
- package/browser/query-builder/SelectQueryBuilder.js +23 -4
- package/browser/query-builder/SelectQueryBuilder.js.map +1 -1
- package/browser/query-runner/QueryRunner.d.ts +1 -1
- package/browser/query-runner/QueryRunner.js.map +1 -1
- package/browser/schema-builder/RdbmsSchemaBuilder.js +11 -5
- package/browser/schema-builder/RdbmsSchemaBuilder.js.map +1 -1
- package/browser/schema-builder/table/Table.js +1 -1
- package/browser/schema-builder/table/Table.js.map +1 -1
- package/browser/util/DateUtils.js +2 -1
- package/browser/util/DateUtils.js.map +1 -1
- package/data-source/BaseDataSourceOptions.d.ts +4 -0
- package/data-source/BaseDataSourceOptions.js.map +1 -1
- package/decorator/columns/VirtualColumn.d.ts +10 -0
- package/decorator/columns/VirtualColumn.js +50 -0
- package/decorator/columns/VirtualColumn.js.map +1 -0
- package/decorator/options/RelationOptions.d.ts +5 -2
- package/decorator/options/RelationOptions.js.map +1 -1
- package/decorator/options/VirtualColumnOptions.d.ts +27 -0
- package/decorator/options/VirtualColumnOptions.js +4 -0
- package/decorator/options/VirtualColumnOptions.js.map +1 -0
- package/driver/aurora-mysql/AuroraMysqlConnectionOptions.d.ts +1 -0
- package/driver/aurora-mysql/AuroraMysqlConnectionOptions.js.map +1 -1
- package/driver/aurora-mysql/AuroraMysqlDriver.js +4 -0
- package/driver/aurora-mysql/AuroraMysqlDriver.js.map +1 -1
- package/driver/aurora-mysql/AuroraMysqlQueryRunner.d.ts +1 -1
- package/driver/aurora-mysql/AuroraMysqlQueryRunner.js +5 -3
- package/driver/aurora-mysql/AuroraMysqlQueryRunner.js.map +1 -1
- package/driver/aurora-postgres/AuroraPostgresConnectionOptions.d.ts +1 -0
- package/driver/aurora-postgres/AuroraPostgresConnectionOptions.js.map +1 -1
- package/driver/better-sqlite3/BetterSqlite3ConnectionOptions.d.ts +1 -0
- package/driver/better-sqlite3/BetterSqlite3ConnectionOptions.js.map +1 -1
- package/driver/capacitor/CapacitorConnectionOptions.d.ts +1 -0
- package/driver/capacitor/CapacitorConnectionOptions.js.map +1 -1
- package/driver/cockroachdb/CockroachDriver.js +1 -0
- package/driver/cockroachdb/CockroachDriver.js.map +1 -1
- package/driver/cockroachdb/CockroachQueryRunner.d.ts +1 -1
- package/driver/cockroachdb/CockroachQueryRunner.js +5 -3
- package/driver/cockroachdb/CockroachQueryRunner.js.map +1 -1
- package/driver/cordova/CordovaConnectionOptions.d.ts +1 -0
- package/driver/cordova/CordovaConnectionOptions.js.map +1 -1
- package/driver/expo/ExpoConnectionOptions.d.ts +1 -0
- package/driver/expo/ExpoConnectionOptions.js.map +1 -1
- package/driver/mongodb/MongoConnectionOptions.d.ts +0 -4
- package/driver/mongodb/MongoConnectionOptions.js.map +1 -1
- package/driver/mysql/MysqlDriver.js +9 -1
- package/driver/mysql/MysqlDriver.js.map +1 -1
- package/driver/mysql/MysqlQueryRunner.d.ts +1 -1
- package/driver/mysql/MysqlQueryRunner.js +5 -3
- package/driver/mysql/MysqlQueryRunner.js.map +1 -1
- package/driver/nativescript/NativescriptConnectionOptions.d.ts +1 -0
- package/driver/nativescript/NativescriptConnectionOptions.js.map +1 -1
- package/driver/oracle/OracleDriver.js +6 -0
- package/driver/oracle/OracleDriver.js.map +1 -1
- package/driver/oracle/OracleQueryRunner.d.ts +1 -1
- package/driver/oracle/OracleQueryRunner.js +5 -3
- package/driver/oracle/OracleQueryRunner.js.map +1 -1
- package/driver/postgres/PostgresDriver.js +5 -0
- package/driver/postgres/PostgresDriver.js.map +1 -1
- package/driver/postgres/PostgresQueryRunner.d.ts +1 -1
- package/driver/postgres/PostgresQueryRunner.js +5 -3
- package/driver/postgres/PostgresQueryRunner.js.map +1 -1
- package/driver/react-native/ReactNativeConnectionOptions.d.ts +1 -0
- package/driver/react-native/ReactNativeConnectionOptions.js.map +1 -1
- package/driver/sap/SapConnectionOptions.d.ts +1 -0
- package/driver/sap/SapConnectionOptions.js.map +1 -1
- package/driver/sap/SapDriver.js +4 -0
- package/driver/sap/SapDriver.js.map +1 -1
- package/driver/sap/SapQueryRunner.d.ts +1 -1
- package/driver/sap/SapQueryRunner.js +5 -3
- package/driver/sap/SapQueryRunner.js.map +1 -1
- package/driver/spanner/SpannerConnectionOptions.d.ts +1 -0
- package/driver/spanner/SpannerConnectionOptions.js.map +1 -1
- package/driver/spanner/SpannerDriver.js +4 -0
- package/driver/spanner/SpannerDriver.js.map +1 -1
- package/driver/sqlite/SqliteConnectionOptions.d.ts +1 -0
- package/driver/sqlite/SqliteConnectionOptions.js.map +1 -1
- package/driver/sqlite-abstract/AbstractSqliteDriver.js +4 -0
- package/driver/sqlite-abstract/AbstractSqliteDriver.js.map +1 -1
- package/driver/sqlite-abstract/AbstractSqliteQueryRunner.d.ts +1 -1
- package/driver/sqlite-abstract/AbstractSqliteQueryRunner.js +5 -3
- package/driver/sqlite-abstract/AbstractSqliteQueryRunner.js.map +1 -1
- package/driver/sqljs/SqljsConnectionOptions.d.ts +1 -0
- package/driver/sqljs/SqljsConnectionOptions.js.map +1 -1
- package/driver/sqljs/SqljsDriver.d.ts +1 -0
- package/driver/sqljs/SqljsDriver.js +3 -1
- package/driver/sqljs/SqljsDriver.js.map +1 -1
- package/driver/sqljs/SqljsQueryRunner.js +3 -1
- package/driver/sqljs/SqljsQueryRunner.js.map +1 -1
- package/driver/sqlserver/SqlServerConnectionOptions.d.ts +1 -0
- package/driver/sqlserver/SqlServerConnectionOptions.js.map +1 -1
- package/driver/sqlserver/SqlServerDriver.js +4 -0
- package/driver/sqlserver/SqlServerDriver.js.map +1 -1
- package/driver/sqlserver/SqlServerQueryRunner.d.ts +1 -1
- package/driver/sqlserver/SqlServerQueryRunner.js +5 -3
- package/driver/sqlserver/SqlServerQueryRunner.js.map +1 -1
- package/entity-schema/EntitySchemaRelationOptions.d.ts +4 -2
- package/entity-schema/EntitySchemaRelationOptions.js.map +1 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/index.js.map +1 -1
- package/index.mjs +2 -0
- package/metadata/ColumnMetadata.d.ts +12 -0
- package/metadata/ColumnMetadata.js +13 -0
- package/metadata/ColumnMetadata.js.map +1 -1
- package/metadata/RelationMetadata.d.ts +4 -2
- package/metadata/RelationMetadata.js.map +1 -1
- package/metadata-args/types/ColumnMode.d.ts +1 -1
- package/metadata-args/types/ColumnMode.js.map +1 -1
- package/metadata-builder/EntityMetadataValidator.js +9 -5
- package/metadata-builder/EntityMetadataValidator.js.map +1 -1
- package/migration/MigrationExecutor.js +13 -3
- package/migration/MigrationExecutor.js.map +1 -1
- package/package.json +1 -1
- package/persistence/subject-builder/OneToManySubjectBuilder.js +30 -27
- package/persistence/subject-builder/OneToManySubjectBuilder.js.map +1 -1
- package/query-builder/SelectQueryBuilder.js +23 -4
- package/query-builder/SelectQueryBuilder.js.map +1 -1
- package/query-runner/QueryRunner.d.ts +1 -1
- package/query-runner/QueryRunner.js.map +1 -1
- package/schema-builder/RdbmsSchemaBuilder.js +11 -5
- package/schema-builder/RdbmsSchemaBuilder.js.map +1 -1
- package/schema-builder/table/Table.js +1 -1
- package/schema-builder/table/Table.js.map +1 -1
- package/util/DateUtils.js +2 -1
- package/util/DateUtils.js.map +1 -1
|
@@ -47,6 +47,7 @@ export class OneToManySubjectBuilder {
|
|
|
47
47
|
// prepare objects (relation id maps) for the database entity
|
|
48
48
|
// by example: since subject is a post, we are expecting to get all post's categories saved in the database here,
|
|
49
49
|
// particularly their relation ids, e.g. category ids stored in the database
|
|
50
|
+
var _a;
|
|
50
51
|
// in most cases relatedEntityDatabaseValues will contain only the entity key properties.
|
|
51
52
|
// this is because subject.databaseEntity contains relations with loaded relation ids only.
|
|
52
53
|
// however if the entity uses the afterLoad hook to calculate any properties, the fetched "key object" might include ADDITIONAL properties.
|
|
@@ -141,34 +142,36 @@ export class OneToManySubjectBuilder {
|
|
|
141
142
|
relatedPersistedEntityRelationIds.push(relationIdMap);
|
|
142
143
|
});
|
|
143
144
|
// find what related entities were added and what were removed based on difference between what we save and what database has
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
145
|
+
if (((_a = relation.inverseRelation) === null || _a === void 0 ? void 0 : _a.orphanedRowAction) !== "disable") {
|
|
146
|
+
EntityMetadata.difference(relatedEntityDatabaseRelationIds, relatedPersistedEntityRelationIds).forEach((removedRelatedEntityRelationId) => {
|
|
147
|
+
// by example: removedRelatedEntityRelationId is category that was bind in the database before, but now its unbind
|
|
148
|
+
// todo: probably we can improve this in the future by finding entity with column those values,
|
|
149
|
+
// todo: maybe it was already in persistence process. This is possible due to unique requirements of join columns
|
|
150
|
+
// we create a new subject which operations will be executed in subject operation executor
|
|
151
|
+
const removedRelatedEntitySubject = new Subject({
|
|
152
|
+
metadata: relation.inverseEntityMetadata,
|
|
153
|
+
parentSubject: subject,
|
|
154
|
+
identifier: removedRelatedEntityRelationId,
|
|
155
|
+
});
|
|
156
|
+
if (!relation.inverseRelation ||
|
|
157
|
+
relation.inverseRelation.orphanedRowAction === "nullify") {
|
|
158
|
+
removedRelatedEntitySubject.canBeUpdated = true;
|
|
159
|
+
removedRelatedEntitySubject.changeMaps = [
|
|
160
|
+
{
|
|
161
|
+
relation: relation.inverseRelation,
|
|
162
|
+
value: null,
|
|
163
|
+
},
|
|
164
|
+
];
|
|
165
|
+
}
|
|
166
|
+
else if (relation.inverseRelation.orphanedRowAction === "delete") {
|
|
167
|
+
removedRelatedEntitySubject.mustBeRemoved = true;
|
|
168
|
+
}
|
|
169
|
+
else if (relation.inverseRelation.orphanedRowAction === "soft-delete") {
|
|
170
|
+
removedRelatedEntitySubject.canBeSoftRemoved = true;
|
|
171
|
+
}
|
|
172
|
+
this.subjects.push(removedRelatedEntitySubject);
|
|
153
173
|
});
|
|
154
|
-
|
|
155
|
-
relation.inverseRelation.orphanedRowAction === "nullify") {
|
|
156
|
-
removedRelatedEntitySubject.canBeUpdated = true;
|
|
157
|
-
removedRelatedEntitySubject.changeMaps = [
|
|
158
|
-
{
|
|
159
|
-
relation: relation.inverseRelation,
|
|
160
|
-
value: null,
|
|
161
|
-
},
|
|
162
|
-
];
|
|
163
|
-
}
|
|
164
|
-
else if (relation.inverseRelation.orphanedRowAction === "delete") {
|
|
165
|
-
removedRelatedEntitySubject.mustBeRemoved = true;
|
|
166
|
-
}
|
|
167
|
-
else if (relation.inverseRelation.orphanedRowAction === "soft-delete") {
|
|
168
|
-
removedRelatedEntitySubject.canBeSoftRemoved = true;
|
|
169
|
-
}
|
|
170
|
-
this.subjects.push(removedRelatedEntitySubject);
|
|
171
|
-
});
|
|
174
|
+
}
|
|
172
175
|
}
|
|
173
176
|
}
|
|
174
177
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../browser/src/persistence/subject-builder/OneToManySubjectBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAE9C,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAA;AAG9D;;;;;;;;;;GAUG;AACH,MAAM,OAAO,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2B,CAAC,UAAU,GAAG;oBACrC;wBACI,QAAQ,EAAE,QAAQ,CAAC,eAAgB;wBACnC,KAAK,EAAE,IAAI;qBACd;iBACJ,CAAA;aACJ;iBAAM,IACH,QAAQ,CAAC,eAAe,CAAC,iBAAiB,KAAK,QAAQ,EACzD;gBACE,2BAA2B,CAAC,aAAa,GAAG,IAAI,CAAA;aACnD;iBAAM,IACH,QAAQ,CAAC,eAAe,CAAC,iBAAiB,KAAK,aAAa,EAC9D;gBACE,2BAA2B,CAAC,gBAAgB,GAAG,IAAI,CAAA;aACtD;YAED,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAA;QACnD,CAAC,CAAC,CAAA;IACN,CAAC;CACJ","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 // Constructor\n // ---------------------------------------------------------------------\n\n constructor(protected subjects: Subject[]) {}\n\n // ---------------------------------------------------------------------\n // Public Methods\n // ---------------------------------------------------------------------\n\n /**\n * Builds all required operations.\n */\n build(): void {\n this.subjects.forEach((subject) => {\n subject.metadata.oneToManyRelations.forEach((relation) => {\n // skip relations for which persistence is disabled\n if (relation.persistenceEnabled === false) return\n\n this.buildForSubjectRelation(subject, relation)\n })\n })\n }\n\n // ---------------------------------------------------------------------\n // Protected Methods\n // ---------------------------------------------------------------------\n\n /**\n * Builds operations for a given subject and relation.\n *\n * by example: subject is \"post\" entity we are saving here and relation is \"categories\" inside it here.\n */\n protected buildForSubjectRelation(\n subject: Subject,\n relation: RelationMetadata,\n ) {\n // 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 // in most cases relatedEntityDatabaseValues will contain only the entity key properties.\n // this is because subject.databaseEntity contains relations with loaded relation ids only.\n // however if the entity uses the afterLoad hook to calculate any properties, the fetched \"key object\" might include ADDITIONAL properties.\n // 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) {\n // related entities in the database can exist only if this entity (post) is saved\n const relatedEntityDatabaseRelation: ObjectLiteral[] | undefined =\n relation.getEntityValue(subject.databaseEntity)\n if (relatedEntityDatabaseRelation) {\n relatedEntityDatabaseRelationIds =\n relatedEntityDatabaseRelation.map(\n (entity) =>\n relation.inverseEntityMetadata.getEntityIdMap(\n entity,\n )!,\n )\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(\n subject.entity!,\n )\n if (relatedEntities === null)\n // we treat relations set to null as removed, so we don't skip it\n relatedEntities = [] as ObjectLiteral[]\n if (relatedEntities === undefined)\n // 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) => {\n // by example: relatedEntity is a category here\n let relationIdMap =\n relation.inverseEntityMetadata!.getEntityIdMap(relatedEntity) // by example: relationIdMap is category.id map here, e.g. { id: ... }\n\n // try to find a subject of this related entity, maybe it was loaded or was marked for persistence\n let relatedEntitySubject = this.subjects.find((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 // 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) 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 =\n relatedEntityDatabaseRelationIds.find(\n (relatedDatabaseEntityRelationId) => {\n return OrmUtils.compareIds(\n relationIdMap,\n relatedDatabaseEntityRelationId,\n )\n },\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 // 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.difference(\n relatedEntityDatabaseRelationIds,\n relatedPersistedEntityRelationIds,\n ).forEach((removedRelatedEntityRelationId) => {\n // 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 (\n !relation.inverseRelation ||\n relation.inverseRelation.orphanedRowAction === \"nullify\"\n ) {\n removedRelatedEntitySubject.canBeUpdated = true\n removedRelatedEntitySubject.changeMaps = [\n {\n relation: relation.inverseRelation!,\n value: null,\n },\n ]\n } else if (\n relation.inverseRelation.orphanedRowAction === \"delete\"\n ) {\n removedRelatedEntitySubject.mustBeRemoved = true\n } else if (\n relation.inverseRelation.orphanedRowAction === \"soft-delete\"\n ) {\n removedRelatedEntitySubject.canBeSoftRemoved = true\n }\n\n this.subjects.push(removedRelatedEntitySubject)\n })\n }\n}\n"],"sourceRoot":"../.."}
|
|
1
|
+
{"version":3,"sources":["../browser/src/persistence/subject-builder/OneToManySubjectBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAE9C,OAAO,EAAE,cAAc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;gBAEF,IACI,CAAC,QAAQ,CAAC,eAAe;oBACzB,QAAQ,CAAC,eAAe,CAAC,iBAAiB,KAAK,SAAS,EAC1D;oBACE,2BAA2B,CAAC,YAAY,GAAG,IAAI,CAAA;oBAC/C,2BAA2B,CAAC,UAAU,GAAG;wBACrC;4BACI,QAAQ,EAAE,QAAQ,CAAC,eAAgB;4BACnC,KAAK,EAAE,IAAI;yBACd;qBACJ,CAAA;iBACJ;qBAAM,IACH,QAAQ,CAAC,eAAe,CAAC,iBAAiB,KAAK,QAAQ,EACzD;oBACE,2BAA2B,CAAC,aAAa,GAAG,IAAI,CAAA;iBACnD;qBAAM,IACH,QAAQ,CAAC,eAAe,CAAC,iBAAiB,KAAK,aAAa,EAC9D;oBACE,2BAA2B,CAAC,gBAAgB,GAAG,IAAI,CAAA;iBACtD;gBAED,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAA;YACnD,CAAC,CAAC,CAAA;SACL;IACL,CAAC;CACJ","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 // Constructor\n // ---------------------------------------------------------------------\n\n constructor(protected subjects: Subject[]) {}\n\n // ---------------------------------------------------------------------\n // Public Methods\n // ---------------------------------------------------------------------\n\n /**\n * Builds all required operations.\n */\n build(): void {\n this.subjects.forEach((subject) => {\n subject.metadata.oneToManyRelations.forEach((relation) => {\n // skip relations for which persistence is disabled\n if (relation.persistenceEnabled === false) return\n\n this.buildForSubjectRelation(subject, relation)\n })\n })\n }\n\n // ---------------------------------------------------------------------\n // Protected Methods\n // ---------------------------------------------------------------------\n\n /**\n * Builds operations for a given subject and relation.\n *\n * by example: subject is \"post\" entity we are saving here and relation is \"categories\" inside it here.\n */\n protected buildForSubjectRelation(\n subject: Subject,\n relation: RelationMetadata,\n ) {\n // 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 // in most cases relatedEntityDatabaseValues will contain only the entity key properties.\n // this is because subject.databaseEntity contains relations with loaded relation ids only.\n // however if the entity uses the afterLoad hook to calculate any properties, the fetched \"key object\" might include ADDITIONAL properties.\n // 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) {\n // related entities in the database can exist only if this entity (post) is saved\n const relatedEntityDatabaseRelation: ObjectLiteral[] | undefined =\n relation.getEntityValue(subject.databaseEntity)\n if (relatedEntityDatabaseRelation) {\n relatedEntityDatabaseRelationIds =\n relatedEntityDatabaseRelation.map(\n (entity) =>\n relation.inverseEntityMetadata.getEntityIdMap(\n entity,\n )!,\n )\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(\n subject.entity!,\n )\n if (relatedEntities === null)\n // we treat relations set to null as removed, so we don't skip it\n relatedEntities = [] as ObjectLiteral[]\n if (relatedEntities === undefined)\n // 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) => {\n // by example: relatedEntity is a category here\n let relationIdMap =\n relation.inverseEntityMetadata!.getEntityIdMap(relatedEntity) // by example: relationIdMap is category.id map here, e.g. { id: ... }\n\n // try to find a subject of this related entity, maybe it was loaded or was marked for persistence\n let relatedEntitySubject = this.subjects.find((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 // 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) 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 =\n relatedEntityDatabaseRelationIds.find(\n (relatedDatabaseEntityRelationId) => {\n return OrmUtils.compareIds(\n relationIdMap,\n relatedDatabaseEntityRelationId,\n )\n },\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 // 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 if (relation.inverseRelation?.orphanedRowAction !== \"disable\") {\n EntityMetadata.difference(\n relatedEntityDatabaseRelationIds,\n relatedPersistedEntityRelationIds,\n ).forEach((removedRelatedEntityRelationId) => {\n // 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 (\n !relation.inverseRelation ||\n relation.inverseRelation.orphanedRowAction === \"nullify\"\n ) {\n removedRelatedEntitySubject.canBeUpdated = true\n removedRelatedEntitySubject.changeMaps = [\n {\n relation: relation.inverseRelation!,\n value: null,\n },\n ]\n } else if (\n relation.inverseRelation.orphanedRowAction === \"delete\"\n ) {\n removedRelatedEntitySubject.mustBeRemoved = true\n } else if (\n relation.inverseRelation.orphanedRowAction === \"soft-delete\"\n ) {\n removedRelatedEntitySubject.canBeSoftRemoved = true\n }\n\n this.subjects.push(removedRelatedEntitySubject)\n })\n }\n }\n}\n"],"sourceRoot":"../.."}
|
|
@@ -1482,8 +1482,12 @@ export class SelectQueryBuilder extends QueryBuilder {
|
|
|
1482
1482
|
: [];
|
|
1483
1483
|
const allColumns = [...columns, ...nonSelectedPrimaryColumns];
|
|
1484
1484
|
const finalSelects = [];
|
|
1485
|
+
const escapedAliasName = this.escape(aliasName);
|
|
1485
1486
|
allColumns.forEach((column) => {
|
|
1486
|
-
let selectionPath =
|
|
1487
|
+
let selectionPath = escapedAliasName + "." + this.escape(column.databaseName);
|
|
1488
|
+
if (column.isVirtualProperty && column.query) {
|
|
1489
|
+
selectionPath = `(${column.query(escapedAliasName)})`;
|
|
1490
|
+
}
|
|
1487
1491
|
if (this.connection.driver.spatialTypes.indexOf(column.type) !== -1) {
|
|
1488
1492
|
if (DriverUtils.isMySQLFamily(this.connection.driver) ||
|
|
1489
1493
|
this.connection.driver.options.type === "aurora-mysql") {
|
|
@@ -2109,7 +2113,7 @@ export class SelectQueryBuilder extends QueryBuilder {
|
|
|
2109
2113
|
// this.addSelect(alias + "." + propertyPath);
|
|
2110
2114
|
}
|
|
2111
2115
|
else if (embed) {
|
|
2112
|
-
this.buildSelect(select[key], metadata, alias,
|
|
2116
|
+
this.buildSelect(select[key], metadata, alias, propertyPath);
|
|
2113
2117
|
// } else if (relation) {
|
|
2114
2118
|
// const joinAlias = alias + "_" + relation.propertyName;
|
|
2115
2119
|
// const existJoin = this.joins.find(join => join.alias === joinAlias);
|
|
@@ -2260,7 +2264,19 @@ export class SelectQueryBuilder extends QueryBuilder {
|
|
|
2260
2264
|
: (nulls === null || nulls === void 0 ? void 0 : nulls.toLowerCase()) === "last"
|
|
2261
2265
|
? "NULLS LAST"
|
|
2262
2266
|
: undefined;
|
|
2263
|
-
|
|
2267
|
+
let aliasPath = `${alias}.${propertyPath}`;
|
|
2268
|
+
if (column.isVirtualProperty && column.query) {
|
|
2269
|
+
const selection = this.expressionMap.selects.find((s) => s.selection === aliasPath);
|
|
2270
|
+
if (selection) {
|
|
2271
|
+
// this is not building correctly now???
|
|
2272
|
+
aliasPath = DriverUtils.buildAlias(this.connection.driver, alias, column.databaseName);
|
|
2273
|
+
selection.aliasName = aliasPath;
|
|
2274
|
+
}
|
|
2275
|
+
else {
|
|
2276
|
+
aliasPath = `(${column.query(alias)})`;
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
this.addOrderBy(aliasPath, direction, nulls);
|
|
2264
2280
|
// this.orderBys.push({ alias: alias + "." + propertyPath, direction, nulls });
|
|
2265
2281
|
}
|
|
2266
2282
|
else if (embed) {
|
|
@@ -2314,7 +2330,10 @@ export class SelectQueryBuilder extends QueryBuilder {
|
|
|
2314
2330
|
if (!embed && !column && !relation)
|
|
2315
2331
|
throw new EntityPropertyNotFoundError(propertyPath, metadata);
|
|
2316
2332
|
if (column) {
|
|
2317
|
-
|
|
2333
|
+
let aliasPath = `${alias}.${propertyPath}`;
|
|
2334
|
+
if (column.isVirtualProperty && column.query) {
|
|
2335
|
+
aliasPath = `(${column.query(alias)})`;
|
|
2336
|
+
}
|
|
2318
2337
|
// const parameterName = alias + "_" + propertyPath.split(".").join("_") + "_" + parameterIndex;
|
|
2319
2338
|
// todo: we need to handle other operators as well?
|
|
2320
2339
|
let parameterValue = where[key];
|