koa3-cli 1.0.2 → 1.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/README.md CHANGED
@@ -38,7 +38,7 @@ npm run dev
38
38
  ## 项目结构
39
39
 
40
40
  ```
41
- koa2-cli/
41
+ koa3-cli/
42
42
  ├── app/ # 应用代码目录
43
43
  │ ├── controller/ # 控制器目录
44
44
  │ │ ├── home.js # 首页控制器
@@ -99,18 +99,6 @@ npm start
99
99
  - API 示例: http://localhost:3000/api/user
100
100
  - 文档: http://localhost:3000/index.html
101
101
 
102
- ### 文档开发
103
-
104
- 启动 VuePress 文档开发服务器:
105
- ```bash
106
- npm run docs:dev
107
- ```
108
-
109
- 构建文档为静态文件:
110
- ```bash
111
- npm run docs:build
112
- ```
113
-
114
102
  ## 环境配置
115
103
 
116
104
  项目支持多环境配置,通过 `NODE_ENV` 环境变量控制:
@@ -196,8 +184,8 @@ router.get('/api/product', productController.list);
196
184
 
197
185
  ## 技术栈
198
186
 
199
- - **Koa2**: Web 框架
200
- - **koa-router**: 路由
187
+ - **Koa3**: Web 框架
188
+ - **@koa/router**: 路由
201
189
  - **koa-bodyparser**: 请求体解析
202
190
  - **koa-static**: 静态资源服务
203
191
  - **koa-views**: 模板引擎支持
@@ -7,7 +7,7 @@ class HomeController {
7
7
  */
8
8
  async index(ctx) {
9
9
  ctx.body = {
10
- message: 'Welcome to Koa2 CLI',
10
+ message: 'Welcome to Koa3 CLI',
11
11
  version: '1.0.0',
12
12
  timestamp: new Date().toISOString()
13
13
  };
package/app/model/user.js CHANGED
@@ -1,70 +1,27 @@
1
1
  /**
2
- * 用户数据模型
3
- * 负责与数据库交互
2
+ * 用户数据格式定义
3
+ * 仅描述数据结构,不包含数据访问方法
4
4
  */
5
5
 
6
- // 示例:模拟数据存储(实际项目中应该连接真实数据库)
7
- let users = [
8
- { id: 1, name: '张三', email: 'zhangsan@example.com', createdAt: new Date() },
9
- { id: 2, name: '李四', email: 'lisi@example.com', createdAt: new Date() }
10
- ];
11
-
12
- class UserModel {
13
- /**
14
- * 查找所有用户
15
- */
16
- async findAll() {
17
- // 实际项目中应该查询数据库
18
- return users;
19
- }
20
-
21
- /**
22
- * 根据ID查找用户
23
- */
24
- async findById(id) {
25
- return users.find(user => user.id === parseInt(id));
26
- }
27
-
28
- /**
29
- * 创建用户
30
- */
31
- async create(userData) {
32
- const newUser = {
33
- id: users.length > 0 ? Math.max(...users.map(u => u.id)) + 1 : 1,
34
- ...userData,
35
- createdAt: new Date()
36
- };
37
- users.push(newUser);
38
- return newUser;
39
- }
40
-
41
- /**
42
- * 更新用户
43
- */
44
- async update(id, userData) {
45
- const index = users.findIndex(user => user.id === parseInt(id));
46
- if (index === -1) {
47
- return null;
48
- }
49
- users[index] = {
50
- ...users[index],
51
- ...userData,
52
- updatedAt: new Date()
53
- };
54
- return users[index];
55
- }
6
+ /** @typedef {Object} User
7
+ * @property {number} id - 用户ID
8
+ * @property {string} name - 用户名
9
+ * @property {string} email - 邮箱
10
+ * @property {Date} [createdAt] - 创建时间
11
+ * @property {Date} [updatedAt] - 更新时间
12
+ */
56
13
 
57
- /**
58
- * 删除用户
59
- */
60
- async delete(id) {
61
- const index = users.findIndex(user => user.id === parseInt(id));
62
- if (index === -1) {
63
- return false;
64
- }
65
- users.splice(index, 1);
66
- return true;
67
- }
68
- }
14
+ /**
15
+ * 用户数据格式(用于校验、文档与默认值)
16
+ */
17
+ const userSchema = {
18
+ id: null,
19
+ name: '',
20
+ email: '',
21
+ createdAt: null,
22
+ updatedAt: null
23
+ };
69
24
 
70
- module.exports = new UserModel();
25
+ module.exports = {
26
+ userSchema
27
+ };
@@ -1,49 +1,51 @@
1
1
  /**
2
2
  * 用户服务层
3
- * 处理业务逻辑,与数据模型交互
3
+ * 处理业务逻辑与数据访问(示例为内存存储,实际可替换为数据库)
4
4
  */
5
- const userModel = require('../model/user');
5
+ // 示例:内存存储(实际项目中应连接数据库)
6
+ let users = [
7
+ { id: 1, name: '张三', email: 'zhangsan@example.com', createdAt: new Date() },
8
+ { id: 2, name: '李四', email: 'lisi@example.com', createdAt: new Date() }
9
+ ];
6
10
 
7
11
  class UserService {
8
- /**
9
- * 获取用户列表
10
- */
11
12
  async getUserList() {
12
- // 这里应该从数据库获取数据
13
- // 示例:返回模拟数据
14
- return await userModel.findAll();
13
+ return users;
15
14
  }
16
15
 
17
- /**
18
- * 根据ID获取用户
19
- */
20
16
  async getUserById(id) {
21
- return await userModel.findById(id);
17
+ return users.find(user => user.id === parseInt(id, 10));
22
18
  }
23
19
 
24
- /**
25
- * 创建用户
26
- */
27
20
  async createUser(userData) {
28
- // 数据验证
29
21
  if (!userData.name || !userData.email) {
30
22
  throw new Error('Name and email are required');
31
23
  }
32
- return await userModel.create(userData);
24
+ const newUser = {
25
+ id: users.length > 0 ? Math.max(...users.map(u => u.id)) + 1 : 1,
26
+ ...userData,
27
+ createdAt: new Date()
28
+ };
29
+ users.push(newUser);
30
+ return newUser;
33
31
  }
34
32
 
35
- /**
36
- * 更新用户
37
- */
38
33
  async updateUser(id, userData) {
39
- return await userModel.update(id, userData);
34
+ const index = users.findIndex(user => user.id === parseInt(id, 10));
35
+ if (index === -1) return null;
36
+ users[index] = {
37
+ ...users[index],
38
+ ...userData,
39
+ updatedAt: new Date()
40
+ };
41
+ return users[index];
40
42
  }
41
43
 
42
- /**
43
- * 删除用户
44
- */
45
44
  async deleteUser(id) {
46
- return await userModel.delete(id);
45
+ const index = users.findIndex(user => user.id === parseInt(id, 10));
46
+ if (index === -1) return false;
47
+ users.splice(index, 1);
48
+ return true;
47
49
  }
48
50
  }
49
51
 
package/app.js CHANGED
@@ -1,5 +1,4 @@
1
1
  const Koa = require('koa');
2
- const { Router } = require('@koa/router');
3
2
  const bodyParser = require('koa-bodyparser');
4
3
  const static = require('koa-static');
5
4
  const views = require('@ladjs/koa-views');
@@ -33,7 +32,7 @@ const router = require('./app/router');
33
32
  const app = new Koa();
34
33
 
35
34
  // 应用配置
36
- app.keys = config.keys || ['koa2-cli-secret-key'];
35
+ app.keys = config.keys || ['koa3-cli-secret-key'];
37
36
 
38
37
  // 静态资源
39
38
  if (config.static && config.static.enable !== false) {
@@ -4,7 +4,7 @@
4
4
  */
5
5
  module.exports = {
6
6
  // 应用名称
7
- name: 'koa2-cli',
7
+ name: 'koa3-cli',
8
8
 
9
9
  // 运行环境: development, production, test
10
10
  env: process.env.NODE_ENV || 'development',
@@ -13,7 +13,7 @@ module.exports = {
13
13
  port: process.env.PORT || 3000,
14
14
 
15
15
  // 密钥,用于加密cookie等
16
- keys: process.env.KEYS ? process.env.KEYS.split(',') : ['koa2-cli-secret-key'],
16
+ keys: process.env.KEYS ? process.env.KEYS.split(',') : ['koa3-cli-secret-key'],
17
17
 
18
18
  // 静态资源配置
19
19
  static: {
package/package.json CHANGED
@@ -1,43 +1,43 @@
1
- {
2
- "name": "koa3-cli",
3
- "version": "1.0.2",
4
- "description": "Koa3脚手架",
5
- "main": "app.js",
6
- "bin": {
7
- "koa3-cli": "./bin/cli.js"
8
- },
9
- "preferGlobal": true,
10
- "scripts": {
11
- "start": "node app.js",
12
- "dev": "nodemon app.js",
13
- "test": "echo \"Error: no test specified\" && exit 1"
14
- },
15
- "keywords": [
16
- "koa3",
17
- "scaffold",
18
- "cli"
19
- ],
20
- "author": "hikerw <965366906@qq.com>",
21
- "repository": {
22
- "type": "git",
23
- "url": "https://gitee.com/wangziwl/koa3-cli"
24
- },
25
- "homepage": "https://atwzc.cn/",
26
- "license": "MIT",
27
- "dependencies": {
28
- "@koa/router": "^15.2.0",
29
- "@ladjs/koa-views": "^9.0.0",
30
- "dotenv": "^17.2.3",
31
- "ejs": "^3.1.10",
32
- "koa": "^3.1.1",
33
- "koa-bodyparser": "^4.4.1",
34
- "koa-cors": "^0.0.16",
35
- "koa-static": "^5.0.0"
36
- },
37
- "devDependencies": {
38
- "nodemon": "^3.1.11"
39
- },
40
- "volta": {
41
- "node": "20.18.1"
42
- }
43
- }
1
+ {
2
+ "name": "koa3-cli",
3
+ "version": "1.0.4",
4
+ "description": "Koa3脚手架",
5
+ "main": "app.js",
6
+ "bin": {
7
+ "koa3-cli": "bin/cli.js"
8
+ },
9
+ "preferGlobal": true,
10
+ "scripts": {
11
+ "start": "node app.js",
12
+ "dev": "nodemon app.js",
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "keywords": [
16
+ "koa3",
17
+ "scaffold",
18
+ "cli"
19
+ ],
20
+ "author": "hikerw <965366906@qq.com>",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://gitee.com/wangziwl/koa3-cli"
24
+ },
25
+ "homepage": "https://atwzc.cn/",
26
+ "license": "MIT",
27
+ "dependencies": {
28
+ "@koa/router": "^15.2.0",
29
+ "@ladjs/koa-views": "^9.0.0",
30
+ "dotenv": "^17.2.3",
31
+ "ejs": "^3.1.10",
32
+ "koa": "^3.1.1",
33
+ "koa-bodyparser": "^4.4.1",
34
+ "koa-cors": "^0.0.16",
35
+ "koa-static": "^5.0.0"
36
+ },
37
+ "devDependencies": {
38
+ "nodemon": "^3.1.11"
39
+ },
40
+ "volta": {
41
+ "node": "20.18.1"
42
+ }
43
+ }
package/public/index.html CHANGED
@@ -173,21 +173,21 @@ class UserController {
173
173
  }
174
174
  }</code></pre>
175
175
  <h4>service/</h4>
176
- <p>服务层目录,处理业务逻辑。</p>
176
+ <p>服务层目录,处理业务逻辑与数据访问。</p>
177
177
  <pre><code class="language-javascript">// app/service/user.js
178
178
  class UserService {
179
179
  async getUserList() {
180
- return await userModel.findAll();
180
+ return users; // 或数据库查询
181
181
  }
182
182
  }</code></pre>
183
183
  <h4>model/</h4>
184
- <p>数据模型目录,处理数据访问。</p>
184
+ <p>数据模型目录,仅定义数据格式(结构、字段)。</p>
185
185
  <pre><code class="language-javascript">// app/model/user.js
186
- class UserModel {
187
- async findAll() {
188
- // 数据库查询逻辑
189
- }
190
- }</code></pre>
186
+ const userSchema = {
187
+ id: null, name: '', email: '',
188
+ createdAt: null, updatedAt: null
189
+ };
190
+ module.exports = { userSchema };</code></pre>
191
191
  <h4>middleware/</h4>
192
192
  <p>中间件目录,处理请求预处理和后处理。</p>
193
193
  <pre><code class="language-javascript">// app/middleware/index.js