Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve entities correctly in datasource when globs are specified #8778

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/data-source/DataSource.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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