-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.ts
37 lines (30 loc) · 872 Bytes
/
db.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
import { Sequelize, SequelizeOptions } from 'sequelize-typescript';
import * as models from './models'
const config = {
DB_URL: 'localhost',
DB_USER: 'postgres',
DB_PASS: 'postgres',
DB_PORT: 5432,
DB_NAME: 'scratch'
}
const dbConfig: SequelizeOptions = {
dialect: 'postgres',
host: config.DB_URL,
username: config.DB_USER,
password: config.DB_PASS,
port: config.DB_PORT,
database: config.DB_NAME,
logging: false,
models: Object.values(models),
};
const sequelize = new Sequelize(dbConfig);
const initialize = async () => {
try {
await sequelize.authenticate();
await sequelize.sync({ alter: true });
console.log("All models were synchronized successfully.");
} catch (error) {
console.error(`Error initializing database: ${error}`)
}
}
export { initialize, sequelize as client }