@fastcar/cli 0.0.7 → 0.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/README.md +259 -21
- package/bin/cli.js +202 -32
- package/package.json +3 -3
- package/skills/fastcar-database/SKILL.md +374 -0
- package/skills/fastcar-framework/SKILL.md +661 -0
- package/skills/fastcar-framework/assets/project-template/package.json +24 -0
- package/skills/fastcar-framework/assets/project-template/resource/application.yml +14 -0
- package/skills/fastcar-framework/assets/project-template/src/app.ts +18 -0
- package/skills/fastcar-framework/assets/project-template/src/component/StartupRunner.ts +15 -0
- package/skills/fastcar-framework/assets/project-template/src/controller/HelloController.ts +21 -0
- package/skills/fastcar-framework/assets/project-template/src/service/HelloService.ts +12 -0
- package/skills/fastcar-framework/assets/project-template/tsconfig.json +21 -0
- package/skills/fastcar-framework/references/api-reference.md +353 -0
- package/skills/fastcar-rpc-microservices/SKILL.md +311 -0
- package/skills/fastcar-serverless/SKILL.md +211 -0
- package/skills/fastcar-toolkit/SKILL.md +282 -0
- package/src/init.js +615 -201
- package/src/pack.js +204 -0
- package/src/reverse.js +204 -0
- package/src/skill-targets.js +74 -0
- package/src/skill.js +362 -0
- package/src/templates.json +32 -0
- package/src/utils.js +51 -66
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: fastcar-database
|
|
3
|
+
description: FastCar 数据库与缓存开发指南。Use when working with FastCar framework for: (1) MySQL/PostgreSQL/MongoDB operations with @fastcar/mysql, @fastcar/pgsql, @fastcar/mongo, (2) Redis caching with @fastcar/redis, (3) Reverse engineering database tables to models/mappers with @fastcar/mysql-tool, (4) Writing entity models with decorators (@Table, @Field, @PrimaryKey, @Repository), (5) Using MysqlMapper/PgsqlMapper/MongoMapper for CRUD, joins, transactions.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# FastCar Database
|
|
7
|
+
|
|
8
|
+
FastCar 数据库模块提供基于装饰器的 ORM 支持,涵盖 MySQL、PostgreSQL、MongoDB 和 Redis,并支持通过 `mysql-tool` 逆向生成 Model 和 Mapper。
|
|
9
|
+
|
|
10
|
+
## 模块速查
|
|
11
|
+
|
|
12
|
+
### MySQL (@fastcar/mysql)
|
|
13
|
+
|
|
14
|
+
#### 开启 MySQL
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { Application } from "@fastcar/core/annotation";
|
|
18
|
+
import { EnableMysql } from "@fastcar/mysql/annotation";
|
|
19
|
+
|
|
20
|
+
@Application
|
|
21
|
+
@EnableMysql
|
|
22
|
+
class APP {}
|
|
23
|
+
export default new APP();
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
#### 定义实体模型
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { Table, Field, DBType, PrimaryKey, NotNull, Size, CustomType } from "@fastcar/core/annotation";
|
|
30
|
+
|
|
31
|
+
@Table("users")
|
|
32
|
+
class User {
|
|
33
|
+
@Field("id")
|
|
34
|
+
@DBType("int")
|
|
35
|
+
@PrimaryKey
|
|
36
|
+
id!: number;
|
|
37
|
+
|
|
38
|
+
@Field("name")
|
|
39
|
+
@DBType("varchar")
|
|
40
|
+
@NotNull
|
|
41
|
+
@Size({ maxSize: 50 })
|
|
42
|
+
name!: string;
|
|
43
|
+
|
|
44
|
+
@Field("profile")
|
|
45
|
+
@DBType("json")
|
|
46
|
+
@CustomType("json")
|
|
47
|
+
profile: any;
|
|
48
|
+
|
|
49
|
+
constructor(...args: any) {
|
|
50
|
+
Object.assign(this, ...args);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### 定义 Mapper
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { Entity, Repository } from "@fastcar/core/annotation";
|
|
59
|
+
import { MysqlMapper } from "@fastcar/mysql";
|
|
60
|
+
import User from "./User";
|
|
61
|
+
|
|
62
|
+
@Entity(User)
|
|
63
|
+
@Repository
|
|
64
|
+
class UserMapper extends MysqlMapper<User> {}
|
|
65
|
+
|
|
66
|
+
export default UserMapper;
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### Service 中使用
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { Service, Autowired } from "@fastcar/core/annotation";
|
|
73
|
+
import { OrderEnum } from "@fastcar/core/db";
|
|
74
|
+
import UserMapper from "./UserMapper";
|
|
75
|
+
import User from "./User";
|
|
76
|
+
|
|
77
|
+
@Service
|
|
78
|
+
class UserService {
|
|
79
|
+
@Autowired
|
|
80
|
+
private userMapper!: UserMapper;
|
|
81
|
+
|
|
82
|
+
// 查询单条
|
|
83
|
+
async getUser(id: number) {
|
|
84
|
+
return this.userMapper.selectOne({ where: { id } });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 条件查询
|
|
88
|
+
async queryUsers(name: string) {
|
|
89
|
+
return this.userMapper.select({
|
|
90
|
+
where: {
|
|
91
|
+
name: { value: name },
|
|
92
|
+
createdAt: { ">=": "2024-01-01", "<=": "2024-12-31" },
|
|
93
|
+
},
|
|
94
|
+
orders: { createdAt: OrderEnum.desc },
|
|
95
|
+
limit: 10,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 数组 IN 查询
|
|
100
|
+
async queryByIds(ids: number[]) {
|
|
101
|
+
return this.userMapper.select({
|
|
102
|
+
where: { id: { IN: ids } },
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// 新增
|
|
107
|
+
async createUser(name: string) {
|
|
108
|
+
const user = new User({ name, createdAt: new Date() });
|
|
109
|
+
return this.userMapper.saveOne(user);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 批量新增
|
|
113
|
+
async createUsers(users: User[]) {
|
|
114
|
+
return this.userMapper.saveList(users);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 更新
|
|
118
|
+
async updateName(id: number, name: string) {
|
|
119
|
+
return this.userMapper.update({ where: { id }, row: { name } });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// 按主键更新
|
|
123
|
+
async updateById(user: User) {
|
|
124
|
+
return this.userMapper.updateByPrimaryKey(user);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 删除
|
|
128
|
+
async deleteUser(id: number) {
|
|
129
|
+
return this.userMapper.delete({ where: { id } });
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// 判断存在
|
|
133
|
+
async exists(name: string) {
|
|
134
|
+
return this.userMapper.exist({ name });
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 统计
|
|
138
|
+
async count() {
|
|
139
|
+
return this.userMapper.count({});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 执行原生 SQL
|
|
143
|
+
async executeSql() {
|
|
144
|
+
return this.userMapper.execute("SELECT * FROM users WHERE id = 1");
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// 左连接查询
|
|
148
|
+
async leftJoin() {
|
|
149
|
+
return this.userMapper.selectByCustom({
|
|
150
|
+
join: [
|
|
151
|
+
{
|
|
152
|
+
type: "LEFT",
|
|
153
|
+
table: "orders o",
|
|
154
|
+
on: "o.user_id = t.id",
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
tableAlias: "t",
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// 强制索引
|
|
162
|
+
async forceIndex() {
|
|
163
|
+
return this.userMapper.select({
|
|
164
|
+
forceIndex: ["idx_name"],
|
|
165
|
+
orders: { name: OrderEnum.desc },
|
|
166
|
+
limit: 1,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// 使用函数
|
|
171
|
+
async formatDate() {
|
|
172
|
+
return this.userMapper.select({
|
|
173
|
+
fields: ['DATE_FORMAT(created_at, "%Y-%m-%d %H:%i:%s") as createdAt'],
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### 多数据源
|
|
180
|
+
|
|
181
|
+
在 `application.yml` 中配置多个数据源,Service 中通过指定数据源名称切换。
|
|
182
|
+
|
|
183
|
+
#### 事务
|
|
184
|
+
|
|
185
|
+
使用 `@Transactional`(如果框架提供)或手动通过 `SqlSession` 控制事务边界。
|
|
186
|
+
|
|
187
|
+
### PostgreSQL (@fastcar/pgsql)
|
|
188
|
+
|
|
189
|
+
#### 开启 PostgreSQL
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import { Application } from "@fastcar/core/annotation";
|
|
193
|
+
import { EnablePgsql } from "@fastcar/pgsql/annotation";
|
|
194
|
+
|
|
195
|
+
@Application
|
|
196
|
+
@EnablePgsql
|
|
197
|
+
class APP {}
|
|
198
|
+
export default new APP();
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
#### 定义 Mapper
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
import { Entity, Repository } from "@fastcar/core/annotation";
|
|
205
|
+
import { PgsqlMapper } from "@fastcar/pgsql";
|
|
206
|
+
import User from "./User";
|
|
207
|
+
|
|
208
|
+
@Entity(User)
|
|
209
|
+
@Repository
|
|
210
|
+
class UserMapper extends PgsqlMapper<User> {}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
用法与 `MysqlMapper` 基本一致,支持相同的查询条件和 CRUD 操作。
|
|
214
|
+
|
|
215
|
+
### MongoDB (@fastcar/mongo)
|
|
216
|
+
|
|
217
|
+
#### 开启 MongoDB
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import { Application } from "@fastcar/core/annotation";
|
|
221
|
+
import { EnableMongo } from "@fastcar/mongo/annotation";
|
|
222
|
+
|
|
223
|
+
@Application
|
|
224
|
+
@EnableMongo
|
|
225
|
+
class APP {}
|
|
226
|
+
export default new APP();
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
#### 定义 Mapper
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
import { Entity, Repository } from "@fastcar/core/annotation";
|
|
233
|
+
import { MongoMapper } from "@fastcar/mongo";
|
|
234
|
+
import User from "./User";
|
|
235
|
+
|
|
236
|
+
@Entity(User)
|
|
237
|
+
@Repository
|
|
238
|
+
class UserMapper extends MongoMapper<User> {}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Redis (@fastcar/redis)
|
|
242
|
+
|
|
243
|
+
#### 开启 Redis
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
import { Application } from "@fastcar/core/annotation";
|
|
247
|
+
import { EnableRedis } from "@fastcar/redis/annotation";
|
|
248
|
+
|
|
249
|
+
@Application
|
|
250
|
+
@EnableRedis
|
|
251
|
+
class APP {}
|
|
252
|
+
export default new APP();
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
#### 使用 RedisTemplate
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
import { Service, Autowired } from "@fastcar/core/annotation";
|
|
259
|
+
import { RedisClient } from "@fastcar/redis/annotation";
|
|
260
|
+
|
|
261
|
+
@Service
|
|
262
|
+
class CacheService {
|
|
263
|
+
@RedisClient
|
|
264
|
+
private redis!: RedisClient;
|
|
265
|
+
|
|
266
|
+
async set(key: string, value: string, ttl?: number) {
|
|
267
|
+
await this.redis.set(key, value, ttl);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async get(key: string) {
|
|
271
|
+
return this.redis.get(key);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async del(key: string) {
|
|
275
|
+
await this.redis.del(key);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## 数据库逆向生成 (@fastcar/mysql-tool)
|
|
281
|
+
|
|
282
|
+
### 生成配置文件
|
|
283
|
+
|
|
284
|
+
```bash
|
|
285
|
+
fastcar-cli reverse:init
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### 配置文件示例
|
|
289
|
+
|
|
290
|
+
```json
|
|
291
|
+
{
|
|
292
|
+
"tables": ["users", "orders"],
|
|
293
|
+
"modelDir": "D:/project/src/model",
|
|
294
|
+
"mapperDir": "D:/project/src/mapper",
|
|
295
|
+
"dbConfig": {
|
|
296
|
+
"host": "localhost",
|
|
297
|
+
"port": 3306,
|
|
298
|
+
"user": "root",
|
|
299
|
+
"password": "password",
|
|
300
|
+
"database": "test_db"
|
|
301
|
+
},
|
|
302
|
+
"style": {
|
|
303
|
+
"tabWidth": 4,
|
|
304
|
+
"printWidth": 200,
|
|
305
|
+
"trailingComma": "es5",
|
|
306
|
+
"useTabs": true,
|
|
307
|
+
"parser": "typescript",
|
|
308
|
+
"endOfLine": "crlf"
|
|
309
|
+
},
|
|
310
|
+
"ignoreCamelcase": false
|
|
311
|
+
}
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### 执行逆向生成
|
|
315
|
+
|
|
316
|
+
```bash
|
|
317
|
+
fastcar-cli reverse
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
## application.yml 数据库配置
|
|
321
|
+
|
|
322
|
+
```yaml
|
|
323
|
+
application:
|
|
324
|
+
env: dev
|
|
325
|
+
|
|
326
|
+
mysql:
|
|
327
|
+
host: localhost
|
|
328
|
+
port: 3306
|
|
329
|
+
database: mydb
|
|
330
|
+
username: root
|
|
331
|
+
password: password
|
|
332
|
+
|
|
333
|
+
pgsql:
|
|
334
|
+
host: localhost
|
|
335
|
+
port: 5432
|
|
336
|
+
database: mydb
|
|
337
|
+
username: postgres
|
|
338
|
+
password: password
|
|
339
|
+
|
|
340
|
+
mongo:
|
|
341
|
+
host: localhost
|
|
342
|
+
port: 27017
|
|
343
|
+
database: mydb
|
|
344
|
+
|
|
345
|
+
redis:
|
|
346
|
+
host: localhost
|
|
347
|
+
port: 6379
|
|
348
|
+
password: ""
|
|
349
|
+
db: 0
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
## 完整模块列表
|
|
353
|
+
|
|
354
|
+
| 模块 | 安装命令 | 用途 |
|
|
355
|
+
|------|----------|------|
|
|
356
|
+
| @fastcar/mysql | `npm i @fastcar/mysql` | MySQL ORM |
|
|
357
|
+
| @fastcar/pgsql | `npm i @fastcar/pgsql` | PostgreSQL ORM |
|
|
358
|
+
| @fastcar/mongo | `npm i @fastcar/mongo` | MongoDB |
|
|
359
|
+
| @fastcar/redis | `npm i @fastcar/redis` | Redis 缓存 |
|
|
360
|
+
| @fastcar/mysql-tool | `npm i @fastcar/mysql-tool` | 逆向生成工具 |
|
|
361
|
+
|
|
362
|
+
## 快速开始
|
|
363
|
+
|
|
364
|
+
```bash
|
|
365
|
+
# 1. 安装依赖
|
|
366
|
+
npm i @fastcar/core @fastcar/mysql @fastcar/redis
|
|
367
|
+
|
|
368
|
+
# 2. 配置 application.yml
|
|
369
|
+
|
|
370
|
+
# 3. 创建 Model、Mapper、Service
|
|
371
|
+
|
|
372
|
+
# 4. 启动应用
|
|
373
|
+
npm run debug
|
|
374
|
+
```
|