speed 1.0.9 → 1.1.0
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/decorator.js +208 -0
- package/{speed.decorator.ts → decorator.ts} +1 -1
- package/index.js +6 -0
- package/index.ts +2 -2
- package/package.json +1 -1
- package/service.js +29 -0
- package/{speed.service.ts → service.ts} +1 -1
- package/tsconfig.json +10 -6
package/decorator.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;
|
|
12
|
+
return { next: verb(0), "throw": verb(1), "return": verb(2) };
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (_) try {
|
|
17
|
+
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [0, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var service_1 = require("./service");
|
|
38
|
+
var paramMetadataKey = Symbol('param');
|
|
39
|
+
function log(message) {
|
|
40
|
+
var optionalParams = [];
|
|
41
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
42
|
+
optionalParams[_i - 1] = arguments[_i];
|
|
43
|
+
}
|
|
44
|
+
console.log.apply(console, [message].concat(optionalParams));
|
|
45
|
+
}
|
|
46
|
+
exports.log = log;
|
|
47
|
+
function ResultType(constructorFunction) {
|
|
48
|
+
var newConstructorFunction = function () {
|
|
49
|
+
var args = [];
|
|
50
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
51
|
+
args[_i] = arguments[_i];
|
|
52
|
+
}
|
|
53
|
+
var func = function () {
|
|
54
|
+
return new (constructorFunction.bind.apply(constructorFunction, [void 0].concat(args)))();
|
|
55
|
+
};
|
|
56
|
+
func.prototype = constructorFunction.prototype;
|
|
57
|
+
return new func();
|
|
58
|
+
};
|
|
59
|
+
newConstructorFunction.prototype = constructorFunction.prototype;
|
|
60
|
+
return function (target, propertyKey) {
|
|
61
|
+
service_1.resultTypeMap.set([target.constructor.name, propertyKey].toString(), newConstructorFunction());
|
|
62
|
+
//never return
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
exports.ResultType = ResultType;
|
|
66
|
+
function Param(name) {
|
|
67
|
+
return function (target, propertyKey, parameterIndex) {
|
|
68
|
+
var existingParameters = Reflect.getOwnMetadata(paramMetadataKey, target, propertyKey) || [];
|
|
69
|
+
existingParameters.push([name, parameterIndex]);
|
|
70
|
+
Reflect.defineMetadata(paramMetadataKey, existingParameters, target, propertyKey);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
exports.Param = Param;
|
|
74
|
+
function convertSQLParams(args, target, propertyKey, decoratorSQL) {
|
|
75
|
+
var queryValues = [];
|
|
76
|
+
var argsVal;
|
|
77
|
+
if (typeof args[0] === 'object') {
|
|
78
|
+
argsVal = new Map(Object.getOwnPropertyNames(args[0]).map(function (valName) { return [
|
|
79
|
+
valName,
|
|
80
|
+
args[0][valName],
|
|
81
|
+
]; }));
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
var existingParameters = Reflect.getOwnMetadata(paramMetadataKey, target, propertyKey);
|
|
85
|
+
argsVal = new Map(existingParameters.map(function (_a) {
|
|
86
|
+
var argName = _a[0], argIdx = _a[1];
|
|
87
|
+
return [argName, args[argIdx]];
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
var regExp = /#{(\w+)}/g;
|
|
91
|
+
decoratorSQL.matchAll(regExp).slice().forEach(function (match) {
|
|
92
|
+
var replaceTag = match[0], matchName = match[1];
|
|
93
|
+
decoratorSQL = decoratorSQL.replace(new RegExp(replaceTag, 'g'), '?');
|
|
94
|
+
queryValues.push(argsVal.get(matchName));
|
|
95
|
+
});
|
|
96
|
+
return [decoratorSQL, queryValues];
|
|
97
|
+
}
|
|
98
|
+
function queryForExecute(sql, args, target, propertyKey) {
|
|
99
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
100
|
+
var sqlValues, newSql, result, _a;
|
|
101
|
+
return __generator(this, function (_b) {
|
|
102
|
+
switch (_b.label) {
|
|
103
|
+
case 0:
|
|
104
|
+
sqlValues = [];
|
|
105
|
+
newSql = sql;
|
|
106
|
+
if (args.length > 0) {
|
|
107
|
+
_a = convertSQLParams(args, target, propertyKey, sql), newSql = _a[0], sqlValues = _a[1];
|
|
108
|
+
}
|
|
109
|
+
return [4 /*yield*/, service_1.speedPromisePool.query(newSql, sqlValues)];
|
|
110
|
+
case 1:
|
|
111
|
+
result = (_b.sent())[0];
|
|
112
|
+
return [2 /*return*/, result];
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
function Insert(sql) {
|
|
118
|
+
return function (target, propertyKey, descriptor) {
|
|
119
|
+
var _this = this;
|
|
120
|
+
descriptor.value = function () {
|
|
121
|
+
var args = [];
|
|
122
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
123
|
+
args[_i] = arguments[_i];
|
|
124
|
+
}
|
|
125
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
126
|
+
var result;
|
|
127
|
+
return __generator(this, function (_a) {
|
|
128
|
+
switch (_a.label) {
|
|
129
|
+
case 0: return [4 /*yield*/, queryForExecute(sql, args, target, propertyKey)];
|
|
130
|
+
case 1:
|
|
131
|
+
result = _a.sent();
|
|
132
|
+
return [2 /*return*/, result.insertId];
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
exports.Insert = Insert;
|
|
140
|
+
function Update(sql) {
|
|
141
|
+
return function (target, propertyKey, descriptor) {
|
|
142
|
+
var _this = this;
|
|
143
|
+
descriptor.value = function () {
|
|
144
|
+
var args = [];
|
|
145
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
146
|
+
args[_i] = arguments[_i];
|
|
147
|
+
}
|
|
148
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
149
|
+
var result;
|
|
150
|
+
return __generator(this, function (_a) {
|
|
151
|
+
switch (_a.label) {
|
|
152
|
+
case 0: return [4 /*yield*/, queryForExecute(sql, args, target, propertyKey)];
|
|
153
|
+
case 1:
|
|
154
|
+
result = _a.sent();
|
|
155
|
+
return [2 /*return*/, result.affectedRows];
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
exports.Update = Update;
|
|
163
|
+
exports.Delete = Update;
|
|
164
|
+
function Select(sql) {
|
|
165
|
+
return function (target, propertyKey, descriptor) {
|
|
166
|
+
var _this = this;
|
|
167
|
+
descriptor.value = function () {
|
|
168
|
+
var args = [];
|
|
169
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
170
|
+
args[_i] = arguments[_i];
|
|
171
|
+
}
|
|
172
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
173
|
+
var sqlValues, rows, records, resultType, _loop_1, rowIndex, _a;
|
|
174
|
+
return __generator(this, function (_b) {
|
|
175
|
+
switch (_b.label) {
|
|
176
|
+
case 0:
|
|
177
|
+
sqlValues = [];
|
|
178
|
+
if (args.length > 0) {
|
|
179
|
+
_a = convertSQLParams(args, target, propertyKey, sql), sql = _a[0], sqlValues = _a[1];
|
|
180
|
+
}
|
|
181
|
+
return [4 /*yield*/, service_1.speedPromisePool.query(sql, sqlValues)];
|
|
182
|
+
case 1:
|
|
183
|
+
rows = (_b.sent())[0];
|
|
184
|
+
if (Object.keys(rows).length === 0) {
|
|
185
|
+
return [2 /*return*/];
|
|
186
|
+
}
|
|
187
|
+
records = [];
|
|
188
|
+
resultType = service_1.resultTypeMap.get([target.constructor.name, propertyKey].toString());
|
|
189
|
+
_loop_1 = function (rowIndex) {
|
|
190
|
+
var entity = Object.create(resultType);
|
|
191
|
+
Object.getOwnPropertyNames(resultType).forEach(function (propertyRow) {
|
|
192
|
+
if (rows[rowIndex].hasOwnProperty(propertyRow)) {
|
|
193
|
+
Object.defineProperty(entity, propertyRow, Object.getOwnPropertyDescriptor(rows[rowIndex], propertyRow));
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
records.push(entity);
|
|
197
|
+
};
|
|
198
|
+
for (rowIndex in rows) {
|
|
199
|
+
_loop_1(rowIndex);
|
|
200
|
+
}
|
|
201
|
+
return [2 /*return*/, records];
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
exports.Select = Select;
|
package/index.js
ADDED
package/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
1
|
+
export * from './decorator';
|
|
2
|
+
export * from './service';
|
package/package.json
CHANGED
package/service.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
9
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
10
|
+
};
|
|
11
|
+
var common_1 = require("@nestjs/common");
|
|
12
|
+
var mysql2_1 = require("mysql2");
|
|
13
|
+
exports.createPool = mysql2_1.createPool;
|
|
14
|
+
var speedPromisePool;
|
|
15
|
+
exports.speedPromisePool = speedPromisePool;
|
|
16
|
+
var resultTypeMap = new Map();
|
|
17
|
+
exports.resultTypeMap = resultTypeMap;
|
|
18
|
+
var Service = (function () {
|
|
19
|
+
function Service(pool) {
|
|
20
|
+
this.pool = pool;
|
|
21
|
+
exports.speedPromisePool = speedPromisePool = pool.promise();
|
|
22
|
+
}
|
|
23
|
+
return Service;
|
|
24
|
+
}());
|
|
25
|
+
Service = __decorate([
|
|
26
|
+
common_1.Injectable(),
|
|
27
|
+
__param(0, common_1.Inject('SPEED_POOL'))
|
|
28
|
+
], Service);
|
|
29
|
+
exports.Service = Service;
|
package/tsconfig.json
CHANGED
|
@@ -8,14 +8,18 @@
|
|
|
8
8
|
"allowSyntheticDefaultImports": true,
|
|
9
9
|
"target": "es2017",
|
|
10
10
|
"sourceMap": true,
|
|
11
|
-
"outDir": "./
|
|
11
|
+
"outDir": "./",
|
|
12
12
|
"baseUrl": "./",
|
|
13
|
-
"incremental": true,
|
|
14
13
|
"skipLibCheck": true,
|
|
15
14
|
"strictNullChecks": false,
|
|
16
15
|
"noImplicitAny": false,
|
|
17
|
-
"strictBindCallApply": false,
|
|
18
16
|
"forceConsistentCasingInFileNames": false,
|
|
19
|
-
"noFallthroughCasesInSwitch": false
|
|
20
|
-
|
|
21
|
-
}
|
|
17
|
+
"noFallthroughCasesInSwitch": false,
|
|
18
|
+
"types": []
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"index.ts",
|
|
22
|
+
"decorator.ts",
|
|
23
|
+
"service.ts"
|
|
24
|
+
]
|
|
25
|
+
}
|