Skip to content

Commit

Permalink
fix(core): fix folder-based discovery for multiple entities in single…
Browse files Browse the repository at this point in the history
… file (#5464)
  • Loading branch information
xmajzel committed Apr 17, 2024
1 parent 02494bf commit d64be7e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/core/src/metadata/MetadataDiscovery.ts
Expand Up @@ -267,9 +267,8 @@ export class MetadataDiscovery {
continue;
}

this.metadata.set(name, Utils.copy(MetadataStorage.getMetadata(name, path), false));
const entity = this.prepare(target) as Constructor<AnyEntity>;
const schema = this.getSchema(entity);
const schema = this.getSchema(entity, path);
const meta = schema.init().meta;
this.metadata.set(meta.className, meta);

Expand Down Expand Up @@ -323,18 +322,23 @@ export class MetadataDiscovery {
return entity.schema;
}

if (EntitySchema.REGISTRY.has(entity)) {
return EntitySchema.REGISTRY.get(entity)!;
}

return entity as EntityClass<T>;
}

private getSchema<T>(entity: Constructor<T> | EntitySchema<T>): EntitySchema<T> {
private getSchema<T>(entity: Constructor<T> | EntitySchema<T>, filepath?: string): EntitySchema<T> {
if (entity instanceof EntitySchema) {
if (filepath) {
// This will initialize global metadata for 'entity.meta.className'
const meta = Utils.copy(MetadataStorage.getMetadata(entity.meta.className, filepath), false);
this.metadata.set(entity.meta.className, meta);
}
return entity;
}

if (EntitySchema.REGISTRY.has(entity)) {
return EntitySchema.REGISTRY.get(entity)!;
}

const path = (entity as Dictionary).__path;

if (path) {
Expand Down
14 changes: 14 additions & 0 deletions tests/MikroORM.test.ts
Expand Up @@ -52,6 +52,20 @@ describe('MikroORM', () => {
await expect(MikroORM.init({ driver: MongoDriver, dbName: 'test', baseDir: BASE_DIR, entities: ['entities-1', 'entities-2'] })).rejects.toThrow('Duplicate entity names are not allowed: Dup1, Dup2');
});

test('should NOT throw when multiple entities in same file were discovered', async () => {
const orm = await MikroORM.init({
driver: SqliteDriver,
dbName: ':memory:',
baseDir: BASE_DIR,
connect: false,
entities: ['complex-entities/**/*.entity.js'],
entitiesTs: ['complex-entities/**/*.entity.ts'],
});

expect(orm).toBeInstanceOf(MikroORM);
expect(Object.keys(orm.getMetadata().getAll()).sort()).toEqual(['AbstractClass', 'ClassA', 'ClassB']);
});

test('should NOT throw when multiple entities with same file name discovered ("checkDuplicateEntities" false)', async () => {
const ormInitCommandPromise = MikroORM.init({ driver: MongoDriver, dbName: 'test', baseDir: BASE_DIR, entities: ['entities-1', 'entities-2'], discovery: { checkDuplicateEntities: false } });

Expand Down
26 changes: 26 additions & 0 deletions tests/complex-entities/abstract-class.entity.ts
@@ -0,0 +1,26 @@
import {
Entity,
Enum, BaseEntity, PrimaryKey,
} from '@mikro-orm/postgresql';

export enum SomeEnum {
CLASS_A = 'class-a',
CLASS_B = 'class-b',
}

@Entity({ discriminatorColumn: 'type', abstract: true })
export class AbstractClass extends BaseEntity {

@PrimaryKey()
id!: string;

@Enum(() => SomeEnum)
type!: SomeEnum;

}

@Entity({ discriminatorValue: SomeEnum.CLASS_A })
export class ClassA extends AbstractClass {}

@Entity({ discriminatorValue: SomeEnum.CLASS_B })
export class ClassB extends AbstractClass {}

0 comments on commit d64be7e

Please sign in to comment.