Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): provide mapped custom types into constructor with `forceE…
…ntityConstructor` Closes #5118
- Loading branch information
Showing
3 changed files
with
109 additions
and
2 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,95 @@ | ||
import { Entity, MikroORM, PrimaryKey, Property, Type } from '@mikro-orm/sqlite'; | ||
|
||
class Value { | ||
|
||
protected readonly value: string; | ||
|
||
toString() { | ||
return this.value; | ||
} | ||
|
||
constructor(id: string) { | ||
this.value = id; | ||
} | ||
|
||
} | ||
|
||
class SimpleType extends Type< | ||
Value | undefined, | ||
string | undefined | ||
> { | ||
|
||
constructor(private classRef: new (value: any) => Value) { | ||
super(); | ||
} | ||
|
||
convertToDatabaseValue( | ||
value: Value | string | null | undefined, | ||
): string | undefined { | ||
if (!value) { | ||
return undefined; | ||
} | ||
if (typeof value === 'string') { | ||
return value; | ||
} | ||
return value.toString(); | ||
} | ||
|
||
convertToJSValue( | ||
value: Value | string | undefined, | ||
): Value | undefined { | ||
if (!value) { | ||
return undefined; | ||
} | ||
return new this.classRef( | ||
typeof value === 'object' ? value.toString() : value, | ||
); | ||
} | ||
|
||
} | ||
|
||
@Entity() | ||
class File { | ||
|
||
@PrimaryKey({ type: new SimpleType(Value) }) | ||
readonly id: Value; | ||
|
||
@Property({ type: new SimpleType(Value) }) | ||
readonly uri: Value; | ||
|
||
constructor({ id, uri }: { id: Value; uri: Value }) { | ||
this.id = id; | ||
this.uri = uri; | ||
} | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
forceEntityConstructor: true, | ||
entities: [File], | ||
dbName: ':memory:', | ||
}); | ||
await orm.schema.createSchema(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test(`custom types and forceEntityConstructor`, async () => { | ||
await orm.em.fork().persistAndFlush( | ||
new File({ | ||
id: new Value('foo'), | ||
uri: new Value('bar'), | ||
}), | ||
); | ||
|
||
const retrieved = await orm.em.findOneOrFail(File, { | ||
id: new Value('foo'), | ||
}); | ||
expect(retrieved.id).toBeInstanceOf(Value); | ||
expect(retrieved.uri).toBeInstanceOf(Value); | ||
}); |