chanjs 1.0.44 → 1.0.46
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/core/lib/service.js +70 -30
- package/index.js +8 -0
- package/package.json +1 -1
package/core/lib/service.js
CHANGED
@@ -10,33 +10,42 @@ class BaseService {
|
|
10
10
|
|
11
11
|
/**
|
12
12
|
* @description 查询表所有记录,慎用
|
13
|
+
* @param {Object} query - 包含查询参数的对象
|
13
14
|
* @returns {Promise} 返回所有记录
|
14
15
|
*/
|
15
|
-
all() {
|
16
|
-
|
16
|
+
all(query={}) {
|
17
|
+
if(Object.keys(query).length === 0) {
|
18
|
+
return this.knex(this.model).select();
|
19
|
+
}
|
20
|
+
return this.knex(this.model).where(query).select();
|
17
21
|
}
|
18
22
|
|
19
23
|
/**
|
20
24
|
* @description 根据指定条件查询记录
|
21
25
|
* @param {Object} options - 包含查询参数的对象
|
22
26
|
* @param {string} options.id - 查询字段名,默认为 'id'
|
23
|
-
* @param {*} options.value - 查询字段值,默认为
|
24
|
-
* @param {Array
|
25
|
-
* @param {number} options.len - 期望获取的记录数量,默认为 1
|
26
|
-
* @returns {Promise
|
27
|
+
* @param {*} options.value - 查询字段值,默认为 0
|
28
|
+
* @param {Array} options.field - 需要返回的字段数组,默认为空(即选择所有字段)
|
29
|
+
* @param {number} options.len - 期望获取的记录数量,默认为 1(如果为 1 则使用 `.first()`,否则使用 `.limit(len)`)
|
30
|
+
* @returns {Promise} 返回匹配条件的记录或记录列表
|
27
31
|
*/
|
28
|
-
async findById({
|
29
|
-
let dataQuery = this.knex(this.model).where(
|
32
|
+
async findById({ query, field = [], len = 1 }) {
|
33
|
+
let dataQuery = this.knex(this.model).where(query);
|
34
|
+
|
35
|
+
// 字段筛选
|
30
36
|
if (field.length > 0) {
|
31
|
-
dataQuery = dataQuery.select(
|
37
|
+
dataQuery = dataQuery.select(field);
|
32
38
|
}
|
39
|
+
|
40
|
+
// 根据len决定是获取单条记录还是多条记录
|
33
41
|
if (len === 1) {
|
34
42
|
dataQuery = dataQuery.first();
|
35
43
|
} else if (len > 1) {
|
36
44
|
dataQuery = dataQuery.limit(len);
|
37
45
|
}
|
38
|
-
|
39
|
-
|
46
|
+
let res = await dataQuery;
|
47
|
+
//返回结果 undefined 则返回空数组或空对象
|
48
|
+
return res || (len === 1 ? {} : []);
|
40
49
|
}
|
41
50
|
|
42
51
|
|
@@ -86,25 +95,55 @@ async insert(data = {}) {
|
|
86
95
|
}
|
87
96
|
/**
|
88
97
|
* @description 根据指定条件更新记录
|
89
|
-
* @param {Object} query -
|
90
|
-
* @param {
|
98
|
+
* @param {Object} query - 查询条件
|
99
|
+
* @param {number} params - 保存内容
|
91
100
|
* @returns {Promise<boolean>} 返回操作是否成功
|
92
101
|
*/
|
93
|
-
async
|
94
|
-
|
95
|
-
return
|
96
|
-
|
102
|
+
async update({query, params} = {}) {
|
103
|
+
const result = await this.knex(this.model).where(query).update(params);
|
104
|
+
return { affectedRows: result };
|
105
|
+
}
|
97
106
|
|
98
|
-
|
99
|
-
|
107
|
+
|
108
|
+
/**
|
109
|
+
* @description 批量更新多条记录(基于事务保证原子性)
|
110
|
+
* @param {Array<{query: Object, params: Object}>} updates - 更新操作数组,每个元素包含查询条件和更新内容
|
111
|
+
* @returns {Promise<{ affectedRows: number[] }>} 返回每个操作影响的行数数组
|
112
|
+
*/
|
113
|
+
async updateMany(updates = []) {
|
114
|
+
// 参数合法性校验
|
115
|
+
if (!Array.isArray(updates)) {
|
116
|
+
throw new Error('参数必须为数组格式');
|
100
117
|
}
|
101
118
|
|
102
|
-
|
103
|
-
|
119
|
+
// 获取事务对象
|
120
|
+
const trx = await this.knex.transaction();
|
121
|
+
|
122
|
+
try {
|
123
|
+
const affectedRows = [];
|
124
|
+
// 循环处理每个更新操作
|
125
|
+
for (const { query, params } of updates) {
|
126
|
+
// 执行单个更新操作,使用事务对象替换原有knex实例
|
127
|
+
const result = await trx(this.model)
|
128
|
+
.where(query)
|
129
|
+
.update(params);
|
130
|
+
affectedRows.push(result);
|
131
|
+
}
|
132
|
+
// 提交事务
|
133
|
+
await trx.commit();
|
134
|
+
// 返回影响行数数组(与入参顺序一致)
|
135
|
+
return { affectedRows };
|
136
|
+
} catch (err) {
|
137
|
+
// 回滚事务
|
138
|
+
await trx.rollback();
|
139
|
+
// 错误向上抛出,由调用者处理
|
140
|
+
throw err;
|
141
|
+
}
|
104
142
|
}
|
105
143
|
|
106
144
|
|
107
145
|
|
146
|
+
|
108
147
|
/**
|
109
148
|
* 查找所有符合条件的记录,并提供分页信息。
|
110
149
|
* @param {Object} options - 包含查询参数的对象
|
@@ -135,6 +174,7 @@ async update({query={}, params={}}) {
|
|
135
174
|
const [totalResult, list] = await Promise.all([countQuery.first(), dataQuery.offset(offset).limit(pageSize)]);
|
136
175
|
// 提取总记录数
|
137
176
|
const total = totalResult?.total || 0;
|
177
|
+
|
138
178
|
return { list, total, current, pageSize };
|
139
179
|
}
|
140
180
|
|
@@ -144,16 +184,16 @@ async update({query={}, params={}}) {
|
|
144
184
|
* @param {Array} query - 数组形式的多个条件 [{<key>:<value>}, {<key>:<value>}, ...]
|
145
185
|
* @returns {Promise<number>} 返回符合条件的记录数量
|
146
186
|
*/
|
147
|
-
async count(query=[]) {
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
187
|
+
async count(query=[]) {
|
188
|
+
let dataQuery = this.knex(this.model);
|
189
|
+
if(query.length >0){
|
190
|
+
query.forEach((condition) => {
|
191
|
+
dataQuery = dataQuery.where(condition);
|
192
|
+
});
|
193
|
+
}
|
194
|
+
const result = await dataQuery.count("* as total").first();
|
195
|
+
return result.total || 0;
|
153
196
|
}
|
154
|
-
const result = await dataQuery.count("* as total").first();
|
155
|
-
return result.total || 0;
|
156
|
-
}
|
157
197
|
|
158
198
|
}
|
159
199
|
|
package/index.js
CHANGED
@@ -25,6 +25,7 @@ class Chan {
|
|
25
25
|
}
|
26
26
|
|
27
27
|
init() {
|
28
|
+
const startTime = performance.now();
|
28
29
|
this.app = express();
|
29
30
|
extend(this.app);
|
30
31
|
this.router = express.Router();
|
@@ -33,6 +34,9 @@ class Chan {
|
|
33
34
|
this.loadCore();
|
34
35
|
this.loadKnex();
|
35
36
|
this.loadCors();
|
37
|
+
// 记录结束时间
|
38
|
+
const endTime = performance.now();
|
39
|
+
console.log(`Chanjs init: ${endTime - startTime} ms`);
|
36
40
|
}
|
37
41
|
|
38
42
|
|
@@ -87,9 +91,13 @@ class Chan {
|
|
87
91
|
}
|
88
92
|
//启动
|
89
93
|
start(cb) {
|
94
|
+
const startTime = performance.now();
|
90
95
|
this.loadModules();
|
91
96
|
this.loadPlugins();
|
92
97
|
this.loadCommonRouter();
|
98
|
+
// 记录结束时间
|
99
|
+
const endTime = performance.now();
|
100
|
+
console.log(`Chanjs load modules: ${endTime - startTime} ms`);
|
93
101
|
cb && cb();
|
94
102
|
}
|
95
103
|
|