speed 1.1.1 → 1.1.2

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.
Files changed (2) hide show
  1. package/README.md +305 -2
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,2 +1,305 @@
1
- # SpeedSQL
2
- SQL injection for nestjs, like mybatis.
1
+ ### Introduction
2
+
3
+ - Follow NestJS Module injection mode.
4
+ - With the TypeScript Decorators, same as Java annotations.
5
+ - Similar to MyBatis used in Java.
6
+ - Support for the Prepared Statements.
7
+ - Support for Entity Injection.
8
+ - Support the Connection pools by mysql2 within.
9
+
10
+ ### Quick Start
11
+
12
+ - Prepare some entities and configurations.
13
+
14
+ *db.provider.ts*
15
+ ```
16
+ import { createPool, Pool } from 'speed';
17
+
18
+ export const DbProviders = [
19
+ {
20
+ provide: 'SPEED_POOL',
21
+ useFactory: async (): Promise<Pool> => {
22
+ return await createPool({
23
+ host: 'localhost',
24
+ user: 'root',
25
+ port: 3306,
26
+ password: 'qwer1234',
27
+ database: 'test',
28
+ });
29
+ },
30
+ },
31
+ ];
32
+ ```
33
+ *entity/param.dto.ts*
34
+ ```
35
+ export class ParamDto {
36
+ constructor(public name: string, public age: number) {}
37
+ }
38
+ ```
39
+ *entity/user.dto.ts*
40
+ ```
41
+ export class UserDto {
42
+ constructor(public name: string, public age: number) {}
43
+ }
44
+ ```
45
+
46
+ * * *
47
+
48
+ - Import into the Module of NestJS
49
+
50
+
51
+ *app.mudule.ts*
52
+ ```
53
+ import { Module } from '@nestjs/common';
54
+ import { AppController } from './app.controller';
55
+ import { AppService } from './app.service';
56
+ import { SpeedService } from 'speed';
57
+ import { DbProviders } from './db.providers';
58
+
59
+ @Module({
60
+ imports: [],
61
+ controllers: [AppController],
62
+ providers: [AppService, SpeedService, ...DbProviders],
63
+ })
64
+ export class AppModule {}
65
+ ```
66
+
67
+ * * *
68
+
69
+ - Define SpeedSQL within Services, use the Decorators as MyBatis.
70
+
71
+ *app.service.ts*
72
+ ```
73
+ import { Injectable } from '@nestjs/common';
74
+ import { Delete, Update, Param, ResultType, Insert } from 'speed';
75
+ import { UserDto } from './entity/user.dto';
76
+ import { ParamDto } from './entity/param.dto';
77
+
78
+ @Injectable()
79
+ export class AppService {
80
+ @Update('update user set age = #{age} where name = #{name}')
81
+ setUserAge(@Param('name') name: string, @Param('age') age: number): number {return;}
82
+
83
+ @Delete('delete from user where name = #{name}')
84
+ deleteUser(@Param('name') name: string): number {return;}
85
+
86
+ @ResultType(UserDto)
87
+ @Select('select `name`, `age` from `user` where `uid` = #{uid} and `name` = #{name}')
88
+ getRecords(paramDto: ParamDto): UserDto[] {return;}
89
+
90
+ @Insert('insert into user (name, age) value (#{name}, #{age})')
91
+ addUser(user: UserDto): number {return;}
92
+ }
93
+ ```
94
+
95
+ * * *
96
+
97
+ - Use Your Services.
98
+
99
+ *app.controller.ts*
100
+ ```
101
+ import { Controller, Get } from '@nestjs/common';
102
+ import { AppService } from './app.service';
103
+ import { ParamDto } from './entity/param.dto';
104
+ import { UserDto } from "./entity/create-cat.dto";
105
+
106
+ @Controller()
107
+ export class AppController {
108
+ constructor(private readonly appService: AppService) {}
109
+
110
+ @Get()
111
+ async getHello() {
112
+ //const word = await this.appService.getMyWord(new ParamDto('world', 20));
113
+ await this.appService.setUserAge("zzz", 20);
114
+ return "hello world";
115
+ }
116
+ }
117
+
118
+ ```
119
+
120
+ ### Configuration
121
+
122
+ The Connection pools configuration is exactly the same as mysql2's [createPool\(\)](https://github.com/sidorares/node-mysql2#using-promise-wrapper).
123
+
124
+ The usual format is as follows:
125
+
126
+ ```
127
+ {
128
+ host: '127.0.0.1',
129
+ user: 'root',
130
+ port: 3306,
131
+ password: '123456',
132
+ database: 'test',
133
+ }
134
+ ```
135
+
136
+ * * *
137
+ Like common NestJS Modules, SpeedSQL uses [Asynchronous providers](https://docs.nestjs.com/fundamentals/async-providers) to inject it's Connection Pool for startup.
138
+
139
+ - Make file ```db.provider.ts```
140
+ ```
141
+ import { createPool, Pool } from 'speed';
142
+
143
+ export const DbProviders = [
144
+ {
145
+ provide: 'SPEED_POOL',
146
+ useFactory: async (): Promise<Pool> => {
147
+ return await createPool({
148
+ host: 'localhost',
149
+ user: 'root',
150
+ port: 3306,
151
+ password: 'qwer1234',
152
+ database: 'test',
153
+ });
154
+ },
155
+ },
156
+ ];
157
+ ```
158
+ - Put ```db.provider.ts``` in NestJS app src dir and set it as a provider.
159
+ ```
160
+ import { Module } from '@nestjs/common';
161
+ import { AppController } from './app.controller';
162
+ import { AppService } from './app.service';
163
+ import { SpeedService } from 'speed';
164
+ import { DbProviders } from './db.providers';
165
+
166
+ @Module({
167
+ imports: [],
168
+ controllers: [AppController],
169
+ providers: [AppService, SpeedService, ...DbProviders],
170
+ })
171
+ export class AppModule {}
172
+ ```
173
+
174
+ ### Parameter with named (for Prepared Statements)
175
+
176
+ 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.
177
+
178
+ 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.
179
+
180
+ **Parameter with named value can support ```@Select```, ```@Insert```, ```@Update```, ```@Delete``` all the CRUD operations.**
181
+
182
+ * * *
183
+
184
+ SpeedSQL has two Parameter with named modes.
185
+
186
+ > Note that you can only choose ONE of the modes at ONE statement.
187
+
188
+ **Object as Parameters**
189
+
190
+ - Creates a conditional entity class with the same attribute and parameter names.
191
+
192
+ ```
193
+ export class ParamDto {
194
+ constructor(public name: string, public age: number) {}
195
+ }
196
+ ```
197
+
198
+ - Inject values as parameter entities.
199
+
200
+ ```
201
+ import { ResultType, Select } from 'speed';
202
+
203
+
204
+ @ResultType(UserDto)
205
+ @Select('select `name`, `age` from `user` where `uid` = #{uid} and `name` = #{name}')
206
+ getRecords(paramDto: ParamDto): UserDto[]{return;}
207
+ ```
208
+ - So we can start to use.
209
+ ```
210
+ const users: UserDto[] = await this.appService.getRecords(
211
+ new ParamDto("zzz", 10)
212
+ );
213
+ ```
214
+
215
+ **Named Value as Parameters**
216
+
217
+ Annotate the parameter value name with the parameter annotation '@Param', which corresponds to the SQL value name.
218
+
219
+ ```
220
+ import { ResultType, Select, Param } from 'speed';
221
+
222
+ @ResultType(UserDto)
223
+ @Select(select `name`, `age` from `user` where `uid` = #{uid} and `name` = #{name}')
224
+ getRecords(@Param('uid') uid:number, @Param('name') name:string): UserDto[]{return;}
225
+ ```
226
+ So we can start to use.
227
+ ```
228
+ const users: UserDto[] = await this.appService.getRecords('zzz', 10);
229
+ ```
230
+
231
+ ### Select
232
+
233
+ SpeedSQL uses ```@resultType``` to annotate the resulting entity.
234
+
235
+ Select returns an array of annotated entity (```@resultType```).
236
+
237
+ - Create a entity:
238
+
239
+ ```
240
+ export class UserDto {
241
+ constructor(public name: string, public age: number) {}
242
+ }
243
+ ```
244
+ - And Select.
245
+ ```
246
+ import { ResultType, Select, Param } from 'speed';
247
+
248
+
249
+ @ResultType(UserDto)
250
+ @Select('select `name`, `age` from user where uid = #{uid} and name = #{name} ')
251
+ getRecords(@Param('uid') uid:number, @Param('name') name:string): UserDto[] {return;}
252
+ ```
253
+ - The return Array will contains entities, and field name will correspond to the attributes of the entity.
254
+
255
+ 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.
256
+
257
+
258
+ ```
259
+ import { ResultType, Select, Param } from 'speed';
260
+
261
+ @ResultType(UserDto)
262
+ @Select('select `realname` as `name`, `age` from user where uid = #{uid} and name = #{name} ')
263
+ getRecords(@Param('uid') uid:number, @Param('name') name:string): UserDto[] {return;}
264
+ ```
265
+
266
+
267
+ ### Insert
268
+
269
+ Parameter with named is also supported in ```@Insert```.
270
+
271
+ The ```@Insert``` return value is <u>the new inserted ID</u>, which can also be ignored.
272
+
273
+ ```
274
+ import { Insert } from 'speed';
275
+
276
+ @Insert('insert into user (name, age) value (#{name}, #{age})')
277
+ addUser(user: UserDto): number {return;}
278
+ ```
279
+
280
+ ### Update and Delete
281
+
282
+ Parameter with named is also supported in ```@Update``` and ```@Delete```.
283
+
284
+ The ```@Update``` and ```@Delete``` returns number is <u>the effected rows</u>, which can also be ignored.
285
+
286
+ ```
287
+ import { Delete, Update, Param } from 'speed';
288
+
289
+ @Update('update user set age = #{age} where name = #{name}')
290
+ setUserAge(@Param('name') name: string, @Param('age') age: number): number {return;}
291
+
292
+ @Delete('delete from user where name = #{name}')
293
+ deleteUser(@Param('name') name: string): number {return;}
294
+ ```
295
+
296
+
297
+ ### About
298
+
299
+ Github:[https://github.com/speedphp/speedsql](https://github.com/speedphp/speedsql)
300
+
301
+ The SpeedSQL project follows the open source agreement of the ```MIT License```.
302
+
303
+ Thanks: [NestJS](https://nestjs.com/),[mysql2](https://github.com/sidorares/node-mysql2),[MyBatis](https://mybatis.org/).
304
+
305
+ Issue: [https://github.com/SpeedPHP/speedsql/issues](https://github.com/SpeedPHP/speedsql/issues)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "speed",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "SQL injection for nestjs, like mybatis.",
5
5
  "author": "speedphp",
6
6
  "license": "MIT License",