koa3-cli 1.0.2 → 1.0.3

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/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/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.3",
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