-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
37 lines (30 loc) · 948 Bytes
/
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
'use strict';
const mysql = require('serverless-mysql')({
config: {
host : process.env.MYSQL_HOST,
port : process.env.MYSQL_PORT,
database : process.env.MYSQL_DBNAME,
user : process.env.MYSQL_USER,
password : process.env.MYSQL_PASSWORD,
insecureAuth : true
}
});
async function conditionallyCreateUsersTable() {
const createTableSQL = `CREATE TABLE IF NOT EXISTS users (
id VARCHAR(64) NOT NULL,
name VARCHAR(128) NOT NULL,
PRIMARY KEY(id))`;
return await mysql.transaction()
.query(createTableSQL)
.commit();
}
module.exports.initializer = async function(context, callback) {
await conditionallyCreateUsersTable();
callback(null, '');
};
module.exports.handler = async function(event, context, callback) {
let results = await mysql.transaction()
.query('REPLACE INTO users (id, name) VALUES(?, ?)', ['1', 'vangie'])
.commit();
callback(null, results);
}