streamline-code-by-ikun2274 0.12.4 → 0.12.5
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/Example.js +14 -63
- package/extensions.txt +0 -0
- package/package.json +3 -5
- package/src/Databaseope/data.js +14 -1
- package/src/Request/StreamlineCode.js +16 -26
- package/types/index.d.ts +3 -22
package/Example.js
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
|
-
import { setDbConfig,startServer,
|
|
1
|
+
import { setDbConfig,startServer,
|
|
2
|
+
DefineRoutePlus_A } from './src/StreamlineCodeIndex.js';
|
|
2
3
|
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
setDbConfig({
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* host: 'your-host',
|
|
10
|
-
user: 'your-username',
|
|
11
|
-
password: 'your-password',
|
|
12
|
-
database: 'your-database'
|
|
13
|
-
*
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
8
|
host: 'localhost',
|
|
17
9
|
user: 'root', // 你的 MySQL 用户名
|
|
18
10
|
password: '123456', // 你的 MySQL 密码
|
|
@@ -32,69 +24,28 @@ startServer({
|
|
|
32
24
|
});
|
|
33
25
|
|
|
34
26
|
|
|
35
|
-
DefineRoutePlus_A({
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 二次封装 defineRoute 函数
|
|
39
|
-
* {Object} options - 路由配置选项
|
|
40
|
-
* {string} options.url - 路由路径(必填)
|
|
41
|
-
* {string} options.method - HTTP 方法(GET/POST/PUT/DELETE,必填)
|
|
42
|
-
* {Array<Function>} [options.middlewares=[]] - 中间件数组(可选)
|
|
43
|
-
* {Function} options.handler - 业务逻辑处理函数(async (req) => data,必填)
|
|
44
|
-
* {Object} options.PlusHandler - 额外处理选项(必填)
|
|
45
|
-
* {Object} options.PlusHandler.db - 数据库操作(必填)select/insert/update/update/delete
|
|
46
|
-
* {string} options.PlusHandler.db.table - 数据库表名(必填)
|
|
47
|
-
* {string} options.PlusHandler.db.operation - 数据库操作(必填)
|
|
48
|
-
* {string} [options.PlusHandler.db.primaryKey='id'] - 主键字段(可选)
|
|
49
|
-
* {string} [options.PlusHandler.db.fields='*'] - 查询字段(可选)
|
|
50
|
-
* {string} [options.PlusHandler.db.categoryField='category_id'] - 分类字段(可选)
|
|
51
|
-
* {Object} options.PlusHandler.Mode - 模式选项(必填)
|
|
52
|
-
* {boolean} [options.PlusHandler.Mode.pagination=false] - 是否支持分页(可选)
|
|
53
|
-
* {boolean} [options.PlusHandler.Mode.requireAuth=false] - 是否需要身份验证(可选)
|
|
54
|
-
* {boolean} [options.PlusHandler.Mode.shuffle=false] - 是否打乱数据(可选)
|
|
55
|
-
* {number} [options.PlusHandler.Mode.pageSize=10] - 每页大小(可选)
|
|
56
|
-
* {Object} options.PlusHandler.other - 其他选项(必填)
|
|
57
|
-
* {string} [options.PlusHandler.other.successMessage='操作成功'] - 成功消息(可选)
|
|
58
|
-
* {string} [options.PlusHandler.other.errorMessage='操作失败'] - 错误消息(可选)
|
|
59
|
-
* */
|
|
60
27
|
|
|
61
|
-
|
|
28
|
+
|
|
29
|
+
DefineRoutePlus_A({
|
|
62
30
|
url: '/api/productlist',
|
|
63
|
-
//请求方法
|
|
64
31
|
method: 'get',
|
|
65
|
-
|
|
32
|
+
// 可选:内置中间件配置
|
|
33
|
+
BuiltinMid: {
|
|
34
|
+
//可选的中间件参数
|
|
35
|
+
Parameter: ['pagination:12', 'unifiedResponse', 'shuffle','requireAuth'] // 启用分页和统一响应
|
|
36
|
+
},
|
|
66
37
|
PlusHandler: {
|
|
67
|
-
//数据表操作
|
|
68
38
|
db: {
|
|
69
|
-
table: '
|
|
70
|
-
operation: 'select'
|
|
39
|
+
table: 'productdb',
|
|
40
|
+
operation: 'select', // select/read 都可以
|
|
71
41
|
primaryKey: 'id',
|
|
72
42
|
fields: '*',
|
|
43
|
+
categoryField: 'category_id' // 可选,默认为 'category_id'
|
|
73
44
|
},
|
|
74
|
-
Mode: {
|
|
75
|
-
pagination: true,
|
|
76
|
-
requireAuth: false,
|
|
77
|
-
shuffle: false,
|
|
78
|
-
pageSize: 10
|
|
79
|
-
|
|
80
|
-
},
|
|
81
|
-
//其他
|
|
82
45
|
other: {
|
|
83
46
|
successMessage: '操作成功',
|
|
84
|
-
errorMessage: '操作失败'
|
|
47
|
+
errorMessage: '操作失败',
|
|
48
|
+
customLogic: null // 可选,自定义逻辑函数
|
|
85
49
|
}
|
|
86
50
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*/
|
|
95
51
|
});
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
package/extensions.txt
ADDED
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "streamline-code-by-ikun2274",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.5",
|
|
4
4
|
"description": "A streamline Express route builder with database operations, pagination, and authentication",
|
|
5
5
|
"main": "src/StreamlineCodeIndex.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"exports": {
|
|
7
8
|
".": {
|
|
8
9
|
"import": "./src/StreamlineCodeIndex.js",
|
|
9
10
|
"type": "./types/index.d.ts"
|
|
10
|
-
|
|
11
11
|
}
|
|
12
12
|
},
|
|
13
13
|
"types": "./types/index.d.ts",
|
|
14
|
-
"scripts": {
|
|
15
|
-
|
|
16
|
-
},
|
|
14
|
+
"scripts": {},
|
|
17
15
|
"keywords": [
|
|
18
16
|
"express",
|
|
19
17
|
"router",
|
package/src/Databaseope/data.js
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
//data.js
|
|
4
4
|
|
|
5
|
-
import mysql from 'mysql2/promise';
|
|
6
5
|
|
|
6
|
+
//import mysql from 'mysql2/promise';
|
|
7
|
+
|
|
8
|
+
import mysql from 'mysql2/promise'
|
|
7
9
|
|
|
8
10
|
|
|
9
11
|
// 数据库配置 模拟环境
|
|
@@ -11,6 +13,7 @@ import mysql from 'mysql2/promise';
|
|
|
11
13
|
*/
|
|
12
14
|
|
|
13
15
|
|
|
16
|
+
|
|
14
17
|
let dbConfig = {
|
|
15
18
|
|
|
16
19
|
host: 'localhost',
|
|
@@ -19,6 +22,16 @@ let dbConfig = {
|
|
|
19
22
|
database: 'my_test_db' // 数据库名
|
|
20
23
|
};
|
|
21
24
|
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 二次封装 defineRoute 函数
|
|
29
|
+
* @param {string} host - 数据库主机地址
|
|
30
|
+
* @param {string} user - 数据库用户名
|
|
31
|
+
* @param {string} password - 数据库密码
|
|
32
|
+
* @param {string} database - 数据库名称
|
|
33
|
+
* */
|
|
34
|
+
|
|
22
35
|
export function setDbConfig(config) {
|
|
23
36
|
dbConfig = { ...dbConfig, ...config };
|
|
24
37
|
}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
2
|
const app = express();
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
let s =['','']
|
|
6
|
+
|
|
3
7
|
import { ToolDb } from '../Databaseope/data.js';
|
|
4
8
|
import {
|
|
5
9
|
validateLogin, verifyUser, generateToken,
|
|
@@ -110,18 +114,7 @@ export function DefineRoutePlus_A(options) {
|
|
|
110
114
|
categoryField = 'category_id',
|
|
111
115
|
|
|
112
116
|
},
|
|
113
|
-
/**
|
|
114
|
-
* Mode: {
|
|
115
|
-
pagination = false, // 是否支持分页
|
|
116
|
-
requireAuth = false, // 是否需要身份验证
|
|
117
|
-
validationMode = '', // 验证模式:login, register
|
|
118
|
-
shuffle = false, // 是否打乱数据
|
|
119
|
-
autoGenerateId = false, // 是否自动生成ID
|
|
120
|
-
pageSize = 10, // 每页大小
|
|
121
|
-
},
|
|
122
117
|
|
|
123
|
-
*
|
|
124
|
-
*/
|
|
125
118
|
|
|
126
119
|
other: {
|
|
127
120
|
customLogic = null, // 自定义逻辑函数
|
|
@@ -200,18 +193,15 @@ export function DefineRoutePlus_A(options) {
|
|
|
200
193
|
// 处理创建操作
|
|
201
194
|
let createData = req.body;
|
|
202
195
|
|
|
203
|
-
//
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
const newId = maxId + 1;
|
|
213
|
-
createData = { [primaryKey]: newId, ...createData };
|
|
214
|
-
}
|
|
196
|
+
// 直接使用自动生成ID的逻辑
|
|
197
|
+
const records = await ToolDb.find(table);
|
|
198
|
+
const ids = Array.isArray(records)
|
|
199
|
+
? records.map(record => record[primaryKey])
|
|
200
|
+
.filter(id => id !== null && id !== undefined && typeof id === 'number')
|
|
201
|
+
: [];
|
|
202
|
+
const maxId = ids.length > 0 ? Math.max(...ids) : 0;
|
|
203
|
+
const newId = maxId + 1;
|
|
204
|
+
createData = { [primaryKey]: newId, ...createData };
|
|
215
205
|
|
|
216
206
|
await ToolDb.create(table, createData);
|
|
217
207
|
break;
|
|
@@ -222,7 +212,7 @@ export function DefineRoutePlus_A(options) {
|
|
|
222
212
|
// 从请求对象中获取查询条件(由categoryFilter中间件设置)
|
|
223
213
|
const where = req.query.where || {};
|
|
224
214
|
|
|
225
|
-
if (pagination) {
|
|
215
|
+
if (req.pagination) {
|
|
226
216
|
// 从请求对象中获取分页参数(由pagination中间件设置)
|
|
227
217
|
const { page, pageSize: size, offset } = req.pagination;
|
|
228
218
|
|
|
@@ -234,7 +224,7 @@ export function DefineRoutePlus_A(options) {
|
|
|
234
224
|
});
|
|
235
225
|
|
|
236
226
|
// 如果需要打乱数据
|
|
237
|
-
if (
|
|
227
|
+
if (req.shuffleArray) {
|
|
238
228
|
data = req.shuffleArray(data);
|
|
239
229
|
}
|
|
240
230
|
|
|
@@ -255,7 +245,7 @@ export function DefineRoutePlus_A(options) {
|
|
|
255
245
|
// 查询所有
|
|
256
246
|
let data = await ToolDb.find(table, where, fields.split(','));
|
|
257
247
|
// 如果需要打乱数据
|
|
258
|
-
if (
|
|
248
|
+
if (req.shuffleArray) {
|
|
259
249
|
data = req.shuffleArray(data);
|
|
260
250
|
}
|
|
261
251
|
result = data;
|
package/types/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import express from 'express';
|
|
4
4
|
|
|
5
5
|
/** 数据库配置接口 */
|
|
6
|
-
export interface
|
|
6
|
+
export interface setDbConfig {
|
|
7
7
|
/** 数据库主机地址 */
|
|
8
8
|
host: string;
|
|
9
9
|
/** 数据库用户名 */
|
|
@@ -125,7 +125,7 @@ export interface ToolDb {
|
|
|
125
125
|
* @param options 路由配置选项
|
|
126
126
|
*
|
|
127
127
|
* /**
|
|
128
|
-
* 二次封装 defineRoute
|
|
128
|
+
* 二次封装 defineRoute
|
|
129
129
|
* @param {Object} options - 路由配置选项
|
|
130
130
|
* @param {string} options.url - 路由路径(必填)
|
|
131
131
|
* @param {string} options.method - HTTP 方法(GET/POST/PUT/DELETE,必填)
|
|
@@ -150,26 +150,7 @@ export interface ToolDb {
|
|
|
150
150
|
* @param {string} [options.PlusHandler.other.errorMessage='操作失败'] - 错误消息(可选)
|
|
151
151
|
*
|
|
152
152
|
*/
|
|
153
|
-
|
|
154
|
-
* 二次封装 defineRoute 函数
|
|
155
|
-
* @param options 路由配置选项
|
|
156
|
-
* @param options.url 路由路径(必填)
|
|
157
|
-
* @param options.method HTTP 方法(GET/POST/PUT/DELETE,必填)
|
|
158
|
-
* @param options.middlewares 中间件数组(可选 用户自定义中间件)
|
|
159
|
-
* @param options.BuiltinMid 内置中间件配置,填一个激活一个
|
|
160
|
-
* @param options.autoGenerateId 自动生成 ID,默认为 false
|
|
161
|
-
* @param options.PlusHandler 额外处理选项(必填)
|
|
162
|
-
* @param options.PlusHandler.db 数据库操作(必填)
|
|
163
|
-
* @param options.PlusHandler.db.table 数据库表名(必填)
|
|
164
|
-
* @param options.PlusHandler.db.operation 数据表操作(必填)select/insert/update/delete
|
|
165
|
-
* @param options.PlusHandler.db.primaryKey 主键字段,默认为 'id'
|
|
166
|
-
* @param options.PlusHandler.db.fields 查询字段,默认为 '*'
|
|
167
|
-
* @param options.PlusHandler.db.categoryField 分类字段,默认为 'category_id'
|
|
168
|
-
* @param options.PlusHandler.other 其他选项(必填)
|
|
169
|
-
* @param options.PlusHandler.other.customLogic 自定义逻辑函数
|
|
170
|
-
* @param options.PlusHandler.other.successMessage 成功消息,默认为 '操作成功'
|
|
171
|
-
* @param options.PlusHandler.other.errorMessage 错误消息,默认为 '操作失败'
|
|
172
|
-
*/
|
|
153
|
+
|
|
173
154
|
export function DefineRoutePlus_A(options: RouteConfig): void;
|
|
174
155
|
|
|
175
156
|
/**
|