Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): allow mapping database defaults from inline embeddables (#…
- Loading branch information
Showing
5 changed files
with
122 additions
and
3 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
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,60 @@ | ||
import { Embeddable, Embedded, Entity, PrimaryKey, Property, MikroORM } from '@mikro-orm/mysql'; | ||
|
||
@Embeddable() | ||
class NestedTime { | ||
|
||
@Property({ defaultRaw: 'current_timestamp' }) | ||
timestamp!: Date; | ||
|
||
} | ||
|
||
@Embeddable() | ||
class Time { | ||
|
||
@Property({ defaultRaw: 'current_timestamp' }) | ||
timestamp!: Date; | ||
|
||
@Embedded(() => NestedTime) | ||
nested!: NestedTime; | ||
|
||
} | ||
|
||
@Entity() | ||
class Test { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Embedded(() => Time) | ||
time!: Time; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [Test], | ||
dbName: '3887', | ||
port: 3308, | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test('reloading database defaults from inlined embeddables', async () => { | ||
const test = new Test(); | ||
test.time = {} as Time; | ||
test.time.nested = {} as Time; | ||
|
||
await orm.em.fork().persistAndFlush(test); | ||
expect(test.time.timestamp).toBeInstanceOf(Date); | ||
expect(test.time.nested.timestamp).toBeInstanceOf(Date); | ||
|
||
const fetched = await orm.em.fork().findOneOrFail(Test, test); | ||
expect(fetched.time.timestamp).toBeInstanceOf(Date); | ||
expect(fetched.time.nested.timestamp).toBeInstanceOf(Date); | ||
}); |
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,59 @@ | ||
import { Embeddable, Embedded, Entity, PrimaryKey, Property, MikroORM } from '@mikro-orm/better-sqlite'; | ||
|
||
@Embeddable() | ||
class NestedTime { | ||
|
||
@Property({ defaultRaw: 'current_timestamp' }) | ||
timestamp!: Date; | ||
|
||
} | ||
|
||
@Embeddable() | ||
class Time { | ||
|
||
@Property({ defaultRaw: 'current_timestamp' }) | ||
timestamp!: Date; | ||
|
||
@Embedded(() => NestedTime) | ||
nested!: NestedTime; | ||
|
||
} | ||
|
||
@Entity() | ||
class Test { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Embedded(() => Time) | ||
time!: Time; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [Test], | ||
dbName: ':memory:', | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test('reloading database defaults from inlined embeddables', async () => { | ||
const test = new Test(); | ||
test.time = {} as Time; | ||
test.time.nested = {} as Time; | ||
|
||
await orm.em.fork().persistAndFlush(test); | ||
expect(test.time.timestamp).toBeInstanceOf(Date); | ||
expect(test.time.nested.timestamp).toBeInstanceOf(Date); | ||
|
||
const fetched = await orm.em.fork().findOneOrFail(Test, test); | ||
expect(fetched.time.timestamp).toBeInstanceOf(Date); | ||
expect(fetched.time.nested.timestamp).toBeInstanceOf(Date); | ||
}); |
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