-
-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): ignore SQL converter on object embeddables with custom types
Closes #5074
- Loading branch information
Showing
4 changed files
with
125 additions
and
4 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
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,120 @@ | ||
import { Embeddable, Embedded, Entity, MikroORM, PrimaryKey, Property, Type } from '@mikro-orm/sqlite'; | ||
|
||
class GeoPoint { | ||
|
||
constructor( | ||
readonly latitude: number, | ||
readonly longitude: number, | ||
) {} | ||
|
||
} | ||
|
||
class GeoPointType extends Type<GeoPoint | undefined, string | undefined> { | ||
|
||
override convertToDatabaseValue( | ||
value: GeoPoint | undefined, | ||
): string | undefined { | ||
if (!value) { | ||
return value; | ||
} | ||
|
||
return `point(${value.latitude} ${value.longitude})`; | ||
} | ||
|
||
override convertToJSValue(value: string | undefined): GeoPoint | undefined { | ||
const m = value?.match(/point\((-?\d+(\.\d+)?) (-?\d+(\.\d+)?)\)/i); | ||
|
||
if (!m) { | ||
return undefined; | ||
} | ||
|
||
return new GeoPoint(+m[1], +m[3]); | ||
} | ||
|
||
override convertToJSValueSQL(key: string) { | ||
return `ST_AsText(${key})`; | ||
} | ||
|
||
override convertToDatabaseValueSQL(key: string) { | ||
return `ST_PointFromText(${key}, 4326)`; | ||
} | ||
|
||
override getColumnType(): string { | ||
return 'point SRID 4326'; | ||
} | ||
|
||
} | ||
|
||
@Embeddable() | ||
class SnapshotMore { | ||
|
||
@Property({ | ||
type: GeoPointType, | ||
}) | ||
position!: GeoPoint; | ||
|
||
@Property({ | ||
nullable: true, | ||
}) | ||
description?: string; | ||
|
||
} | ||
|
||
@Embeddable() | ||
class Snapshot { | ||
|
||
@Embedded(() => SnapshotMore, { | ||
object: true, | ||
}) | ||
ref!: SnapshotMore; | ||
|
||
} | ||
|
||
@Entity() | ||
class Outer { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Embedded(() => Snapshot, { | ||
object: true, | ||
}) | ||
bigSnapshot!: Snapshot; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: ':memory:', | ||
entities: [Outer, Snapshot], | ||
}); | ||
await orm.schema.createSchema(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test(`GH issue 5074`, async () => { | ||
const snapshotMore = new SnapshotMore(); | ||
snapshotMore.position = new GeoPoint(21, 32); | ||
|
||
const snapshot = new Snapshot(); | ||
snapshot.ref = snapshotMore; | ||
|
||
const outer = new Outer(); | ||
outer.id = 123_456; | ||
outer.bigSnapshot = snapshot; | ||
|
||
await orm.em.persistAndFlush([outer]); | ||
|
||
const result = await orm.em.fork().findOneOrFail(Outer, { | ||
id: outer.id, | ||
}); | ||
expect(result.bigSnapshot.ref.position).toEqual({ | ||
latitude: 21, | ||
longitude: 32, | ||
}); | ||
}); |