-
Notifications
You must be signed in to change notification settings - Fork 1
/
common_mysql.js
53 lines (46 loc) · 1.52 KB
/
common_mysql.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
'use strict';
const common = require('./common');
const connectionName = process.env.INSTANCE_CONNECTION_NAME || null;
const dbUser = process.env.SQL_USER || null;
const dbName = process.env.SQL_DB_NAME || null;
if(connectionName === null)
{
throw new Error('env var not defined : INSTANCE_CONNECTION_NAME');
}
if(dbUser === null)
{
throw new Error('env var not defined : SQL_USER' );
}
if( dbName === null)
{
throw new Error('env var not defined : SQL_DB_NAME' );
}
const mysql = require('mysql');
const mysqlConfig = {
connectionLimit : 1,
user : dbUser,
database : dbName,
};
if (process.env.NODE_ENV === 'production') {
mysqlConfig.socketPath = `/cloudsql/${connectionName}`;
}
// Connection pools reuse connections between invocations,
// and handle dropped or expired connections automatically.
let mysqlPool;
//: Promise<T>
async function initMySQL(secretName) {
// Initialize the pool lazily, in case SQL access isn't needed for this
// GCF instance. Doing so minimizes the number of active SQL connections,
// which helps keep your GCF instances under SQL connection limits.
if (!module.exports.mysqlPool)
{
// Access the secret.
mysqlConfig.password = await common.getSecret(secretName);
module.exports.mysqlPool = mysql.createPool(mysqlConfig);
}
return module.exports.mysqlPool;
}
module.exports = {
initMySQL : initMySQL,
mysqlPool : mysqlPool
};