-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathdatabase.js
More file actions
46 lines (42 loc) · 1.05 KB
/
Copy pathdatabase.js
File metadata and controls
46 lines (42 loc) · 1.05 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
const Sequelize = require("sequelize");
const sequelize = new Sequelize(
process.env.DB_SCHEMA || "postgres",
process.env.DB_USER || "postgres",
process.env.DB_PASSWORD || "123456",
{
host: process.env.DB_HOST || "localhost",
port: process.env.DB_PORT || 5432,
dialect: "postgres",
dialectOptions: {
ssl: process.env.DB_SSL == "true",
},
logging: false, // Disable logging
}
);
sequelize
.authenticate()
.then(() => {
console.log("Connection has been established successfully.");
})
.catch(err => {
console.error("Unable to connect to the database:", err);
});
const Users = sequelize.define("Users", {
name: {
type: Sequelize.STRING,
allowNull: false,
},
age: {
type: Sequelize.INTEGER,
allowNull: true,
},
});
sequelize
.sync({ force: false }) // `force: true` will drop the table if it already exists
.then(() => {
console.log("Database & tables created!");
})
.catch(error => {
console.error("Unable to create tables:", error);
});
module.exports = { sequelize, Users };