This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.js
121 lines (91 loc) · 3.91 KB
/
create.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Database Cache
const db = {};
// Module
module.exports = function (mysql, proxyType, databases, cfg) {
return new Promise(function (resolve, reject) {
// Modules
const util = require('util');
const clone = require('clone');
const objType = require('@tinypudding/puddy-lib/get/objType');
const _ = require('lodash');
let databaseList = [];
// Create Settings
const tinyCfg = _.defaultsDeep({}, cfg, {
charset: "utf8mb4"
});
// Convert
if (!Array.isArray(databases)) {
databaseList = [clone(databases)];
} else {
databaseList = clone(databases);
}
// Run Array
for (const item in databaseList) {
// Exist
if (objType(databaseList[item], 'object') && objType(databaseList[item].data, 'object')) {
if (!db[databaseList[item].data.database]) {
// Charset
if (typeof databaseList[item].data.charset !== "string") {
databaseList[item].data.charset = tinyCfg.charset;
}
// Firebase Is Emulator
let firebaseIsEmulator = false;
if (proxyType === "firebase") {
try {
firebaseIsEmulator = require('@tinypudding/firebase-lib/isEmulator')();
} catch (err) {
firebaseIsEmulator = false;
}
}
// Exist Proxy
if (!firebaseIsEmulator && typeof proxyType === "string" && proxyType !== "default") {
// Google Cloud
if (
// Validate Type
(
proxyType === "firebase" ||
proxyType === "google_cloud"
) &&
// Var
objType(databaseList[item].google_cloud, 'object') &&
typeof databaseList[item].google_cloud.socketPath === "string"
) {
// Insert the config
databaseList[item].data.socketPath = databaseList[item].google_cloud.socketPath;
}
// Nothing
else { reject(new Error('Invalid Proxy Type! Index:' + item)); return; }
}
// Normal Connection
else {
// SSL Files
if (objType(databaseList[item].default.ssl, 'object')) { databaseList[item].data.ssl = databaseList[item].default.ssl; }
// Host and Port
databaseList[item].data.host = databaseList[item].default.host;
databaseList[item].data.port = databaseList[item].default.port;
}
// Create DB Connection
// Complete
try {
db[databaseList[item].data.database] = mysql.createPool(databaseList[item].data);
db[databaseList[item].data.database].query = util.promisify(db[databaseList[item].data.database].query);
}
// Error
catch (err) {
reject(err);
return;
}
}
}
// Nope
else { reject(new Error('Invalid MySQL Setting! Index:' + item)); return; }
}
// Fix
const dbKets = Object.keys(databaseList);
// Complete
if (dbKets.length < 2) {
resolve(db[databaseList[0].data.database]);
} else { resolve(db); }
return;
});
};