@rdyl/node-mysql 0.0.2 → 0.0.4
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/CHANGELOG.md +1 -1
- package/dist/helper.js +519 -0
- package/dist/index.js +11 -1
- package/dist/interface.js +23 -0
- package/dist/meta.js +180 -92
- package/dist/utils.js +42 -15
- package/package.json +3 -3
- package/types/helper.d.ts +79 -0
- package/types/index.d.ts +6 -3
- package/types/interface.d.ts +40 -0
- package/types/meta.d.ts +22 -41
- package/types/utils.d.ts +7 -4
- package/dist/selector.js +0 -175
- package/types/selector.d.ts +0 -37
package/CHANGELOG.md
CHANGED
package/dist/helper.js
ADDED
|
@@ -0,0 +1,519 @@
|
|
|
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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
38
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
39
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
40
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
41
|
+
function step(op) {
|
|
42
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
43
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
44
|
+
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;
|
|
45
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
46
|
+
switch (op[0]) {
|
|
47
|
+
case 0: case 1: t = op; break;
|
|
48
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
49
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
50
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
51
|
+
default:
|
|
52
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
53
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
54
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
55
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
56
|
+
if (t[2]) _.ops.pop();
|
|
57
|
+
_.trys.pop(); continue;
|
|
58
|
+
}
|
|
59
|
+
op = body.call(thisArg, _);
|
|
60
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
61
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
65
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
66
|
+
if (ar || !(i in from)) {
|
|
67
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
68
|
+
ar[i] = from[i];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
72
|
+
};
|
|
73
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
74
|
+
exports.MysqlUpdater = exports.MysqlDeleter = exports.MysqlInserter = exports.MysqlSelector = void 0;
|
|
75
|
+
var interface_1 = require("./interface");
|
|
76
|
+
var utils_1 = require("./utils");
|
|
77
|
+
var BaseHelper = /** @class */ (function () {
|
|
78
|
+
function BaseHelper(_tableName) {
|
|
79
|
+
this._tableName = _tableName;
|
|
80
|
+
this.selects = [];
|
|
81
|
+
this.options = {
|
|
82
|
+
limit: 0,
|
|
83
|
+
skip: 0,
|
|
84
|
+
sort: {},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
Object.defineProperty(BaseHelper.prototype, "selectStat", {
|
|
88
|
+
get: function () {
|
|
89
|
+
var sql = '';
|
|
90
|
+
var selects = this.selects;
|
|
91
|
+
var values = [];
|
|
92
|
+
if (selects.length > 0) {
|
|
93
|
+
sql += ' WHERE ';
|
|
94
|
+
selects.forEach(function (ele, i) {
|
|
95
|
+
var field = ele.field, value = ele.value, _a = ele.condition, condition = _a === void 0 ? 'eq' : _a, _b = ele.relation, relation = _b === void 0 ? 'and' : _b;
|
|
96
|
+
var res = "".concat(utils_1.Parser.toDbName(field), " ").concat(interface_1.ConditionMaps[condition], " ");
|
|
97
|
+
if (condition === 'include') {
|
|
98
|
+
res += '(?)';
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
res += '?';
|
|
102
|
+
}
|
|
103
|
+
if (i > 0) {
|
|
104
|
+
sql += " ".concat(relation, " ").concat(res);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
sql += " ".concat(res);
|
|
108
|
+
}
|
|
109
|
+
values.push(value);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
sql: sql,
|
|
114
|
+
values: values,
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
enumerable: false,
|
|
118
|
+
configurable: true
|
|
119
|
+
});
|
|
120
|
+
Object.defineProperty(BaseHelper.prototype, "optionStat", {
|
|
121
|
+
get: function () {
|
|
122
|
+
var _a = this.options, _b = _a.limit, limit = _b === void 0 ? 0 : _b, _c = _a.skip, skip = _c === void 0 ? 0 : _c, sort = _a.sort;
|
|
123
|
+
var sql = '';
|
|
124
|
+
if (Object.keys(sort).length > 0) {
|
|
125
|
+
var items_1 = [];
|
|
126
|
+
Object.keys(sort).forEach(function (name) {
|
|
127
|
+
items_1.push("".concat(utils_1.Parser.toDbName(name), " ").concat(interface_1.SortMaps[sort[name]]));
|
|
128
|
+
});
|
|
129
|
+
sql += "ORDER BY ".concat(items_1.join('), '));
|
|
130
|
+
}
|
|
131
|
+
if (limit > 0) {
|
|
132
|
+
sql += "\nLIMIT ".concat(limit, " OFFSET ").concat(skip);
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
sql: sql ? "\n".concat(sql) : sql,
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
enumerable: false,
|
|
139
|
+
configurable: true
|
|
140
|
+
});
|
|
141
|
+
BaseHelper.prototype.select = function (opts) {
|
|
142
|
+
this.selects.push(opts);
|
|
143
|
+
return this;
|
|
144
|
+
};
|
|
145
|
+
BaseHelper.prototype.limit = function (n) {
|
|
146
|
+
this.options.limit = n;
|
|
147
|
+
return this;
|
|
148
|
+
};
|
|
149
|
+
BaseHelper.prototype.skip = function (n) {
|
|
150
|
+
this.options.skip = n;
|
|
151
|
+
return this;
|
|
152
|
+
};
|
|
153
|
+
BaseHelper.prototype.sort = function (field, v) {
|
|
154
|
+
this.options.sort[field] = v;
|
|
155
|
+
return this;
|
|
156
|
+
};
|
|
157
|
+
return BaseHelper;
|
|
158
|
+
}());
|
|
159
|
+
/** @description 查询数据 */
|
|
160
|
+
var MysqlSelector = /** @class */ (function (_super) {
|
|
161
|
+
__extends(MysqlSelector, _super);
|
|
162
|
+
function MysqlSelector(t, exFields) {
|
|
163
|
+
if (exFields === void 0) { exFields = []; }
|
|
164
|
+
var _this = _super.call(this, t) || this;
|
|
165
|
+
_this.exFields = exFields;
|
|
166
|
+
return _this;
|
|
167
|
+
}
|
|
168
|
+
Object.defineProperty(MysqlSelector.prototype, "sql", {
|
|
169
|
+
get: function () {
|
|
170
|
+
var _a = this, selectStat = _a.selectStat, optionStat = _a.optionStat;
|
|
171
|
+
var target = "SELECT * FROM ".concat(this._tableName);
|
|
172
|
+
if (selectStat.sql) {
|
|
173
|
+
target += selectStat.sql;
|
|
174
|
+
}
|
|
175
|
+
if (optionStat.sql) {
|
|
176
|
+
target += optionStat.sql;
|
|
177
|
+
}
|
|
178
|
+
return target + ';';
|
|
179
|
+
},
|
|
180
|
+
enumerable: false,
|
|
181
|
+
configurable: true
|
|
182
|
+
});
|
|
183
|
+
Object.defineProperty(MysqlSelector.prototype, "countSql", {
|
|
184
|
+
get: function () {
|
|
185
|
+
var selectStat = this.selectStat;
|
|
186
|
+
var target = "SELECT COUNT(*) AS count FROM users ".concat(this._tableName);
|
|
187
|
+
if (selectStat.sql) {
|
|
188
|
+
target += selectStat.sql;
|
|
189
|
+
}
|
|
190
|
+
return target + ';';
|
|
191
|
+
},
|
|
192
|
+
enumerable: false,
|
|
193
|
+
configurable: true
|
|
194
|
+
});
|
|
195
|
+
MysqlSelector.prototype.total = function () {
|
|
196
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
197
|
+
var _a, selectStat, countSql, res, _b;
|
|
198
|
+
return __generator(this, function (_c) {
|
|
199
|
+
switch (_c.label) {
|
|
200
|
+
case 0:
|
|
201
|
+
_c.trys.push([0, 2, , 3]);
|
|
202
|
+
_a = this, selectStat = _a.selectStat, countSql = _a.countSql;
|
|
203
|
+
return [4 /*yield*/, global.mysqlPool.execute(countSql, selectStat.values)];
|
|
204
|
+
case 1:
|
|
205
|
+
res = (_c.sent())[0];
|
|
206
|
+
//@ts-ignore
|
|
207
|
+
return [2 /*return*/, res[0].count];
|
|
208
|
+
case 2:
|
|
209
|
+
_b = _c.sent();
|
|
210
|
+
return [2 /*return*/, 0];
|
|
211
|
+
case 3: return [2 /*return*/];
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
};
|
|
216
|
+
/** @查询结果 */
|
|
217
|
+
MysqlSelector.prototype.result = function () {
|
|
218
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
219
|
+
var _a, selectStat, sql, res, exFields_1, get, err_1;
|
|
220
|
+
return __generator(this, function (_b) {
|
|
221
|
+
switch (_b.label) {
|
|
222
|
+
case 0:
|
|
223
|
+
_b.trys.push([0, 2, , 3]);
|
|
224
|
+
_a = this, selectStat = _a.selectStat, sql = _a.sql;
|
|
225
|
+
return [4 /*yield*/, global.mysqlPool.execute(sql, selectStat.values)];
|
|
226
|
+
case 1:
|
|
227
|
+
res = (_b.sent())[0];
|
|
228
|
+
console.log(res);
|
|
229
|
+
exFields_1 = this.exFields;
|
|
230
|
+
get = function (obj) {
|
|
231
|
+
return Object.fromEntries(
|
|
232
|
+
// @ts-ignore
|
|
233
|
+
Object.keys(obj)
|
|
234
|
+
.filter(function (k) { return !exFields_1.includes(utils_1.Parser.toFieldName(k)); })
|
|
235
|
+
// @ts-ignore
|
|
236
|
+
.map(function (k) { return [utils_1.Parser.toFieldName(k), obj[k]]; }));
|
|
237
|
+
};
|
|
238
|
+
if (Array.isArray(res)) {
|
|
239
|
+
return [2 /*return*/, res.map(get)];
|
|
240
|
+
}
|
|
241
|
+
return [2 /*return*/, [get(res)]];
|
|
242
|
+
case 2:
|
|
243
|
+
err_1 = _b.sent();
|
|
244
|
+
console.log(err_1);
|
|
245
|
+
return [2 /*return*/, []];
|
|
246
|
+
case 3: return [2 /*return*/];
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
};
|
|
251
|
+
/** @根据id查询 */
|
|
252
|
+
MysqlSelector.prototype.getById = function (value) {
|
|
253
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
254
|
+
var list;
|
|
255
|
+
return __generator(this, function (_a) {
|
|
256
|
+
switch (_a.label) {
|
|
257
|
+
case 0:
|
|
258
|
+
this.select({
|
|
259
|
+
field: 'id',
|
|
260
|
+
value: value,
|
|
261
|
+
});
|
|
262
|
+
return [4 /*yield*/, this.result()];
|
|
263
|
+
case 1:
|
|
264
|
+
list = _a.sent();
|
|
265
|
+
if (list.length) {
|
|
266
|
+
return [2 /*return*/, list[0]];
|
|
267
|
+
}
|
|
268
|
+
return [2 /*return*/, null];
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
};
|
|
273
|
+
MysqlSelector.prototype.paged = function (param) {
|
|
274
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
275
|
+
var page, pageSize, records, total;
|
|
276
|
+
return __generator(this, function (_a) {
|
|
277
|
+
switch (_a.label) {
|
|
278
|
+
case 0:
|
|
279
|
+
page = param.page, pageSize = param.pageSize;
|
|
280
|
+
this.skip((page - 1) * pageSize);
|
|
281
|
+
this.limit(pageSize);
|
|
282
|
+
return [4 /*yield*/, this.result()];
|
|
283
|
+
case 1:
|
|
284
|
+
records = _a.sent();
|
|
285
|
+
return [4 /*yield*/, this.total()];
|
|
286
|
+
case 2:
|
|
287
|
+
total = _a.sent();
|
|
288
|
+
return [2 /*return*/, {
|
|
289
|
+
page: page,
|
|
290
|
+
pageSize: pageSize,
|
|
291
|
+
records: records,
|
|
292
|
+
total: total,
|
|
293
|
+
}];
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
};
|
|
298
|
+
return MysqlSelector;
|
|
299
|
+
}(BaseHelper));
|
|
300
|
+
exports.MysqlSelector = MysqlSelector;
|
|
301
|
+
/** @description 插入数据 */
|
|
302
|
+
var MysqlInserter = /** @class */ (function (_super) {
|
|
303
|
+
__extends(MysqlInserter, _super);
|
|
304
|
+
function MysqlInserter(t, fields) {
|
|
305
|
+
if (fields === void 0) { fields = []; }
|
|
306
|
+
var _this = _super.call(this, t) || this;
|
|
307
|
+
_this.values = [];
|
|
308
|
+
_this.fields = fields.filter(function (e) { return e !== 'id'; });
|
|
309
|
+
return _this;
|
|
310
|
+
}
|
|
311
|
+
Object.defineProperty(MysqlInserter.prototype, "sql", {
|
|
312
|
+
get: function () {
|
|
313
|
+
return "INSERT INTO ".concat(this._tableName, " (").concat(this.fields
|
|
314
|
+
.map(utils_1.Parser.toDbName)
|
|
315
|
+
.join(', '), ") \nVALUES (").concat(this.fields.map(function () { return '?'; }).join(', '), ")");
|
|
316
|
+
},
|
|
317
|
+
enumerable: false,
|
|
318
|
+
configurable: true
|
|
319
|
+
});
|
|
320
|
+
MysqlInserter.prototype.push = function () {
|
|
321
|
+
var _a;
|
|
322
|
+
var _this = this;
|
|
323
|
+
var arr = [];
|
|
324
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
325
|
+
arr[_i] = arguments[_i];
|
|
326
|
+
}
|
|
327
|
+
var list = arr.map(function (obj) { return _this.fields.map(function (k) { return obj[k]; }); });
|
|
328
|
+
(_a = this.values).push.apply(_a, list);
|
|
329
|
+
};
|
|
330
|
+
// 插入数据
|
|
331
|
+
MysqlInserter.prototype.result = function () {
|
|
332
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
333
|
+
var conn, result, stmt, _i, _a, value, row, _b;
|
|
334
|
+
return __generator(this, function (_c) {
|
|
335
|
+
switch (_c.label) {
|
|
336
|
+
case 0: return [4 /*yield*/, global.mysqlPool.getConnection()];
|
|
337
|
+
case 1:
|
|
338
|
+
conn = _c.sent();
|
|
339
|
+
result = [];
|
|
340
|
+
return [4 /*yield*/, conn.prepare(this.sql)];
|
|
341
|
+
case 2:
|
|
342
|
+
stmt = _c.sent();
|
|
343
|
+
_i = 0, _a = this.values;
|
|
344
|
+
_c.label = 3;
|
|
345
|
+
case 3:
|
|
346
|
+
if (!(_i < _a.length)) return [3 /*break*/, 8];
|
|
347
|
+
value = _a[_i];
|
|
348
|
+
_c.label = 4;
|
|
349
|
+
case 4:
|
|
350
|
+
_c.trys.push([4, 6, , 7]);
|
|
351
|
+
return [4 /*yield*/, stmt.execute(value)];
|
|
352
|
+
case 5:
|
|
353
|
+
row = (_c.sent())[0];
|
|
354
|
+
// @ts-ignore
|
|
355
|
+
result.push(row === null || row === void 0 ? void 0 : row.insertId);
|
|
356
|
+
return [3 /*break*/, 7];
|
|
357
|
+
case 6:
|
|
358
|
+
_b = _c.sent();
|
|
359
|
+
result.push(-1);
|
|
360
|
+
return [3 /*break*/, 7];
|
|
361
|
+
case 7:
|
|
362
|
+
_i++;
|
|
363
|
+
return [3 /*break*/, 3];
|
|
364
|
+
case 8: return [2 /*return*/, result];
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
});
|
|
368
|
+
};
|
|
369
|
+
return MysqlInserter;
|
|
370
|
+
}(BaseHelper));
|
|
371
|
+
exports.MysqlInserter = MysqlInserter;
|
|
372
|
+
/** @description 删除数据 */
|
|
373
|
+
var MysqlDeleter = /** @class */ (function (_super) {
|
|
374
|
+
__extends(MysqlDeleter, _super);
|
|
375
|
+
function MysqlDeleter(t) {
|
|
376
|
+
return _super.call(this, t) || this;
|
|
377
|
+
}
|
|
378
|
+
Object.defineProperty(MysqlDeleter.prototype, "sql", {
|
|
379
|
+
get: function () {
|
|
380
|
+
var _a = this, selectStat = _a.selectStat, optionStat = _a.optionStat;
|
|
381
|
+
var target = "DELETE FROM ".concat(this._tableName);
|
|
382
|
+
if (selectStat.sql) {
|
|
383
|
+
target += selectStat.sql;
|
|
384
|
+
}
|
|
385
|
+
if (optionStat.sql) {
|
|
386
|
+
target += optionStat.sql;
|
|
387
|
+
}
|
|
388
|
+
return target + ';';
|
|
389
|
+
},
|
|
390
|
+
enumerable: false,
|
|
391
|
+
configurable: true
|
|
392
|
+
});
|
|
393
|
+
/** @查询结果 */
|
|
394
|
+
MysqlDeleter.prototype.result = function () {
|
|
395
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
396
|
+
var _a, sql, selectStat, res, err_2;
|
|
397
|
+
return __generator(this, function (_b) {
|
|
398
|
+
switch (_b.label) {
|
|
399
|
+
case 0:
|
|
400
|
+
_b.trys.push([0, 2, , 3]);
|
|
401
|
+
_a = this, sql = _a.sql, selectStat = _a.selectStat;
|
|
402
|
+
return [4 /*yield*/, global.mysqlPool.execute(sql, selectStat.values)];
|
|
403
|
+
case 1:
|
|
404
|
+
res = (_b.sent())[0];
|
|
405
|
+
// @ts-ignore
|
|
406
|
+
return [2 /*return*/, res.affectedRows];
|
|
407
|
+
case 2:
|
|
408
|
+
err_2 = _b.sent();
|
|
409
|
+
return [2 /*return*/, 0];
|
|
410
|
+
case 3: return [2 /*return*/];
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
});
|
|
414
|
+
};
|
|
415
|
+
/** @根据id删除 */
|
|
416
|
+
MysqlDeleter.prototype.removeById = function (value) {
|
|
417
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
418
|
+
var count;
|
|
419
|
+
return __generator(this, function (_a) {
|
|
420
|
+
switch (_a.label) {
|
|
421
|
+
case 0:
|
|
422
|
+
this.select({
|
|
423
|
+
field: 'id',
|
|
424
|
+
value: value,
|
|
425
|
+
});
|
|
426
|
+
return [4 /*yield*/, this.result()];
|
|
427
|
+
case 1:
|
|
428
|
+
count = _a.sent();
|
|
429
|
+
if (count > 0) {
|
|
430
|
+
return [2 /*return*/, 'ok'];
|
|
431
|
+
}
|
|
432
|
+
return [2 /*return*/, 'fail'];
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
});
|
|
436
|
+
};
|
|
437
|
+
return MysqlDeleter;
|
|
438
|
+
}(BaseHelper));
|
|
439
|
+
exports.MysqlDeleter = MysqlDeleter;
|
|
440
|
+
/** @description 更新数据 */
|
|
441
|
+
var MysqlUpdater = /** @class */ (function (_super) {
|
|
442
|
+
__extends(MysqlUpdater, _super);
|
|
443
|
+
function MysqlUpdater(t, exFields) {
|
|
444
|
+
if (exFields === void 0) { exFields = []; }
|
|
445
|
+
var _this = _super.call(this, t) || this;
|
|
446
|
+
_this.exFields = exFields;
|
|
447
|
+
_this.updateFields = {};
|
|
448
|
+
return _this;
|
|
449
|
+
}
|
|
450
|
+
Object.defineProperty(MysqlUpdater.prototype, "updateStat", {
|
|
451
|
+
get: function () {
|
|
452
|
+
var _a = this, updateFields = _a.updateFields, exFields = _a.exFields;
|
|
453
|
+
var keys = Object.keys(updateFields).filter(function (k) { return k !== 'id' && !exFields.includes(k); });
|
|
454
|
+
var sql = '';
|
|
455
|
+
var values = [];
|
|
456
|
+
if (keys.length > 0) {
|
|
457
|
+
sql += ' SET ';
|
|
458
|
+
keys.map(function (field) {
|
|
459
|
+
sql += "".concat(utils_1.Parser.toDbName(field), " = ? ");
|
|
460
|
+
values.push(updateFields[field]);
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
return {
|
|
464
|
+
sql: sql,
|
|
465
|
+
values: values,
|
|
466
|
+
};
|
|
467
|
+
},
|
|
468
|
+
enumerable: false,
|
|
469
|
+
configurable: true
|
|
470
|
+
});
|
|
471
|
+
Object.defineProperty(MysqlUpdater.prototype, "sql", {
|
|
472
|
+
get: function () {
|
|
473
|
+
var _a = this, updateStat = _a.updateStat, selectStat = _a.selectStat, optionStat = _a.optionStat;
|
|
474
|
+
var target = "UPDATE ".concat(this._tableName);
|
|
475
|
+
if (selectStat.sql && updateStat.sql) {
|
|
476
|
+
target += updateStat.sql;
|
|
477
|
+
target += selectStat.sql;
|
|
478
|
+
}
|
|
479
|
+
if (optionStat.sql) {
|
|
480
|
+
target += optionStat.sql;
|
|
481
|
+
}
|
|
482
|
+
return target + ';';
|
|
483
|
+
},
|
|
484
|
+
enumerable: false,
|
|
485
|
+
configurable: true
|
|
486
|
+
});
|
|
487
|
+
MysqlUpdater.prototype.setField = function (field, v) {
|
|
488
|
+
this.updateFields[field] = v;
|
|
489
|
+
return this;
|
|
490
|
+
};
|
|
491
|
+
MysqlUpdater.prototype.setFields = function (values) {
|
|
492
|
+
this.updateFields = __assign(__assign({}, this.updateFields), values);
|
|
493
|
+
return this;
|
|
494
|
+
};
|
|
495
|
+
/** @查询结果 */
|
|
496
|
+
MysqlUpdater.prototype.result = function () {
|
|
497
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
498
|
+
var _a, sql, selectStat, updateStat, values, res, err_3;
|
|
499
|
+
return __generator(this, function (_b) {
|
|
500
|
+
switch (_b.label) {
|
|
501
|
+
case 0:
|
|
502
|
+
_b.trys.push([0, 2, , 3]);
|
|
503
|
+
_a = this, sql = _a.sql, selectStat = _a.selectStat, updateStat = _a.updateStat;
|
|
504
|
+
values = __spreadArray(__spreadArray([], updateStat.values, true), selectStat.values, true);
|
|
505
|
+
return [4 /*yield*/, global.mysqlPool.execute(sql, values)];
|
|
506
|
+
case 1:
|
|
507
|
+
res = (_b.sent())[0];
|
|
508
|
+
return [2 /*return*/, res.affectedRows];
|
|
509
|
+
case 2:
|
|
510
|
+
err_3 = _b.sent();
|
|
511
|
+
return [2 /*return*/, 0];
|
|
512
|
+
case 3: return [2 /*return*/];
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
});
|
|
516
|
+
};
|
|
517
|
+
return MysqlUpdater;
|
|
518
|
+
}(BaseHelper));
|
|
519
|
+
exports.MysqlUpdater = MysqlUpdater;
|
package/dist/index.js
CHANGED
|
@@ -13,7 +13,17 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
13
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
16
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
20
|
+
exports.setup = setup;
|
|
21
|
+
var promise_1 = __importDefault(require("mysql2/promise")); // 使用 Promise 接口
|
|
22
|
+
function setup(opts) {
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
global.mysqlPool = promise_1.default.createPool(opts);
|
|
25
|
+
}
|
|
26
|
+
__exportStar(require("./helper"), exports);
|
|
18
27
|
__exportStar(require("./meta"), exports);
|
|
28
|
+
__exportStar(require("./interface"), exports);
|
|
19
29
|
__exportStar(require("./utils"), exports);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConditionMaps = exports.SortMaps = exports.RelationMaps = void 0;
|
|
4
|
+
exports.RelationMaps = {
|
|
5
|
+
and: 'AND',
|
|
6
|
+
or: 'OR',
|
|
7
|
+
};
|
|
8
|
+
exports.SortMaps = {
|
|
9
|
+
asc: 'ASC',
|
|
10
|
+
desc: 'DESC',
|
|
11
|
+
};
|
|
12
|
+
exports.ConditionMaps = {
|
|
13
|
+
eq: '=',
|
|
14
|
+
ne: '!=',
|
|
15
|
+
gt: '>',
|
|
16
|
+
gte: '>=',
|
|
17
|
+
lt: '<',
|
|
18
|
+
lte: '<=',
|
|
19
|
+
like: 'LIKE',
|
|
20
|
+
include: 'IN',
|
|
21
|
+
area: 'BETWEEN', // 范围
|
|
22
|
+
not: '=', // 'NOT {name} = {value}'
|
|
23
|
+
};
|