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 +22 -65
- package/app/service/user.js +27 -25
- package/package.json +43 -43
- package/public/index.html +8 -8
package/app/model/user.js
CHANGED
|
@@ -1,70 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* 用户数据格式定义
|
|
3
|
+
* 仅描述数据结构,不包含数据访问方法
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
]
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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 =
|
|
25
|
+
module.exports = {
|
|
26
|
+
userSchema
|
|
27
|
+
};
|
package/app/service/user.js
CHANGED
|
@@ -1,49 +1,51 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 用户服务层
|
|
3
|
-
*
|
|
3
|
+
* 处理业务逻辑与数据访问(示例为内存存储,实际可替换为数据库)
|
|
4
4
|
*/
|
|
5
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
|
176
|
+
<p>服务层目录,处理业务逻辑与数据访问。</p>
|
|
177
177
|
<pre><code class="language-javascript">// app/service/user.js
|
|
178
178
|
class UserService {
|
|
179
179
|
async getUserList() {
|
|
180
|
-
return
|
|
180
|
+
return users; // 或数据库查询
|
|
181
181
|
}
|
|
182
182
|
}</code></pre>
|
|
183
183
|
<h4>model/</h4>
|
|
184
|
-
<p
|
|
184
|
+
<p>数据模型目录,仅定义数据格式(结构、字段)。</p>
|
|
185
185
|
<pre><code class="language-javascript">// app/model/user.js
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
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
|