|
| 1 | +import { EntitySchema, SimpleLogger } from '@mikro-orm/core'; |
| 2 | +import { MikroORM } from '@mikro-orm/sqlite'; |
| 3 | + |
| 4 | +abstract class BaseClass { |
| 5 | + |
| 6 | + id?: number; |
| 7 | + |
| 8 | +} |
| 9 | + |
| 10 | +interface BaseInterface { |
| 11 | + id?: number; |
| 12 | +} |
| 13 | + |
| 14 | +class DerivedClass extends BaseClass { |
| 15 | + |
| 16 | + name?: string; |
| 17 | + |
| 18 | +} |
| 19 | + |
| 20 | +class ImplementingClass implements BaseInterface { |
| 21 | + |
| 22 | + id?: number; |
| 23 | + name?: string; |
| 24 | + |
| 25 | +} |
| 26 | + |
| 27 | +const BaseClassSchema = new EntitySchema<BaseClass>({ |
| 28 | + name: 'BaseClass', |
| 29 | + abstract: true, |
| 30 | + properties: { |
| 31 | + id: { type: Number, primary: true }, |
| 32 | + }, |
| 33 | +}); |
| 34 | + |
| 35 | +const BaseInterfaceSchema = new EntitySchema<BaseInterface>({ |
| 36 | + name: 'BaseInterface', |
| 37 | + abstract: true, |
| 38 | + properties: { |
| 39 | + id: { type: Number, primary: true }, |
| 40 | + }, |
| 41 | +}); |
| 42 | + |
| 43 | +const DerivedClassSchema = new EntitySchema<DerivedClass>({ |
| 44 | + class: DerivedClass, |
| 45 | + extends: 'BaseClass', |
| 46 | + properties: { |
| 47 | + name: { type: String }, |
| 48 | + }, |
| 49 | +}); |
| 50 | + |
| 51 | +const ImplementingClassSchema = new EntitySchema<ImplementingClass>({ |
| 52 | + class: ImplementingClass, |
| 53 | + extends: 'BaseInterface', |
| 54 | + properties: { |
| 55 | + name: { type: String }, |
| 56 | + }, |
| 57 | +}); |
| 58 | + |
| 59 | +let orm: MikroORM; |
| 60 | + |
| 61 | +beforeAll(async () => { |
| 62 | + const logger = jest.fn(); |
| 63 | + orm = await MikroORM.init({ |
| 64 | + entities: [ |
| 65 | + BaseClassSchema, |
| 66 | + DerivedClassSchema, |
| 67 | + BaseInterfaceSchema, |
| 68 | + ImplementingClassSchema, |
| 69 | + ], |
| 70 | + dbName: `:memory:`, |
| 71 | + logger: msg => logger(msg), |
| 72 | + loggerFactory: options => new SimpleLogger(options), |
| 73 | + debug: true, |
| 74 | + }); |
| 75 | + expect(logger.mock.calls.toString()).not.toMatch('undefined'); |
| 76 | + await orm.schema.refreshDatabase(); |
| 77 | +}); |
| 78 | + |
| 79 | +afterAll(() => orm.close(true)); |
| 80 | + |
| 81 | +test('4080', async () => { |
| 82 | + orm.em.create(DerivedClassSchema, { name: 'foo' }); |
| 83 | + await orm.em.flush(); |
| 84 | + orm.em.clear(); |
| 85 | + |
| 86 | + await orm.em.findOneOrFail(DerivedClassSchema, { name: 'foo' }); |
| 87 | +}); |
0 commit comments