-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
130 lines (118 loc) · 3.21 KB
/
db.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const { Sequelize } = require('sequelize')
const { userData } = require('./userData')
const localHost = {
db: 'm3_db',
user: 'postgres',
host: 'localhost',
pass: 'yechezkal'
}
const awsHost = {
db: 'apollodb',
user: 'postgres',
host: 'apollodb.cxeokcheapqj.us-east-2.rds.amazonaws.com',
pass: 'yechezkal'
}
const azureHost = {
db: 'postdb',
user: 'postgres@postdb',
host: 'postdb.postgres.database.azure.com',
pass: 'Yechezkal1'
}
class DB {
constructor(where) {
this.conn = null
this.db = null
switch (where) {
case 'local':
this.conn = localHost; break;
case 'aws':
this.conn = awsHost; break;
case 'azure':
this.conn = azureHost; break;
}
}
async connect() {
if (this.db !== null) return this.db
else {
await this.open()
return this.db
}
}
async open() {
let host = this.conn
console.log(`Host: ${JSON.stringify(host)}`)
this.db = new Sequelize(host.db, host.user, host.pass, {
host: host.host,
dialect: 'postgres',
logging: false,
pool: {
max: 5,
min: 0,
idle: 20000,
handleDisconnects: true
},
dialectOptions: {
requestTimeout: 100000
},
define: {
freezeTableName: true
}
})
this.User = this.db.define('users', {
firstName: Sequelize.STRING,
lastName: Sequelize.STRING,
addressNumber: Sequelize.INTEGER,
streetName: Sequelize.STRING,
city: Sequelize.STRING,
email: Sequelize.STRING,
})
try {
await this.db.authenticate()
console.log('Connected to DB')
} catch (err) {
console.error('Unable to connect to DB', err)
}
}
async select(id) {
await this.connect()
let who = await this.User.findAll({ where: { id: id } })
await this.close()
return who.get({ plain: true })
}
async findFirst(name) {
await this.connect()
let me = await this.User.findAll({ where: { firstName: name } })
await this.close()
return me[0].get({ plain: true })
}
async addUser(user) {
await this.connect()
let me = await this.User.create(user)
await this.close()
return me.get({ plain: true })
}
async populate() {
await this.connect()
await this.db.sync({ force: true })
try {
await this.User.bulkCreate(userData, { validate: true })
console.log('users created');
} catch (err) {
console.error('failed to create users')
console.error(err)
} finally {
await this.close()
}
}
async findAll() {
await this.connect()
let users = await this.User.findAll({ raw: true })
await this.close()
return users
}
async close() {
await this.db.close()
this.db = null
}
}
exports.DB = new DB('aws')