-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.ts
30 lines (27 loc) · 902 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
import { Sequelize } from "sequelize";
import dotenv from "dotenv";
dotenv.config();
const connection = `postgres://${process.env.DATABASE_USER_NAME}:${process.env.DATABASE_PASSWORD}@${process.env.DATABASE_HOST}/${process.env.DATABASE_NAME}?sslmode=verify-full`;
const sequelize = new Sequelize(connection, {
dialectOptions: {
ssl: {
rejectUnauthorized: false, // very important for planet scale to work
},
},
});
const makeConnectionWithDB = async () => {
try {
await sequelize.authenticate();
// if (process.env.NODE_ENV === "development") {
await sequelize.sync({
force: true,
logging: (sql) => console.log(sql),
});
// }
console.log("Connection has been established successfully.");
} catch (error) {
console.error("Unable to connect to the database:", error);
process.exit(1);
}
};
export { sequelize, makeConnectionWithDB };