Skip to content

Commit

Permalink
fix: resolve entities correctly in datasource when globs are specified (
Browse files Browse the repository at this point in the history
  • Loading branch information
pleerock committed Mar 23, 2022
1 parent f010205 commit a641c5d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/data-source/DataSource.ts
Expand Up @@ -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)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/repository/BaseEntity.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
25 changes: 25 additions & 0 deletions test/functional/base-entity/base-entity.test.ts
Expand Up @@ -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)
Expand Down

0 comments on commit a641c5d

Please sign in to comment.