Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): convert custom types when snapshotting scalar composite keys
Closes #3988
- Loading branch information
Showing
3 changed files
with
151 additions
and
29 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,114 @@ | ||
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKey, SimpleLogger, Type } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/sqlite'; | ||
import { mockLogger } from '../helpers'; | ||
|
||
class Id { | ||
|
||
readonly value: number; | ||
|
||
constructor(value: number) { | ||
this.value = value; | ||
} | ||
|
||
} | ||
|
||
export class IdType extends Type<Id, string> { | ||
|
||
override convertToDatabaseValue(value: any) { | ||
if (value instanceof Id) { | ||
return value.value; | ||
} | ||
|
||
return value; | ||
} | ||
|
||
override convertToJSValue(value: any) { | ||
if (typeof value === 'string') { | ||
const id = Object.create(Id.prototype); | ||
|
||
return Object.assign(id, { | ||
value, | ||
}); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
override compareAsType() { | ||
return 'number'; | ||
} | ||
|
||
override getColumnType() { | ||
return 'integer'; | ||
} | ||
|
||
} | ||
|
||
@Entity() | ||
class ParentEntity { | ||
|
||
@PrimaryKey({ type: IdType, autoincrement: false }) | ||
id!: Id; | ||
|
||
@PrimaryKey({ type: IdType, autoincrement: false }) | ||
id2!: Id; | ||
|
||
@OneToMany({ | ||
entity: () => ChildEntity, | ||
mappedBy: 'parent', | ||
}) | ||
children = new Collection<ChildEntity>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
class ChildEntity { | ||
|
||
@PrimaryKey({ type: IdType, autoincrement: false }) | ||
id!: Id; | ||
|
||
@ManyToOne(() => ParentEntity) | ||
parent!: ParentEntity; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [ParentEntity, ChildEntity], | ||
dbName: ':memory:', | ||
loggerFactory: options => new SimpleLogger(options), | ||
}); | ||
|
||
await orm.schema.createSchema(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(); | ||
}); | ||
|
||
it('should create and persist entity along with child entity', async () => { | ||
const parentRepository = orm.em.fork().getRepository(ParentEntity); | ||
|
||
// Create parent | ||
const parent = new ParentEntity(); | ||
parent.id = new Id(1); | ||
parent.id2 = new Id(2); | ||
|
||
// Create child | ||
const child = new ChildEntity(); | ||
child.id = new Id(1); | ||
|
||
// Add child to parent | ||
parent.children.add(child); | ||
|
||
const mock = mockLogger(orm); | ||
await parentRepository.persistAndFlush(parent); | ||
expect(mock.mock.calls).toEqual([ | ||
['[query] begin'], | ||
['[query] insert into `parent_entity` (`id`, `id2`) values (1, 2) returning `id`, `id2`'], | ||
['[query] insert into `child_entity` (`id`, `parent_id`, `parent_id2`) values (1, 1, 2) returning `id`'], | ||
['[query] commit'], | ||
]); | ||
}); |