-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdb.ts
More file actions
61 lines (53 loc) · 1.48 KB
/
Copy pathdb.ts
File metadata and controls
61 lines (53 loc) · 1.48 KB
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
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
Model,
ModelCtor,
Sequelize,
SequelizeOptions,
} from "sequelize-typescript";
import { Domain } from "./models/domain";
import { Subject } from "./models/subject";
import { Url } from "./models/url";
import { Session } from "./models/session";
import { Worker } from "./models/worker";
// If db is not setup, create connection
if (!(global as any).db) {
const dbModels: ModelCtor<Model<any, any>>[] = [
Domain,
Subject,
Url,
Session,
Worker
];
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const log = (query: string, timing?: number) => {
// console.log(`${query} took ${timing} ms`);
}
let db: Sequelize;
try {
const options: SequelizeOptions = {
host: process.env.POSTGRES_HOST!,
dialect: "postgres",
benchmark: true,
logging: log,
models: dbModels,
// NOTE: Configure to database specifications
pool: {
max: 6,
min: 0,
acquire: 90000
}
};
db = new Sequelize(
process.env.POSTGRES_DB!,
process.env.POSTGRES_USER!,
process.env.POSTGRES_PASSWORD!,
options
);
} catch (err) {
// console.log(err);
db = null!;
}
(global as any).db = db;
}
export const sequelize = (global as any).db;