Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): fix nested inlined embedded property hydration
Closes #4145
- Loading branch information
Showing
4 changed files
with
117 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Embeddable, Embedded, Entity, PrimaryKey, Property, wrap } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/better-sqlite'; | ||
|
||
@Embeddable() | ||
class Time { | ||
|
||
@Property() | ||
hour: number; | ||
|
||
@Property() | ||
minute: number; | ||
|
||
constructor(hour: number, minute: number) { | ||
this.hour = hour; | ||
this.minute = minute; | ||
} | ||
|
||
} | ||
|
||
@Embeddable() | ||
class TimeInterval { | ||
|
||
@Embedded(() => Time) | ||
start!: Time; | ||
|
||
@Embedded(() => Time) | ||
end!: Time; | ||
|
||
} | ||
|
||
@Entity() | ||
class Example { | ||
|
||
@PrimaryKey() | ||
name!: string; | ||
|
||
@Embedded(() => TimeInterval) | ||
timeInterval!: TimeInterval; | ||
|
||
} | ||
|
||
describe('embedded entities without other properties', () => { | ||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [Example], | ||
dbName: ':memory:', | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test('fetching entity should populate embedded property', async () => { | ||
const example = new Example(); | ||
example.name = 'Test'; | ||
example.timeInterval = new TimeInterval(); | ||
example.timeInterval.start = new Time(9, 0); | ||
example.timeInterval.end = new Time(17, 59); | ||
|
||
expect(wrap(example.timeInterval).toJSON()).toEqual({ | ||
start: { | ||
hour: 9, | ||
minute: 0, | ||
}, | ||
end: { | ||
hour: 17, | ||
minute: 59, | ||
}, | ||
}); | ||
|
||
await orm.em.persistAndFlush(example); | ||
orm.em.clear(); | ||
|
||
const fetched = await orm.em.findOneOrFail(Example, { name: example.name }); | ||
expect(fetched.timeInterval).not.toBeUndefined(); | ||
expect(wrap(fetched.timeInterval).toJSON()).toEqual({ | ||
start: { | ||
hour: 9, | ||
minute: 0, | ||
}, | ||
end: { | ||
hour: 17, | ||
minute: 59, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters