-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
67 lines (59 loc) · 1.58 KB
/
main.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
console.log(`PostgreSQL GET Function`)
const { Client } = require('pg')
const local = {
user: "postgres",
host: "localhost",
database: "m3_db",
password: "yechezkal",
port: 5432
}
const aws = {
user: "postgres",
host: "apollodb.cxeokcheapqj.us-east-2.rds.amazonaws.com",
database: "apollodb",
password: "yechezkal",
port: 5432
}
let response = {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": 'none',
"isBase64Encoded": false
}
exports.handler = async (event, context, callback) => {
const c = aws // switch to local for localDB
console.log(`aws credentials: ${JSON.stringify(c)}`)
const client = new Client({
user: c.user,
host: c.host,
database: c.database,
password: c.password,
port: c.port
})
try {
await client.connect();
console.log(`DB connected`)
} catch (err) {
console.error(`DB Connect Failed: ${JSON.stringify(err)}`)
response.body = err
callback(null, response)
}
client.query('SELECT NOW()', (err, res) => {
if (err) {
console.log('Database ' + err)
response.body = err
callback(null, response)
client.end()
} else {
response.body = res
console.log(`response: ${JSON.stringify(res)}`)
callback(null, response)
client.end()
}
})
}
if (process.env.USERNAME == 'ysg4206') {
this.handler(null, null, (_, txt) => {console.log(`callback: ${JSON.stringify(txt)}`)})
}