-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmikro.ts
63 lines (49 loc) · 1.24 KB
/
mikro.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
import { MySqlDriver, Options, MikroORM, Entity, PrimaryKey, Property } from '@mikro-orm/mysql';
import { mySqlConfig } from '../config';
@Entity({
tableName: 'users',
})
class User {
@PrimaryKey()
id!: number;
@Property()
username: string;
@Property()
email: string;
@Property()
password: string;
@Property()
name: string;
@Property()
createdAt: Date = new Date();
@Property({ onUpdate: () => new Date() })
updatedAt: Date = new Date();
@Property({ nullable: true })
deletedAt?: Date;
constructor(username: string, email: string, password: string, name: string) {
this.username = username;
this.email = email;
this.password = password;
this.name = name;
}
}
const options: Options = {
entities: [User],
dbName: mySqlConfig.database,
port: mySqlConfig.port,
host: mySqlConfig.host,
user: mySqlConfig.user,
password: mySqlConfig.password,
pool: {
min: 0,
max: mySqlConfig.connectionLimit,
},
allowGlobalContext: true,
};
if (!mySqlConfig.connectionLimit) {
delete options.pool;
}
const orm = await MikroORM.init<MySqlDriver>(options);
export const mikroMySqlGetUser = async (id: number) =>
await orm.em.findOne(User, { id });
export const mikroClose = () => orm.close();