-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
75 lines (62 loc) · 2.29 KB
/
database.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
/**
* Created by wellington on 07/04/17.
*
*
* May 10, 2017
* Changed the database to return the PG object
*
*/
let logger = require(`${CORE_PATH}/logger`)
class Database
{
async query(sql)
{
console.log(`SQL->${sql}`)
return new Promise(function (resolve, reject)
{
let user = `${process.env.db_user || process.env.USER}`
let pass = `${process.env.db_pass || ''}`
let host = `${process.env.db_host || 'localhost'}`
let base = `${process.env.db_base || ''}`
if(!process.env.db_user) logger.error(`Your '.env' file seem to have no database users '${process.env.USER}' will be take`)
if(!process.env.db_pass) logger.error(`It seems in '.env' file have no password for database user '${user}', blank will be taken as default!`)
if(!process.env.db_host) logger.error(`No hostname was specified in your '.env' file '${host}' will be taken`)
if(!process.env.db_base) logger.error(`It seems you have no database set in your '.env' file blank will be taken`)
let Pool = require('pg').Pool
let config =
{
user:user,
password:pass,
host:host,
database:base,
max:10,
idleTimeoutMillis: 1000,
}
let pool = new Pool(config)
pool.on('error', function(e, client)
{
console.log(e)
console.log(client)
// if a client is idle in the pool
// and receives an error - for example when your PostgreSQL server restarts
// the pool will catch the error & let you handle it here
});
pool.query(sql, function(err, result)
{
if (err)
{
logger.log_query(sql)
logger.error(`You have some error while try to run "${sql}" in your database!`)
resolve(err);
}
else
{
if ( typeof result === undefined ) resolve([])
logger.log_query(sql)
resolve(result.rows)
}
});
})
}
}
export default Database