This repository has been archived by the owner on Jul 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathindex.js
104 lines (91 loc) · 3.42 KB
/
index.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
/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const mysql = require('mysql');
const connection = mysql.createConnection({
host : process.env.ENDPOINT,
user : process.env.USERNAME,
password : process.env.PASSWORD
});
let dbInit = false;
async function conditionallyCreateDB(connection) {
await executeSQL(connection, 'CREATE DATABASE IF NOT EXISTS ' + process.env.DBNAME);
return new Promise((resolve,reject) => {
connection.changeUser({ database: process.env.DBNAME }, (err, data) => {
if (err) {
return reject(err)
}
return resolve(data)
} )
})
}
function conditionallyCreateCommentsTable(connection) {
const createTableSQL = `CREATE TABLE IF NOT EXISTS comments (
id VARCHAR(64) NOT NULL,
author VARCHAR(128) NOT NULL,
postId VARCHAR(64) NOT NULL,
content VARCHAR(255) NOT NULL,
upvotes INT NOT NULL,
downvotes INT NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(postId) REFERENCES posts(id))`;
return executeSQL(connection, createTableSQL);
}
function conditionallyCreatePostsTable(connection) {
const createTableSQL = `CREATE TABLE IF NOT EXISTS posts (
id VARCHAR(64) NOT NULL,
author VARCHAR(64) NOT NULL,
content VARCHAR(2048) NOT NULL,
views INT NOT NULL,
PRIMARY KEY(id))`;
return executeSQL(connection, createTableSQL);
}
function executeSQL(connection, sql) {
console.log('Executing SQL:', sql);
return new Promise((resolve,reject) => {
connection.query(sql, (err, data) => {
if (err) {
return reject(err)
}
return resolve(data)
} )
})
}
function populateAndSanitizeSQL(sql, variableMapping, connection) {
Object.entries(variableMapping).forEach(([key, value]) => {
const escapedValue = connection.escape(value);
sql = sql.replace(key, escapedValue);
});
return sql;
}
exports.handler = async (event) => {
console.log('Received event', JSON.stringify(event, null, 3));
if (!dbInit) {
await conditionallyCreateDB(connection);
await conditionallyCreatePostsTable(connection);
await conditionallyCreateCommentsTable(connection);
dbInit = true;
}
const inputSQL = populateAndSanitizeSQL(event.sql, event.variableMapping, connection);
let result = await executeSQL(connection, inputSQL);
if (event.responseSQL) {
const responseSQL =
populateAndSanitizeSQL(event.responseSQL, event.variableMapping, connection);
result = await executeSQL(connection, responseSQL);
}
console.log(JSON.stringify(result, null, 3));
return result;
};