From a641c5dff86df683b84e14873e88011013258f87 Mon Sep 17 00:00:00 2001 From: Umed Khudoiberdiev Date: Wed, 23 Mar 2022 09:50:23 +0500 Subject: [PATCH] fix: resolve entities correctly in datasource when globs are specified (#8778) --- src/data-source/DataSource.ts | 9 ++++--- src/repository/BaseEntity.ts | 5 ++-- .../base-entity/base-entity.test.ts | 25 +++++++++++++++++++ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/data-source/DataSource.ts b/src/data-source/DataSource.ts index ed8b2d9162..c4d1fec07d 100644 --- a/src/data-source/DataSource.ts +++ b/src/data-source/DataSource.ts @@ -669,10 +669,11 @@ export class DataSource { ) // set current data source to the entities - for (let entityKey in flattenedEntities) { - const entity = flattenedEntities[entityKey] - if (InstanceChecker.isBaseEntityConstructor(entity)) { - entity.useDataSource(this) + for (let entityMetadata of entityMetadatas) { + if ( + InstanceChecker.isBaseEntityConstructor(entityMetadata.target) + ) { + entityMetadata.target.useDataSource(this) } } } diff --git a/src/repository/BaseEntity.ts b/src/repository/BaseEntity.ts index fa810b3cf9..e80a2837ce 100644 --- a/src/repository/BaseEntity.ts +++ b/src/repository/BaseEntity.ts @@ -27,8 +27,7 @@ export class BaseEntity { /** * DataSource used in all static methods of the BaseEntity. */ - // @ts-ignore: Unused variable which is actually used - private static dataSource?: DataSource + private static dataSource: DataSource | null // ------------------------------------------------------------------------- // Public Methods @@ -95,7 +94,7 @@ export class BaseEntity { /** * Sets DataSource to be used by entity. */ - static useDataSource(dataSource: DataSource) { + static useDataSource(dataSource: DataSource | null) { this.dataSource = dataSource } diff --git a/test/functional/base-entity/base-entity.test.ts b/test/functional/base-entity/base-entity.test.ts index 896bec4e18..5833a76a34 100644 --- a/test/functional/base-entity/base-entity.test.ts +++ b/test/functional/base-entity/base-entity.test.ts @@ -12,6 +12,31 @@ describe("base entity", () => { }) if (!dataSourceOptions.length) return + // reset data source just to make sure inside DataSource it's really being set + User.useDataSource(null) + + const dataSource = new DataSource(dataSourceOptions[0]) + await dataSource.initialize() + await dataSource.synchronize(true) + + await User.save({ name: "Timber Saw" }) + const timber = await User.findOneByOrFail({ name: "Timber Saw" }) + expect(timber).to.be.eql({ + id: 1, + name: "Timber Saw", + }) + }) + + it("test if DataSource calls `useDataSource` of the provided entities in the entities directory", async () => { + const dataSourceOptions = setupTestingConnections({ + entities: [__dirname + "/entity/*{.js,.ts}"], + enabledDrivers: ["sqlite"], + }) + if (!dataSourceOptions.length) return + + // reset data source just to make sure inside DataSource it's really being set + User.useDataSource(null) + const dataSource = new DataSource(dataSourceOptions[0]) await dataSource.initialize() await dataSource.synchronize(true)