speed 1.1.1 → 1.1.6
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/README.md +326 -2
- package/index.js +14 -5
- package/index.js.map +1 -1
- package/package.json +2 -2
- package/speed.decorator.js +54 -153
- package/speed.decorator.js.map +1 -1
- package/speed.decorator.ts +8 -2
- package/speed.service.js +19 -11
- package/speed.service.js.map +1 -1
package/README.md
CHANGED
|
@@ -1,2 +1,326 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
## SpeedSQL
|
|
2
|
+
|
|
3
|
+
SQL injection for NestJS, similar mybatis.
|
|
4
|
+
|
|
5
|
+
### Introduction
|
|
6
|
+
|
|
7
|
+
- Follow NestJS Module injection mode.
|
|
8
|
+
- With the TypeScript Decorators, same as Java annotations.
|
|
9
|
+
- Similar to MyBatis used in Java.
|
|
10
|
+
- Support for the Prepared Statements.
|
|
11
|
+
- Support for Entity Injection.
|
|
12
|
+
- Support the Connection pools by mysql2 within.
|
|
13
|
+
|
|
14
|
+
### Support Decorators
|
|
15
|
+
|
|
16
|
+
`@Param`, `@ResultType`, `@Select`, `@Insert`, `@Update`, `@Delete`.
|
|
17
|
+
|
|
18
|
+
### Install as a dependency
|
|
19
|
+
|
|
20
|
+
Setup SpeedSQL (NPM named `speed`) as dependency in *package.json* file `dependencies`
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"speed": "latest"
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Quick Start
|
|
29
|
+
|
|
30
|
+
- Prepare some entities and configurations.
|
|
31
|
+
|
|
32
|
+
*db.provider.ts*
|
|
33
|
+
```
|
|
34
|
+
import { createPool, Pool } from 'speed';
|
|
35
|
+
|
|
36
|
+
export const DbProviders = [
|
|
37
|
+
{
|
|
38
|
+
provide: 'SPEED_POOL',
|
|
39
|
+
useFactory: async (): Promise<Pool> => {
|
|
40
|
+
return await createPool({
|
|
41
|
+
host: 'localhost',
|
|
42
|
+
user: 'root',
|
|
43
|
+
port: 3306,
|
|
44
|
+
password: 'qwer1234',
|
|
45
|
+
database: 'test',
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
];
|
|
50
|
+
```
|
|
51
|
+
*entity/param.dto.ts*
|
|
52
|
+
```
|
|
53
|
+
export class ParamDto {
|
|
54
|
+
constructor(public name: string, public age: number) {}
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
*entity/user.dto.ts*
|
|
58
|
+
```
|
|
59
|
+
export class UserDto {
|
|
60
|
+
constructor(public name: string, public age: number) {}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
* * *
|
|
65
|
+
|
|
66
|
+
- Import into the Module of NestJS
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
*app.mudule.ts*
|
|
70
|
+
```
|
|
71
|
+
import { Module } from '@nestjs/common';
|
|
72
|
+
import { AppController } from './app.controller';
|
|
73
|
+
import { AppService } from './app.service';
|
|
74
|
+
import { SpeedService } from 'speed';
|
|
75
|
+
import { DbProviders } from './db.providers';
|
|
76
|
+
|
|
77
|
+
@Module({
|
|
78
|
+
imports: [],
|
|
79
|
+
controllers: [AppController],
|
|
80
|
+
providers: [AppService, SpeedService, ...DbProviders],
|
|
81
|
+
})
|
|
82
|
+
export class AppModule {}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
* * *
|
|
86
|
+
|
|
87
|
+
- Define SpeedSQL within Services, use the Decorators as MyBatis.
|
|
88
|
+
|
|
89
|
+
*app.service.ts*
|
|
90
|
+
```
|
|
91
|
+
import { Injectable } from '@nestjs/common';
|
|
92
|
+
import { Delete, Update, Param, ResultType, Insert } from 'speed';
|
|
93
|
+
import { UserDto } from './entity/user.dto';
|
|
94
|
+
import { ParamDto } from './entity/param.dto';
|
|
95
|
+
|
|
96
|
+
@Injectable()
|
|
97
|
+
export class AppService {
|
|
98
|
+
@Update('update user set age = #{age} where name = #{name}')
|
|
99
|
+
setUserAge(@Param('name') name: string, @Param('age') age: number): number {return;}
|
|
100
|
+
|
|
101
|
+
@Delete('delete from user where name = #{name}')
|
|
102
|
+
deleteUser(@Param('name') name: string): number {return;}
|
|
103
|
+
|
|
104
|
+
@ResultType(UserDto)
|
|
105
|
+
@Select('select `name`, `age` from `user` where `uid` = #{uid} and `name` = #{name}')
|
|
106
|
+
getRecords(paramDto: ParamDto): UserDto[] {return;}
|
|
107
|
+
|
|
108
|
+
@Insert('insert into user (name, age) value (#{name}, #{age})')
|
|
109
|
+
addUser(user: UserDto): number {return;}
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
* * *
|
|
114
|
+
|
|
115
|
+
- Use Your Services.
|
|
116
|
+
|
|
117
|
+
*app.controller.ts*
|
|
118
|
+
```
|
|
119
|
+
import { Controller, Get } from '@nestjs/common';
|
|
120
|
+
import { AppService } from './app.service';
|
|
121
|
+
import { ParamDto } from './entity/param.dto';
|
|
122
|
+
import { UserDto } from "./entity/create-cat.dto";
|
|
123
|
+
|
|
124
|
+
@Controller()
|
|
125
|
+
export class AppController {
|
|
126
|
+
constructor(private readonly appService: AppService) {}
|
|
127
|
+
|
|
128
|
+
@Get()
|
|
129
|
+
async getHello() {
|
|
130
|
+
await this.appService.setUserAge("zzz", 20);
|
|
131
|
+
return "hello world";
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Configuration
|
|
138
|
+
|
|
139
|
+
The Connection pools configuration is exactly the same as mysql2's [createPool\(\)](https://github.com/sidorares/node-mysql2#using-promise-wrapper).
|
|
140
|
+
|
|
141
|
+
The usual format is as follows:
|
|
142
|
+
|
|
143
|
+
```
|
|
144
|
+
{
|
|
145
|
+
host: '127.0.0.1',
|
|
146
|
+
user: 'root',
|
|
147
|
+
port: 3306,
|
|
148
|
+
password: '123456',
|
|
149
|
+
database: 'test',
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
* * *
|
|
154
|
+
Like common NestJS Modules, SpeedSQL uses [Asynchronous providers](https://docs.nestjs.com/fundamentals/async-providers) to inject it's Connection Pool for startup.
|
|
155
|
+
|
|
156
|
+
- Make file ```db.provider.ts```
|
|
157
|
+
```
|
|
158
|
+
import { createPool, Pool } from 'speed';
|
|
159
|
+
|
|
160
|
+
export const DbProviders = [
|
|
161
|
+
{
|
|
162
|
+
provide: 'SPEED_POOL',
|
|
163
|
+
useFactory: async (): Promise<Pool> => {
|
|
164
|
+
return await createPool({
|
|
165
|
+
host: 'localhost',
|
|
166
|
+
user: 'root',
|
|
167
|
+
port: 3306,
|
|
168
|
+
password: 'qwer1234',
|
|
169
|
+
database: 'test',
|
|
170
|
+
});
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
];
|
|
174
|
+
```
|
|
175
|
+
- Put ```db.provider.ts``` in NestJS app src dir and set it as a provider.
|
|
176
|
+
```
|
|
177
|
+
import { Module } from '@nestjs/common';
|
|
178
|
+
import { AppController } from './app.controller';
|
|
179
|
+
import { AppService } from './app.service';
|
|
180
|
+
import { SpeedService } from 'speed';
|
|
181
|
+
import { DbProviders } from './db.providers';
|
|
182
|
+
|
|
183
|
+
@Module({
|
|
184
|
+
imports: [],
|
|
185
|
+
controllers: [AppController],
|
|
186
|
+
providers: [AppService, SpeedService, ...DbProviders],
|
|
187
|
+
})
|
|
188
|
+
export class AppModule {}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Parameter with named (for Prepared Statements)
|
|
192
|
+
|
|
193
|
+
`@Param` define the named parameters.
|
|
194
|
+
|
|
195
|
+
As with MyBatis, SpeedSQL is possible to pass a value to a bind parameter as a named parameter to ensure readability and prevent SQL Injection attacks.
|
|
196
|
+
|
|
197
|
+
Unlike [Prepared Statements](https://github.com/sidorares/node-mysql2#using-prepared-statements) in mysql2, SpeedSQL can be pass param value with named to make SQL more clearer.
|
|
198
|
+
|
|
199
|
+
**Parameter with named value can support ```@Select```, ```@Insert```, ```@Update```, ```@Delete``` all the CRUD operations.**
|
|
200
|
+
|
|
201
|
+
* * *
|
|
202
|
+
|
|
203
|
+
SpeedSQL has two Parameter with named modes.
|
|
204
|
+
|
|
205
|
+
> Note that you can only choose ONE of the modes at ONE statement.
|
|
206
|
+
|
|
207
|
+
**Object as Parameters**
|
|
208
|
+
|
|
209
|
+
- Creates a conditional entity class with the same attribute and parameter names.
|
|
210
|
+
|
|
211
|
+
```
|
|
212
|
+
export class ParamDto {
|
|
213
|
+
constructor(public name: string, public age: number) {}
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
- Inject values as parameter entities.
|
|
218
|
+
|
|
219
|
+
```
|
|
220
|
+
import { ResultType, Select } from 'speed';
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
@ResultType(UserDto)
|
|
224
|
+
@Select('select `name`, `age` from `user` where `uid` = #{uid} and `name` = #{name}')
|
|
225
|
+
getRecords(paramDto: ParamDto): UserDto[]{return;}
|
|
226
|
+
```
|
|
227
|
+
- So we can start to use.
|
|
228
|
+
```
|
|
229
|
+
const users: UserDto[] = await this.appService.getRecords(
|
|
230
|
+
new ParamDto("zzz", 10)
|
|
231
|
+
);
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
**Named Value as Parameters**
|
|
235
|
+
|
|
236
|
+
Annotate the parameter value name with the parameter annotation '@Param', which corresponds to the SQL value name.
|
|
237
|
+
|
|
238
|
+
```
|
|
239
|
+
import { ResultType, Select, Param } from 'speed';
|
|
240
|
+
|
|
241
|
+
@ResultType(UserDto)
|
|
242
|
+
@Select(select `name`, `age` from `user` where `uid` = #{uid} and `name` = #{name}')
|
|
243
|
+
getRecords(@Param('uid') uid:number, @Param('name') name:string): UserDto[]{return;}
|
|
244
|
+
```
|
|
245
|
+
So we can start to use.
|
|
246
|
+
```
|
|
247
|
+
const users: UserDto[] = await this.appService.getRecords('zzz', 10);
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### @Select
|
|
251
|
+
|
|
252
|
+
SpeedSQL uses ```@ResultType``` to annotate the resulting entity.
|
|
253
|
+
|
|
254
|
+
`@ResultType` define the data entity for @Selete returns.
|
|
255
|
+
|
|
256
|
+
Select returns an array of annotated entity (```@ResultType```).
|
|
257
|
+
|
|
258
|
+
- Create a entity:
|
|
259
|
+
|
|
260
|
+
```
|
|
261
|
+
export class UserDto {
|
|
262
|
+
constructor(public name: string, public age: number) {}
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
- And Select.
|
|
266
|
+
```
|
|
267
|
+
import { ResultType, Select, Param } from 'speed';
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
@ResultType(UserDto)
|
|
271
|
+
@Select('select `name`, `age` from user where uid = #{uid} and name = #{name} ')
|
|
272
|
+
getRecords(@Param('uid') uid:number, @Param('name') name:string): UserDto[] {return;}
|
|
273
|
+
```
|
|
274
|
+
- The return Array will contains entities, and field name will correspond to the attributes of the entity.
|
|
275
|
+
|
|
276
|
+
If the field name and attribute are not the same, the value of different name will be lost. The solution is to use SQL's ```AS``` to alias the field name to correspond to the attributes of the entity.
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
```
|
|
280
|
+
import { ResultType, Select, Param } from 'speed';
|
|
281
|
+
|
|
282
|
+
@ResultType(UserDto)
|
|
283
|
+
@Select('select `realname` as `name`, `age` from user where uid = #{uid} and name = #{name} ')
|
|
284
|
+
getRecords(@Param('uid') uid:number, @Param('name') name:string): UserDto[] {return;}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
### @Insert
|
|
289
|
+
|
|
290
|
+
Parameter with named is also supported in ```@Insert```.
|
|
291
|
+
|
|
292
|
+
The ```@Insert``` return value is <u>the new inserted ID</u>, which can also be ignored.
|
|
293
|
+
|
|
294
|
+
```
|
|
295
|
+
import { Insert } from 'speed';
|
|
296
|
+
|
|
297
|
+
@Insert('insert into user (name, age) value (#{name}, #{age})')
|
|
298
|
+
addUser(user: UserDto): number {return;}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### @Update and @Delete
|
|
302
|
+
|
|
303
|
+
Parameter with named is also supported in ```@Update``` and ```@Delete```.
|
|
304
|
+
|
|
305
|
+
The ```@Update``` and ```@Delete``` returns number is <u>the effected rows</u>, which can also be ignored.
|
|
306
|
+
|
|
307
|
+
```
|
|
308
|
+
import { Delete, Update, Param } from 'speed';
|
|
309
|
+
|
|
310
|
+
@Update('update user set age = #{age} where name = #{name}')
|
|
311
|
+
setUserAge(@Param('name') name: string, @Param('age') age: number): number {return;}
|
|
312
|
+
|
|
313
|
+
@Delete('delete from user where name = #{name}')
|
|
314
|
+
deleteUser(@Param('name') name: string): number {return;}
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
### About
|
|
319
|
+
|
|
320
|
+
Github:[https://github.com/speedphp/speedsql](https://github.com/speedphp/speedsql)
|
|
321
|
+
|
|
322
|
+
The SpeedSQL project follows the open source agreement of the ```MIT License```.
|
|
323
|
+
|
|
324
|
+
Thanks: [NestJS](https://nestjs.com/),[mysql2](https://github.com/sidorares/node-mysql2),[MyBatis](https://mybatis.org/).
|
|
325
|
+
|
|
326
|
+
Issue: [https://github.com/SpeedPHP/speedsql/issues](https://github.com/SpeedPHP/speedsql/issues)
|
package/index.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
function
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./speed.decorator"), exports);
|
|
14
|
+
__exportStar(require("./speed.service"), exports);
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oDAAkC;AAClC,kDAAgC"}
|
package/package.json
CHANGED
package/speed.decorator.js
CHANGED
|
@@ -1,57 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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 speed_service_1 = require("./speed.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));
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Delete = exports.Update = exports.Insert = exports.Select = exports.ResultType = exports.Param = exports.log = void 0;
|
|
4
|
+
const speed_service_1 = require("./speed.service");
|
|
5
|
+
const paramMetadataKey = Symbol('param');
|
|
6
|
+
function log(message, ...optionalParams) {
|
|
7
|
+
console.log(message, ...optionalParams);
|
|
45
8
|
}
|
|
46
9
|
exports.log = log;
|
|
47
10
|
function ResultType(constructorFunction) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
args[_i] = arguments[_i];
|
|
52
|
-
}
|
|
53
|
-
var func = function () {
|
|
54
|
-
return new (constructorFunction.bind.apply(constructorFunction, [void 0].concat(args)))();
|
|
11
|
+
const newConstructorFunction = function (...args) {
|
|
12
|
+
const func = function () {
|
|
13
|
+
return new constructorFunction(...args);
|
|
55
14
|
};
|
|
56
15
|
func.prototype = constructorFunction.prototype;
|
|
57
16
|
return new func();
|
|
@@ -59,103 +18,62 @@ function ResultType(constructorFunction) {
|
|
|
59
18
|
newConstructorFunction.prototype = constructorFunction.prototype;
|
|
60
19
|
return function (target, propertyKey) {
|
|
61
20
|
speed_service_1.resultTypeMap.set([target.constructor.name, propertyKey].toString(), newConstructorFunction());
|
|
62
|
-
//never return
|
|
63
21
|
};
|
|
64
22
|
}
|
|
65
23
|
exports.ResultType = ResultType;
|
|
66
24
|
function Param(name) {
|
|
67
25
|
return function (target, propertyKey, parameterIndex) {
|
|
68
|
-
|
|
26
|
+
const existingParameters = Reflect.getOwnMetadata(paramMetadataKey, target, propertyKey) || [];
|
|
69
27
|
existingParameters.push([name, parameterIndex]);
|
|
70
28
|
Reflect.defineMetadata(paramMetadataKey, existingParameters, target, propertyKey);
|
|
71
29
|
};
|
|
72
30
|
}
|
|
73
31
|
exports.Param = Param;
|
|
74
32
|
function convertSQLParams(args, target, propertyKey, decoratorSQL) {
|
|
75
|
-
|
|
76
|
-
|
|
33
|
+
const queryValues = [];
|
|
34
|
+
let argsVal;
|
|
77
35
|
if (typeof args[0] === 'object') {
|
|
78
|
-
argsVal = new Map(Object.getOwnPropertyNames(args[0]).map(
|
|
36
|
+
argsVal = new Map(Object.getOwnPropertyNames(args[0]).map((valName) => [
|
|
79
37
|
valName,
|
|
80
38
|
args[0][valName],
|
|
81
|
-
]
|
|
39
|
+
]));
|
|
82
40
|
}
|
|
83
41
|
else {
|
|
84
|
-
|
|
85
|
-
argsVal = new Map(existingParameters.map(
|
|
86
|
-
var argName = _a[0], argIdx = _a[1];
|
|
87
|
-
return [argName, args[argIdx]];
|
|
88
|
-
}));
|
|
42
|
+
const existingParameters = Reflect.getOwnMetadata(paramMetadataKey, target, propertyKey);
|
|
43
|
+
argsVal = new Map(existingParameters.map(([argName, argIdx]) => [argName, args[argIdx]]));
|
|
89
44
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
45
|
+
const regExp = /#{(\w+)}/g;
|
|
46
|
+
let match;
|
|
47
|
+
while (match = regExp.exec(decoratorSQL)) {
|
|
48
|
+
const [replaceTag, matchName] = match;
|
|
93
49
|
decoratorSQL = decoratorSQL.replace(new RegExp(replaceTag, 'g'), '?');
|
|
94
50
|
queryValues.push(argsVal.get(matchName));
|
|
95
|
-
}
|
|
51
|
+
}
|
|
96
52
|
return [decoratorSQL, queryValues];
|
|
97
53
|
}
|
|
98
|
-
function queryForExecute(sql, args, target, propertyKey) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (args.length > 0) {
|
|
107
|
-
_a = convertSQLParams(args, target, propertyKey, sql), newSql = _a[0], sqlValues = _a[1];
|
|
108
|
-
}
|
|
109
|
-
return [4 /*yield*/, speed_service_1.speedPromisePool.query(newSql, sqlValues)];
|
|
110
|
-
case 1:
|
|
111
|
-
result = (_b.sent())[0];
|
|
112
|
-
return [2 /*return*/, result];
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
});
|
|
54
|
+
async function queryForExecute(sql, args, target, propertyKey) {
|
|
55
|
+
let sqlValues = [];
|
|
56
|
+
let newSql = sql;
|
|
57
|
+
if (args.length > 0) {
|
|
58
|
+
[newSql, sqlValues] = convertSQLParams(args, target, propertyKey, sql);
|
|
59
|
+
}
|
|
60
|
+
const [result] = await speed_service_1.speedPromisePool.query(newSql, sqlValues);
|
|
61
|
+
return result;
|
|
116
62
|
}
|
|
117
63
|
function Insert(sql) {
|
|
118
64
|
return function (target, propertyKey, descriptor) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
-
});
|
|
65
|
+
descriptor.value = async (...args) => {
|
|
66
|
+
const result = await queryForExecute(sql, args, target, propertyKey);
|
|
67
|
+
return result.insertId;
|
|
136
68
|
};
|
|
137
69
|
};
|
|
138
70
|
}
|
|
139
71
|
exports.Insert = Insert;
|
|
140
72
|
function Update(sql) {
|
|
141
73
|
return function (target, propertyKey, descriptor) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
});
|
|
74
|
+
descriptor.value = async (...args) => {
|
|
75
|
+
const result = await queryForExecute(sql, args, target, propertyKey);
|
|
76
|
+
return result.affectedRows;
|
|
159
77
|
};
|
|
160
78
|
};
|
|
161
79
|
}
|
|
@@ -163,46 +81,29 @@ exports.Update = Update;
|
|
|
163
81
|
exports.Delete = Update;
|
|
164
82
|
function Select(sql) {
|
|
165
83
|
return function (target, propertyKey, descriptor) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
84
|
+
descriptor.value = async (...args) => {
|
|
85
|
+
let sqlValues = [];
|
|
86
|
+
if (args.length > 0) {
|
|
87
|
+
[sql, sqlValues] = convertSQLParams(args, target, propertyKey, sql);
|
|
88
|
+
}
|
|
89
|
+
const [rows] = await speed_service_1.speedPromisePool.query(sql, sqlValues);
|
|
90
|
+
if (Object.keys(rows).length === 0) {
|
|
91
|
+
return;
|
|
171
92
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
_a = convertSQLParams(args, target, propertyKey, sql), sql = _a[0], sqlValues = _a[1];
|
|
180
|
-
}
|
|
181
|
-
return [4 /*yield*/, speed_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 = speed_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];
|
|
93
|
+
const records = [];
|
|
94
|
+
const resultType = speed_service_1.resultTypeMap.get([target.constructor.name, propertyKey].toString());
|
|
95
|
+
for (const rowIndex in rows) {
|
|
96
|
+
const entity = Object.create(resultType);
|
|
97
|
+
Object.getOwnPropertyNames(resultType).forEach(function (propertyRow) {
|
|
98
|
+
if (rows[rowIndex].hasOwnProperty(propertyRow)) {
|
|
99
|
+
Object.defineProperty(entity, propertyRow, Object.getOwnPropertyDescriptor(rows[rowIndex], propertyRow));
|
|
202
100
|
}
|
|
203
101
|
});
|
|
204
|
-
|
|
102
|
+
records.push(entity);
|
|
103
|
+
}
|
|
104
|
+
return records;
|
|
205
105
|
};
|
|
206
106
|
};
|
|
207
107
|
}
|
|
208
108
|
exports.Select = Select;
|
|
109
|
+
//# sourceMappingURL=speed.decorator.js.map
|
package/speed.decorator.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"speed.decorator.js","sourceRoot":"","sources":["speed.decorator.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"speed.decorator.js","sourceRoot":"","sources":["speed.decorator.ts"],"names":[],"mappings":";;;AAAA,mDAAkE;AAGlE,MAAM,gBAAgB,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;AAEzC,SAAS,GAAG,CAAC,OAAa,EAAE,GAAG,cAAqB;IAClD,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,cAAc,CAAC,CAAC;AAC1C,CAAC;AAsKQ,kBAAG;AApKZ,SAAS,UAAU,CAAC,mBAAmB;IACrC,MAAM,sBAAsB,GAAQ,UAAU,GAAG,IAAI;QACnD,MAAM,IAAI,GAAQ;YAChB,OAAO,IAAI,mBAAmB,CAAC,GAAG,IAAI,CAAC,CAAC;QAC1C,CAAC,CAAC;QACF,IAAI,CAAC,SAAS,GAAG,mBAAmB,CAAC,SAAS,CAAC;QAC/C,OAAO,IAAI,IAAI,EAAE,CAAC;IACpB,CAAC,CAAC;IACF,sBAAsB,CAAC,SAAS,GAAG,mBAAmB,CAAC,SAAS,CAAC;IACjE,OAAO,UAAU,MAAM,EAAE,WAAmB;QAC1C,6BAAa,CAAC,GAAG,CACb,CAA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,CAAC,OAAO,CAAC,UAAU,WAAW;oBAClE,IAAI,IAAI,CAAC,QAAQ,CAAC,CAAC,cAAc,CAAC,WAAW,CAAC,EAAE;wBAC9C,MAAM,CAAC,cAAc,CACjB,MAAM,EACN,WAAW,EACX,MAAM,CAAC,wBAAwB,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,WAAW,CAAC,CAC/D,CAAC;qBACH;gBACH,CAAC,CAAC,CAAC;gBACH,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;aACtB;YACD,OAAO,OAAO,CAAC;QACjB,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAEgC,wBAAM"}
|
package/speed.decorator.ts
CHANGED
|
@@ -69,11 +69,17 @@ function convertSQLParams(
|
|
|
69
69
|
);
|
|
70
70
|
}
|
|
71
71
|
const regExp = /#{(\w+)}/g;
|
|
72
|
-
|
|
72
|
+
let match;
|
|
73
|
+
while(match = regExp.exec(decoratorSQL)){
|
|
73
74
|
const [replaceTag, matchName] = match;
|
|
74
75
|
decoratorSQL = decoratorSQL.replace(new RegExp(replaceTag, 'g'), '?');
|
|
75
76
|
queryValues.push(argsVal.get(matchName));
|
|
76
|
-
}
|
|
77
|
+
}
|
|
78
|
+
// [...decoratorSQL.matchAll(regExp)].forEach((match) => {
|
|
79
|
+
// const [replaceTag, matchName] = match;
|
|
80
|
+
// decoratorSQL = decoratorSQL.replace(new RegExp(replaceTag, 'g'), '?');
|
|
81
|
+
// queryValues.push(argsVal.get(matchName));
|
|
82
|
+
// });
|
|
77
83
|
return [decoratorSQL, queryValues];
|
|
78
84
|
}
|
|
79
85
|
|
package/speed.service.js
CHANGED
|
@@ -5,25 +5,33 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
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
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
8
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
9
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
10
13
|
};
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
exports.createPool =
|
|
14
|
-
|
|
14
|
+
var _a;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.Pool = exports.createPool = exports.resultTypeMap = exports.speedPromisePool = exports.SpeedService = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const mysql2_1 = require("mysql2");
|
|
19
|
+
Object.defineProperty(exports, "Pool", { enumerable: true, get: function () { return mysql2_1.Pool; } });
|
|
20
|
+
Object.defineProperty(exports, "createPool", { enumerable: true, get: function () { return mysql2_1.createPool; } });
|
|
21
|
+
let speedPromisePool;
|
|
15
22
|
exports.speedPromisePool = speedPromisePool;
|
|
16
|
-
|
|
23
|
+
const resultTypeMap = new Map();
|
|
17
24
|
exports.resultTypeMap = resultTypeMap;
|
|
18
|
-
|
|
19
|
-
|
|
25
|
+
let SpeedService = class SpeedService {
|
|
26
|
+
constructor(pool) {
|
|
20
27
|
this.pool = pool;
|
|
21
28
|
exports.speedPromisePool = speedPromisePool = pool.promise();
|
|
22
29
|
}
|
|
23
|
-
|
|
24
|
-
}());
|
|
30
|
+
};
|
|
25
31
|
SpeedService = __decorate([
|
|
26
|
-
common_1.Injectable(),
|
|
27
|
-
__param(0, common_1.Inject('SPEED_POOL'))
|
|
32
|
+
(0, common_1.Injectable)(),
|
|
33
|
+
__param(0, (0, common_1.Inject)('SPEED_POOL')),
|
|
34
|
+
__metadata("design:paramtypes", [typeof (_a = typeof mysql2_1.Pool !== "undefined" && mysql2_1.Pool) === "function" ? _a : Object])
|
|
28
35
|
], SpeedService);
|
|
29
36
|
exports.SpeedService = SpeedService;
|
|
37
|
+
//# sourceMappingURL=speed.service.js.map
|
package/speed.service.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"speed.service.js","sourceRoot":"","sources":["speed.service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"speed.service.js","sourceRoot":"","sources":["speed.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAoD;AACpD,mCAA0C;AAYY,qFAZ7C,aAAI,OAY6C;AAAhB,2FAZ3B,mBAAU,OAY2B;AATpD,IAAI,gBAA6B,CAAC;AASzB,4CAAgB;AARzB,MAAM,aAAa,GAAG,IAAI,GAAG,EAAe,CAAC;AAQlB,sCAAa;AALxC,IAAa,YAAY,GAAzB,MAAa,YAAY;IACrB,YAA0C,IAAU;QAAV,SAAI,GAAJ,IAAI,CAAM;QAChD,2BAAA,gBAAgB,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;IACtC,CAAC;CACJ,CAAA;AAJY,YAAY;IADxB,IAAA,mBAAU,GAAE;IAEI,WAAA,IAAA,eAAM,EAAC,YAAY,CAAC,CAAA;yDAAe,aAAI,oBAAJ,aAAI;GAD3C,YAAY,CAIxB;AAJY,oCAAY"}
|