-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (49 loc) · 1.52 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
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const mysql = require("mysql");
require("dotenv").config();
// Initialize Firebase admin SDK.
// Omit deatiled settings, becasue of same GCP project.
admin.initializeApp();
// Initalize MySQL settings.
const pool = mysql.createPool({
connectionLimit: 10,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
});
// Trigger this function when new document adds in Firestore's collection.
exports.syncFirestoreToMySQL = functions
.region(process.env.GCP_REGION)
.firestore.document(`${process.env.FIRESTORE_COLLECTION}/{docId}`)
.onCreate((snap, context) => {
const newData = snap.data();
// Logging.
console.log(`New Firestore document(${context.params.docId}): `, newData);
// Generate SQL query.
const sql = `
INSERT INTO vr_resource (id, title, type, filePath, groupId)
VALUES (?, ?, ?, ?, ?)
`;
const values = [
context.params.docId,
newData.title,
newData.type,
newData.filePath,
newData.groupId,
];
// Insert data to MySQL.
return new Promise((resolve, reject) => {
pool.query(sql, values, (error, results, fields) => {
if (error) {
console.error(error);
reject(error);
} else {
console.log("Inserted data into MySQL", results);
resolve(results);
}
});
});
});