-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): hydrate relations with
mapToPk
as scalars to support cus…
…tom types Closes #4803
- Loading branch information
Showing
2 changed files
with
123 additions
and
10 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,118 @@ | ||
import { EntityProperty, Platform, Type, EntitySchema, Collection, BigIntType } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/sqlite'; | ||
|
||
class User { | ||
|
||
readonly id!: Id; | ||
readonly email!: string; | ||
readonly profiles = new Collection<Profile>(this); | ||
|
||
} | ||
|
||
class Profile { | ||
|
||
readonly id!: string; | ||
readonly user!: Id; | ||
|
||
} | ||
|
||
const profileSchema = new EntitySchema<Profile>({ | ||
class: Profile, | ||
properties: { | ||
id: { | ||
type: BigIntType, | ||
primary: true, | ||
autoincrement: true, | ||
}, | ||
user: { | ||
entity: () => User, | ||
reference: 'm:1', | ||
inversedBy: 'profiles', | ||
mapToPk: true, | ||
}, | ||
}, | ||
}); | ||
|
||
class Id extends Type<Id | undefined, string> { | ||
|
||
readonly value?: bigint; | ||
|
||
constructor(value: bigint | number) { | ||
super(); | ||
if (value) { | ||
this.value = BigInt(value); | ||
} | ||
} | ||
|
||
convertToDatabaseValue(value: Id): string { | ||
return value?.value as unknown as string; | ||
} | ||
|
||
convertToJSValue(value: string): Id | undefined { | ||
return value ? new Id(+value) : undefined; | ||
} | ||
|
||
getColumnType(prop: EntityProperty, platform: Platform) { | ||
return platform.getBigIntTypeDeclarationSQL(prop); | ||
} | ||
|
||
compareAsType(): string { | ||
return 'string'; | ||
} | ||
|
||
} | ||
|
||
const userSchema = new EntitySchema<User>({ | ||
class: User, | ||
properties: { | ||
id: { | ||
type: Id, | ||
primary: true, | ||
autoincrement: true, | ||
}, | ||
email: { | ||
type: 'string', | ||
}, | ||
profiles: { | ||
entity: () => Profile, | ||
reference: '1:m', | ||
mappedBy: 'user', | ||
nullable: true, | ||
}, | ||
}, | ||
}); | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: ':memory:', | ||
entities: [userSchema, profileSchema], | ||
}); | ||
|
||
await orm.schema.createSchema(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(); | ||
}); | ||
|
||
test('A profile user id should be a custom type', async () => { | ||
const em = orm.em.fork(); | ||
const aUser = orm.em.create(User, { | ||
email: 'user@mail.com', | ||
}); | ||
await em.persistAndFlush(aUser); | ||
|
||
const aProfile = orm.em.create(Profile, { | ||
user: aUser.id, | ||
}); | ||
await em.persistAndFlush(aProfile); | ||
|
||
const userProfile = await em.findOneOrFail(Profile, { id: aProfile.id }, { | ||
refresh: true, | ||
}); | ||
|
||
expect(userProfile).toBeTruthy(); | ||
expect(userProfile.user).toBeInstanceOf(Id); | ||
}); |