serverless-simple-middleware 0.0.53 → 0.0.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierignore +2 -2
- package/README.md +4 -4
- package/dist/aws/config.d.ts +15 -15
- package/dist/aws/config.js +29 -29
- package/dist/aws/define.d.ts +21 -21
- package/dist/aws/define.js +8 -8
- package/dist/aws/index.d.ts +3 -3
- package/dist/aws/index.js +8 -8
- package/dist/aws/simple.d.ts +46 -44
- package/dist/aws/simple.js +682 -656
- package/dist/index.d.ts +3 -3
- package/dist/index.js +8 -8
- package/dist/middleware/aws.d.ts +25 -25
- package/dist/middleware/aws.js +128 -128
- package/dist/middleware/base.d.ts +54 -54
- package/dist/middleware/base.js +140 -140
- package/dist/middleware/build.d.ts +3 -3
- package/dist/middleware/build.js +234 -234
- package/dist/middleware/index.d.ts +12 -12
- package/dist/middleware/index.js +22 -22
- package/dist/middleware/logger.d.ts +18 -18
- package/dist/middleware/logger.js +71 -71
- package/dist/middleware/mysql.d.ts +44 -44
- package/dist/middleware/mysql.js +289 -289
- package/dist/middleware/trace.d.ts +86 -86
- package/dist/middleware/trace.js +255 -255
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.js +7 -7
- package/dist/utils/logger.d.ts +26 -26
- package/dist/utils/logger.js +73 -73
- package/dist/utils/misc.d.ts +1 -1
- package/dist/utils/misc.js +9 -9
- package/jest.config.js +7 -7
- package/package.json +61 -61
- package/src/aws/config.ts +46 -46
- package/src/aws/define.ts +29 -29
- package/src/aws/index.ts +3 -3
- package/src/aws/simple.ts +547 -531
- package/src/index.ts +3 -3
- package/src/middleware/aws.ts +78 -78
- package/src/middleware/base.ts +164 -164
- package/src/middleware/build.ts +173 -173
- package/src/middleware/index.ts +20 -20
- package/src/middleware/logger.ts +28 -28
- package/src/middleware/mysql.ts +210 -210
- package/src/middleware/trace.ts +269 -269
- package/src/utils/index.ts +2 -2
- package/src/utils/logger.ts +94 -94
- package/src/utils/misc.ts +11 -11
- package/tsconfig.json +15 -15
- package/tslint.json +12 -12
package/dist/middleware/mysql.js
CHANGED
|
@@ -1,289 +1,289 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
extendStatics(d, b);
|
|
11
|
-
function __() { this.constructor = d; }
|
|
12
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
|
-
};
|
|
14
|
-
})();
|
|
15
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
16
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
17
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
18
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
19
|
-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
20
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
21
|
-
});
|
|
22
|
-
};
|
|
23
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
24
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
25
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
26
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
27
|
-
function step(op) {
|
|
28
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
29
|
-
while (_) try {
|
|
30
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
31
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
32
|
-
switch (op[0]) {
|
|
33
|
-
case 0: case 1: t = op; break;
|
|
34
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
35
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
36
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
37
|
-
default:
|
|
38
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
39
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
40
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
41
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
42
|
-
if (t[2]) _.ops.pop();
|
|
43
|
-
_.trys.pop(); continue;
|
|
44
|
-
}
|
|
45
|
-
op = body.call(thisArg, _);
|
|
46
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
47
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
-
var mysql = require("mysql");
|
|
52
|
-
var utils_1 = require("../utils");
|
|
53
|
-
var base_1 = require("./base");
|
|
54
|
-
var logger = utils_1.getLogger(__filename);
|
|
55
|
-
var ConnectionProxy = /** @class */ (function () {
|
|
56
|
-
function ConnectionProxy(config) {
|
|
57
|
-
var _this = this;
|
|
58
|
-
this.query = function (sql, params) {
|
|
59
|
-
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
60
|
-
var connection;
|
|
61
|
-
return __generator(this, function (_a) {
|
|
62
|
-
switch (_a.label) {
|
|
63
|
-
case 0:
|
|
64
|
-
connection = this.prepareConnection();
|
|
65
|
-
return [4 /*yield*/, this.tryToInitializeSchema(false)];
|
|
66
|
-
case 1:
|
|
67
|
-
_a.sent();
|
|
68
|
-
if (process.env.NODE_ENV !== 'test') {
|
|
69
|
-
logger.silly("Execute query[" + sql + "] with params[" + params + "]");
|
|
70
|
-
}
|
|
71
|
-
connection.query(sql, params, function (err, result) {
|
|
72
|
-
if (err) {
|
|
73
|
-
logger.error("error occurred in database query=" + sql + ", error=" + err);
|
|
74
|
-
reject(err);
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
resolve(result);
|
|
78
|
-
if (process.env.NODE_ENV !== 'test') {
|
|
79
|
-
logger.silly("DB result is " + JSON.stringify(result));
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
return [2 /*return*/];
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}); });
|
|
87
|
-
};
|
|
88
|
-
this.fetch = function (sql, params) {
|
|
89
|
-
return _this.query(sql, params).then(function (res) { return res || []; });
|
|
90
|
-
};
|
|
91
|
-
this.fetchOne = function (sql, params, defaultValue) {
|
|
92
|
-
return _this.fetch(sql, params).then(function (res) {
|
|
93
|
-
if (res === undefined || res[0] === undefined) {
|
|
94
|
-
// Makes it as non-null result.
|
|
95
|
-
return defaultValue || {};
|
|
96
|
-
}
|
|
97
|
-
return res[0];
|
|
98
|
-
});
|
|
99
|
-
};
|
|
100
|
-
this.beginTransaction = function () {
|
|
101
|
-
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
102
|
-
var connection;
|
|
103
|
-
return __generator(this, function (_a) {
|
|
104
|
-
switch (_a.label) {
|
|
105
|
-
case 0:
|
|
106
|
-
connection = this.prepareConnection();
|
|
107
|
-
return [4 /*yield*/, this.tryToInitializeSchema(false)];
|
|
108
|
-
case 1:
|
|
109
|
-
_a.sent();
|
|
110
|
-
connection.beginTransaction(function (err) {
|
|
111
|
-
if (err) {
|
|
112
|
-
reject(err);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
resolve();
|
|
116
|
-
});
|
|
117
|
-
return [2 /*return*/];
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}); });
|
|
121
|
-
};
|
|
122
|
-
this.commit = function () {
|
|
123
|
-
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
124
|
-
var connection;
|
|
125
|
-
return __generator(this, function (_a) {
|
|
126
|
-
switch (_a.label) {
|
|
127
|
-
case 0:
|
|
128
|
-
connection = this.prepareConnection();
|
|
129
|
-
return [4 /*yield*/, this.tryToInitializeSchema(false)];
|
|
130
|
-
case 1:
|
|
131
|
-
_a.sent();
|
|
132
|
-
connection.commit(function (err) {
|
|
133
|
-
if (err) {
|
|
134
|
-
reject(err);
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
resolve();
|
|
138
|
-
});
|
|
139
|
-
return [2 /*return*/];
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
}); });
|
|
143
|
-
};
|
|
144
|
-
this.rollback = function () {
|
|
145
|
-
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
146
|
-
var connection;
|
|
147
|
-
return __generator(this, function (_a) {
|
|
148
|
-
switch (_a.label) {
|
|
149
|
-
case 0:
|
|
150
|
-
connection = this.prepareConnection();
|
|
151
|
-
return [4 /*yield*/, this.tryToInitializeSchema(false)];
|
|
152
|
-
case 1:
|
|
153
|
-
_a.sent();
|
|
154
|
-
connection.rollback(function (err) {
|
|
155
|
-
if (err) {
|
|
156
|
-
reject(err);
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
resolve();
|
|
160
|
-
});
|
|
161
|
-
return [2 /*return*/];
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
}); });
|
|
165
|
-
};
|
|
166
|
-
this.clearConnection = function () {
|
|
167
|
-
if (_this.connection) {
|
|
168
|
-
_this.connection.end();
|
|
169
|
-
_this.connection = undefined;
|
|
170
|
-
logger.verbose('Connection is end');
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
|
-
this.onPluginCreated = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
174
|
-
return [2 /*return*/, this.tryToInitializeSchema(true)];
|
|
175
|
-
}); }); };
|
|
176
|
-
this.prepareConnection = function () {
|
|
177
|
-
if (_this.connection) {
|
|
178
|
-
return _this.connection;
|
|
179
|
-
}
|
|
180
|
-
_this.connection = mysql.createConnection(_this.pluginConfig.config);
|
|
181
|
-
_this.connection.connect();
|
|
182
|
-
return _this.connection;
|
|
183
|
-
};
|
|
184
|
-
this.changeDatabase = function (dbName) {
|
|
185
|
-
return new Promise(function (resolve, reject) {
|
|
186
|
-
return _this.prepareConnection().changeUser({
|
|
187
|
-
database: dbName,
|
|
188
|
-
}, function (err) { return (err ? reject(err) : resolve()); });
|
|
189
|
-
});
|
|
190
|
-
};
|
|
191
|
-
this.tryToInitializeSchema = function (initial) { return __awaiter(_this, void 0, void 0, function () {
|
|
192
|
-
var _a, _b, eager, _c, ignoreError, _d, database, _e, tables, result, _i, _f, _g, name_1, query, result, error_1;
|
|
193
|
-
return __generator(this, function (_h) {
|
|
194
|
-
switch (_h.label) {
|
|
195
|
-
case 0:
|
|
196
|
-
_a = this.pluginConfig.schema || {}, _b = _a.eager, eager = _b === void 0 ? false : _b, _c = _a.ignoreError, ignoreError = _c === void 0 ? false : _c, _d = _a.database, database = _d === void 0 ? '' : _d, _e = _a.tables, tables = _e === void 0 ? {} : _e;
|
|
197
|
-
if (initial && !eager) {
|
|
198
|
-
return [2 /*return*/];
|
|
199
|
-
}
|
|
200
|
-
// This method can be called twice when eager option is on,
|
|
201
|
-
// so this flag should be set and checked at first.
|
|
202
|
-
if (this.initialized) {
|
|
203
|
-
return [2 /*return*/];
|
|
204
|
-
}
|
|
205
|
-
this.initialized = true;
|
|
206
|
-
_h.label = 1;
|
|
207
|
-
case 1:
|
|
208
|
-
_h.trys.push([1, 10, , 11]);
|
|
209
|
-
if (!database) return [3 /*break*/, 3];
|
|
210
|
-
logger.debug("Prepare a database[" + this.dbName + "]");
|
|
211
|
-
logger.stupid(this.dbName, database);
|
|
212
|
-
return [4 /*yield*/, this.query(database)];
|
|
213
|
-
case 2:
|
|
214
|
-
result = _h.sent();
|
|
215
|
-
logger.debug("Database[" + this.dbName + "] is initialized: " + JSON.stringify(result));
|
|
216
|
-
_h.label = 3;
|
|
217
|
-
case 3:
|
|
218
|
-
if (!this.dbName) return [3 /*break*/, 5];
|
|
219
|
-
return [4 /*yield*/, this.changeDatabase(this.dbName)];
|
|
220
|
-
case 4:
|
|
221
|
-
_h.sent();
|
|
222
|
-
logger.verbose("Database[" + this.dbName + "] is connected.");
|
|
223
|
-
_h.label = 5;
|
|
224
|
-
case 5:
|
|
225
|
-
_i = 0, _f = Object.entries(tables);
|
|
226
|
-
_h.label = 6;
|
|
227
|
-
case 6:
|
|
228
|
-
if (!(_i < _f.length)) return [3 /*break*/, 9];
|
|
229
|
-
_g = _f[_i], name_1 = _g[0], query = _g[1];
|
|
230
|
-
logger.debug("Prepare a table[" + name_1 + "]");
|
|
231
|
-
logger.stupid(name_1, query);
|
|
232
|
-
return [4 /*yield*/, this.query(query)];
|
|
233
|
-
case 7:
|
|
234
|
-
result = _h.sent();
|
|
235
|
-
logger.debug("Table[" + name_1 + "] is initialized: " + JSON.stringify(result));
|
|
236
|
-
_h.label = 8;
|
|
237
|
-
case 8:
|
|
238
|
-
_i++;
|
|
239
|
-
return [3 /*break*/, 6];
|
|
240
|
-
case 9:
|
|
241
|
-
logger.verbose("Database schema is initialized.");
|
|
242
|
-
return [3 /*break*/, 11];
|
|
243
|
-
case 10:
|
|
244
|
-
error_1 = _h.sent();
|
|
245
|
-
logger.warn(error_1);
|
|
246
|
-
if (!ignoreError) {
|
|
247
|
-
throw error_1;
|
|
248
|
-
}
|
|
249
|
-
return [3 /*break*/, 11];
|
|
250
|
-
case 11: return [2 /*return*/];
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
}); };
|
|
254
|
-
this.pluginConfig = config;
|
|
255
|
-
if (config.schema && config.schema.database) {
|
|
256
|
-
this.dbName = config.config.database;
|
|
257
|
-
config.config.database = undefined;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
return ConnectionProxy;
|
|
261
|
-
}());
|
|
262
|
-
exports.ConnectionProxy = ConnectionProxy;
|
|
263
|
-
var MySQLPlugin = /** @class */ (function (_super) {
|
|
264
|
-
__extends(MySQLPlugin, _super);
|
|
265
|
-
function MySQLPlugin(options) {
|
|
266
|
-
var _this = _super.call(this) || this;
|
|
267
|
-
_this.create = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
268
|
-
return __generator(this, function (_a) {
|
|
269
|
-
switch (_a.label) {
|
|
270
|
-
case 0: return [4 /*yield*/, this.proxy.onPluginCreated()];
|
|
271
|
-
case 1:
|
|
272
|
-
_a.sent();
|
|
273
|
-
return [2 /*return*/, { db: this.proxy }];
|
|
274
|
-
}
|
|
275
|
-
});
|
|
276
|
-
}); };
|
|
277
|
-
_this.end = function () {
|
|
278
|
-
if (_this.proxy) {
|
|
279
|
-
_this.proxy.clearConnection();
|
|
280
|
-
}
|
|
281
|
-
};
|
|
282
|
-
_this.proxy = new ConnectionProxy(options);
|
|
283
|
-
return _this;
|
|
284
|
-
}
|
|
285
|
-
return MySQLPlugin;
|
|
286
|
-
}(base_1.HandlerPluginBase));
|
|
287
|
-
exports.MySQLPlugin = MySQLPlugin;
|
|
288
|
-
var build = function (options) { return new MySQLPlugin(options); };
|
|
289
|
-
exports.default = build;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
extendStatics(d, b);
|
|
11
|
+
function __() { this.constructor = d; }
|
|
12
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
16
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
17
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
18
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
19
|
+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
20
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
24
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
25
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
26
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
27
|
+
function step(op) {
|
|
28
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
29
|
+
while (_) try {
|
|
30
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
31
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
32
|
+
switch (op[0]) {
|
|
33
|
+
case 0: case 1: t = op; break;
|
|
34
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
35
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
36
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
37
|
+
default:
|
|
38
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
39
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
40
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
41
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
42
|
+
if (t[2]) _.ops.pop();
|
|
43
|
+
_.trys.pop(); continue;
|
|
44
|
+
}
|
|
45
|
+
op = body.call(thisArg, _);
|
|
46
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
47
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
+
var mysql = require("mysql");
|
|
52
|
+
var utils_1 = require("../utils");
|
|
53
|
+
var base_1 = require("./base");
|
|
54
|
+
var logger = utils_1.getLogger(__filename);
|
|
55
|
+
var ConnectionProxy = /** @class */ (function () {
|
|
56
|
+
function ConnectionProxy(config) {
|
|
57
|
+
var _this = this;
|
|
58
|
+
this.query = function (sql, params) {
|
|
59
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
60
|
+
var connection;
|
|
61
|
+
return __generator(this, function (_a) {
|
|
62
|
+
switch (_a.label) {
|
|
63
|
+
case 0:
|
|
64
|
+
connection = this.prepareConnection();
|
|
65
|
+
return [4 /*yield*/, this.tryToInitializeSchema(false)];
|
|
66
|
+
case 1:
|
|
67
|
+
_a.sent();
|
|
68
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
69
|
+
logger.silly("Execute query[" + sql + "] with params[" + params + "]");
|
|
70
|
+
}
|
|
71
|
+
connection.query(sql, params, function (err, result) {
|
|
72
|
+
if (err) {
|
|
73
|
+
logger.error("error occurred in database query=" + sql + ", error=" + err);
|
|
74
|
+
reject(err);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
resolve(result);
|
|
78
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
79
|
+
logger.silly("DB result is " + JSON.stringify(result));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return [2 /*return*/];
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}); });
|
|
87
|
+
};
|
|
88
|
+
this.fetch = function (sql, params) {
|
|
89
|
+
return _this.query(sql, params).then(function (res) { return res || []; });
|
|
90
|
+
};
|
|
91
|
+
this.fetchOne = function (sql, params, defaultValue) {
|
|
92
|
+
return _this.fetch(sql, params).then(function (res) {
|
|
93
|
+
if (res === undefined || res[0] === undefined) {
|
|
94
|
+
// Makes it as non-null result.
|
|
95
|
+
return defaultValue || {};
|
|
96
|
+
}
|
|
97
|
+
return res[0];
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
this.beginTransaction = function () {
|
|
101
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
102
|
+
var connection;
|
|
103
|
+
return __generator(this, function (_a) {
|
|
104
|
+
switch (_a.label) {
|
|
105
|
+
case 0:
|
|
106
|
+
connection = this.prepareConnection();
|
|
107
|
+
return [4 /*yield*/, this.tryToInitializeSchema(false)];
|
|
108
|
+
case 1:
|
|
109
|
+
_a.sent();
|
|
110
|
+
connection.beginTransaction(function (err) {
|
|
111
|
+
if (err) {
|
|
112
|
+
reject(err);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
resolve();
|
|
116
|
+
});
|
|
117
|
+
return [2 /*return*/];
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}); });
|
|
121
|
+
};
|
|
122
|
+
this.commit = function () {
|
|
123
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
124
|
+
var connection;
|
|
125
|
+
return __generator(this, function (_a) {
|
|
126
|
+
switch (_a.label) {
|
|
127
|
+
case 0:
|
|
128
|
+
connection = this.prepareConnection();
|
|
129
|
+
return [4 /*yield*/, this.tryToInitializeSchema(false)];
|
|
130
|
+
case 1:
|
|
131
|
+
_a.sent();
|
|
132
|
+
connection.commit(function (err) {
|
|
133
|
+
if (err) {
|
|
134
|
+
reject(err);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
resolve();
|
|
138
|
+
});
|
|
139
|
+
return [2 /*return*/];
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}); });
|
|
143
|
+
};
|
|
144
|
+
this.rollback = function () {
|
|
145
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
146
|
+
var connection;
|
|
147
|
+
return __generator(this, function (_a) {
|
|
148
|
+
switch (_a.label) {
|
|
149
|
+
case 0:
|
|
150
|
+
connection = this.prepareConnection();
|
|
151
|
+
return [4 /*yield*/, this.tryToInitializeSchema(false)];
|
|
152
|
+
case 1:
|
|
153
|
+
_a.sent();
|
|
154
|
+
connection.rollback(function (err) {
|
|
155
|
+
if (err) {
|
|
156
|
+
reject(err);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
resolve();
|
|
160
|
+
});
|
|
161
|
+
return [2 /*return*/];
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}); });
|
|
165
|
+
};
|
|
166
|
+
this.clearConnection = function () {
|
|
167
|
+
if (_this.connection) {
|
|
168
|
+
_this.connection.end();
|
|
169
|
+
_this.connection = undefined;
|
|
170
|
+
logger.verbose('Connection is end');
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
this.onPluginCreated = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
174
|
+
return [2 /*return*/, this.tryToInitializeSchema(true)];
|
|
175
|
+
}); }); };
|
|
176
|
+
this.prepareConnection = function () {
|
|
177
|
+
if (_this.connection) {
|
|
178
|
+
return _this.connection;
|
|
179
|
+
}
|
|
180
|
+
_this.connection = mysql.createConnection(_this.pluginConfig.config);
|
|
181
|
+
_this.connection.connect();
|
|
182
|
+
return _this.connection;
|
|
183
|
+
};
|
|
184
|
+
this.changeDatabase = function (dbName) {
|
|
185
|
+
return new Promise(function (resolve, reject) {
|
|
186
|
+
return _this.prepareConnection().changeUser({
|
|
187
|
+
database: dbName,
|
|
188
|
+
}, function (err) { return (err ? reject(err) : resolve()); });
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
this.tryToInitializeSchema = function (initial) { return __awaiter(_this, void 0, void 0, function () {
|
|
192
|
+
var _a, _b, eager, _c, ignoreError, _d, database, _e, tables, result, _i, _f, _g, name_1, query, result, error_1;
|
|
193
|
+
return __generator(this, function (_h) {
|
|
194
|
+
switch (_h.label) {
|
|
195
|
+
case 0:
|
|
196
|
+
_a = this.pluginConfig.schema || {}, _b = _a.eager, eager = _b === void 0 ? false : _b, _c = _a.ignoreError, ignoreError = _c === void 0 ? false : _c, _d = _a.database, database = _d === void 0 ? '' : _d, _e = _a.tables, tables = _e === void 0 ? {} : _e;
|
|
197
|
+
if (initial && !eager) {
|
|
198
|
+
return [2 /*return*/];
|
|
199
|
+
}
|
|
200
|
+
// This method can be called twice when eager option is on,
|
|
201
|
+
// so this flag should be set and checked at first.
|
|
202
|
+
if (this.initialized) {
|
|
203
|
+
return [2 /*return*/];
|
|
204
|
+
}
|
|
205
|
+
this.initialized = true;
|
|
206
|
+
_h.label = 1;
|
|
207
|
+
case 1:
|
|
208
|
+
_h.trys.push([1, 10, , 11]);
|
|
209
|
+
if (!database) return [3 /*break*/, 3];
|
|
210
|
+
logger.debug("Prepare a database[" + this.dbName + "]");
|
|
211
|
+
logger.stupid(this.dbName, database);
|
|
212
|
+
return [4 /*yield*/, this.query(database)];
|
|
213
|
+
case 2:
|
|
214
|
+
result = _h.sent();
|
|
215
|
+
logger.debug("Database[" + this.dbName + "] is initialized: " + JSON.stringify(result));
|
|
216
|
+
_h.label = 3;
|
|
217
|
+
case 3:
|
|
218
|
+
if (!this.dbName) return [3 /*break*/, 5];
|
|
219
|
+
return [4 /*yield*/, this.changeDatabase(this.dbName)];
|
|
220
|
+
case 4:
|
|
221
|
+
_h.sent();
|
|
222
|
+
logger.verbose("Database[" + this.dbName + "] is connected.");
|
|
223
|
+
_h.label = 5;
|
|
224
|
+
case 5:
|
|
225
|
+
_i = 0, _f = Object.entries(tables);
|
|
226
|
+
_h.label = 6;
|
|
227
|
+
case 6:
|
|
228
|
+
if (!(_i < _f.length)) return [3 /*break*/, 9];
|
|
229
|
+
_g = _f[_i], name_1 = _g[0], query = _g[1];
|
|
230
|
+
logger.debug("Prepare a table[" + name_1 + "]");
|
|
231
|
+
logger.stupid(name_1, query);
|
|
232
|
+
return [4 /*yield*/, this.query(query)];
|
|
233
|
+
case 7:
|
|
234
|
+
result = _h.sent();
|
|
235
|
+
logger.debug("Table[" + name_1 + "] is initialized: " + JSON.stringify(result));
|
|
236
|
+
_h.label = 8;
|
|
237
|
+
case 8:
|
|
238
|
+
_i++;
|
|
239
|
+
return [3 /*break*/, 6];
|
|
240
|
+
case 9:
|
|
241
|
+
logger.verbose("Database schema is initialized.");
|
|
242
|
+
return [3 /*break*/, 11];
|
|
243
|
+
case 10:
|
|
244
|
+
error_1 = _h.sent();
|
|
245
|
+
logger.warn(error_1);
|
|
246
|
+
if (!ignoreError) {
|
|
247
|
+
throw error_1;
|
|
248
|
+
}
|
|
249
|
+
return [3 /*break*/, 11];
|
|
250
|
+
case 11: return [2 /*return*/];
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}); };
|
|
254
|
+
this.pluginConfig = config;
|
|
255
|
+
if (config.schema && config.schema.database) {
|
|
256
|
+
this.dbName = config.config.database;
|
|
257
|
+
config.config.database = undefined;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return ConnectionProxy;
|
|
261
|
+
}());
|
|
262
|
+
exports.ConnectionProxy = ConnectionProxy;
|
|
263
|
+
var MySQLPlugin = /** @class */ (function (_super) {
|
|
264
|
+
__extends(MySQLPlugin, _super);
|
|
265
|
+
function MySQLPlugin(options) {
|
|
266
|
+
var _this = _super.call(this) || this;
|
|
267
|
+
_this.create = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
268
|
+
return __generator(this, function (_a) {
|
|
269
|
+
switch (_a.label) {
|
|
270
|
+
case 0: return [4 /*yield*/, this.proxy.onPluginCreated()];
|
|
271
|
+
case 1:
|
|
272
|
+
_a.sent();
|
|
273
|
+
return [2 /*return*/, { db: this.proxy }];
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}); };
|
|
277
|
+
_this.end = function () {
|
|
278
|
+
if (_this.proxy) {
|
|
279
|
+
_this.proxy.clearConnection();
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
_this.proxy = new ConnectionProxy(options);
|
|
283
|
+
return _this;
|
|
284
|
+
}
|
|
285
|
+
return MySQLPlugin;
|
|
286
|
+
}(base_1.HandlerPluginBase));
|
|
287
|
+
exports.MySQLPlugin = MySQLPlugin;
|
|
288
|
+
var build = function (options) { return new MySQLPlugin(options); };
|
|
289
|
+
exports.default = build;
|