-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
GH5118.test.ts
95 lines (75 loc) · 1.72 KB
/
GH5118.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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);
});