Browse Source

feat(core): Deprecation of getRepository without context argument (#1603)

Allowing the use of TransactionalConnection.getRepository() _without_ a `ctx` object makes it too easy to accidentally write code that is accidentally outside of an ongoing transaction. Therefore we are deprecating it here and in v2 we will remove it altogether.
Alexander Shitikov 3 years ago
parent
commit
9ec2fe5831

+ 3 - 1
packages/core/src/common/error/errors.ts

@@ -93,7 +93,9 @@ export class ChannelNotFoundError extends I18nError {
  * @docsPage Error Types
  */
 export class EntityNotFoundError extends I18nError {
-    constructor(entityName: keyof typeof coreEntitiesMap, id: ID) {
+    constructor(entityName: keyof typeof coreEntitiesMap, id: ID)
+    constructor(entityName: string, id: ID)
+    constructor(entityName: keyof typeof coreEntitiesMap | string, id: ID) {
         super('error.entity-with-id-not-found', { entityName, id }, 'ENTITY_NOT_FOUND', LogLevel.Warn);
     }
 }

+ 4 - 3
packages/core/src/connection/transactional-connection.ts

@@ -7,7 +7,6 @@ import {
     EntitySchema,
     FindOneOptions,
     FindOptionsUtils,
-    getRepository,
     ObjectType,
     Repository,
 } from 'typeorm';
@@ -55,6 +54,8 @@ export class TransactionalConnection {
      * Returns a TypeORM repository. Note that when no RequestContext is supplied, the repository will not
      * be aware of any existing transaction. Therefore calling this method without supplying a RequestContext
      * is discouraged without a deliberate reason.
+     * 
+     * @deprecated since 1.7.0: Use {@link TransactionalConnection.rawConnection rawConnection.getRepository()} function instead.
      */
     getRepository<Entity>(target: ObjectType<Entity> | EntitySchema<Entity> | string): Repository<Entity>;
     /**
@@ -78,11 +79,11 @@ export class TransactionalConnection {
                 return transactionManager.getRepository(maybeTarget!);
             } else {
                 // tslint:disable-next-line:no-non-null-assertion
-                return getRepository(maybeTarget!);
+                return this.rawConnection.getRepository(maybeTarget!);
             }
         } else {
             // tslint:disable-next-line:no-non-null-assertion
-            return getRepository(ctxOrTarget ?? maybeTarget!);
+            return this.rawConnection.getRepository(ctxOrTarget ?? maybeTarget!);
         }
     }