-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmikro.ts
63 lines (49 loc) · 1.26 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 { PostgreSqlDriver, Options, MikroORM, Entity, PrimaryKey, Property } from '@mikro-orm/postgresql';
import { postgresConfig } 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: postgresConfig.database,
port: postgresConfig.port,
host: postgresConfig.host,
user: postgresConfig.user,
password: postgresConfig.password,
pool: {
min: 0,
max: postgresConfig.max,
},
allowGlobalContext: true,
};
if (!postgresConfig.max) {
delete options.pool;
}
const orm = await MikroORM.init<PostgreSqlDriver>(options);
export const mikroPostgresGetUser = async (id: number) =>
await orm.em.findOne(User, { id });
export const mikroClose = () => orm.close();