-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·56 lines (48 loc) · 1.65 KB
/
index.js
File metadata and controls
executable file
·56 lines (48 loc) · 1.65 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
47
48
49
50
51
52
53
54
55
56
var mysql = require('mysql');
exports.handler = (event, context, callback) => {
var connection = mysql.createConnection({
host: 'advertising.cjcgw9t4ox6y.us-west-1.rds.amazonaws.com',
user: 'dba',
password: 'doritos24+',
database: 'Advertising'
});
//Check if connection is OK
connection.connect(function(error) {
if (error) {
console.log('ERROR - Could not connect to the database: ' + error.message);
//Internal server error, return 500
callback(JSON.stringify({
"status": 500,
"errorMessage": error.message
}));
}
});
var query = 'SELECT * FROM advertisers';
connection.query(query, function(error, results, fields) {
//Close connection
connection.end();
if (error) {
console.log('ERROR - Problem with provided query: ' + error.message);
//Internal server error, return 500
callback(JSON.stringify({
"status": 500,
"errorMessage": error.message
}));
}
//Successful query
if (results.length == 0) {
//No records, return 404
console.log('ERROR - No records found');
callback(JSON.stringify({
"status": 404,
"errorMessage": "No advertisers found"
}));
}
//Found records
console.log('OK - Successful query: "' + query + '" with results: ' + results);
callback(null, {
"status": 200,
"response": results
});
});
};