chanjs 1.0.43 → 1.0.44
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 +13 -17
- package/package.json +1 -1
package/core/lib/service.js
CHANGED
@@ -20,27 +20,23 @@ class BaseService {
|
|
20
20
|
* @description 根据指定条件查询记录
|
21
21
|
* @param {Object} options - 包含查询参数的对象
|
22
22
|
* @param {string} options.id - 查询字段名,默认为 'id'
|
23
|
-
* @param {*} options.value - 查询字段值,默认为
|
24
|
-
* @param {Array} options.field -
|
25
|
-
* @param {number} options.len - 期望获取的记录数量,默认为 1
|
26
|
-
* @returns {Promise}
|
23
|
+
* @param {*} options.value - 查询字段值,默认为 null
|
24
|
+
* @param {Array<string>} options.field - 需要返回的字段数组,默认为所有字段
|
25
|
+
* @param {number} options.len - 期望获取的记录数量,默认为 1
|
26
|
+
* @returns {Promise<Array|Object|null>} 返回匹配条件的记录或记录列表,若无结果则返回 null
|
27
27
|
*/
|
28
|
-
findById({ id = "id", value =
|
28
|
+
async findById({ id = "id", value = null, field = [], len = 1 }) {
|
29
29
|
let dataQuery = this.knex(this.model).where(id, value);
|
30
|
-
|
31
|
-
// 字段筛选
|
32
30
|
if (field.length > 0) {
|
33
|
-
dataQuery = dataQuery.select(field);
|
31
|
+
dataQuery = dataQuery.select(...field);
|
34
32
|
}
|
35
|
-
|
36
|
-
// 根据len决定是获取单条记录还是多条记录
|
37
33
|
if (len === 1) {
|
38
34
|
dataQuery = dataQuery.first();
|
39
35
|
} else if (len > 1) {
|
40
36
|
dataQuery = dataQuery.limit(len);
|
41
37
|
}
|
42
|
-
|
43
|
-
return
|
38
|
+
const result = await dataQuery;
|
39
|
+
return result;
|
44
40
|
}
|
45
41
|
|
46
42
|
|
@@ -94,16 +90,16 @@ async insert(data = {}) {
|
|
94
90
|
* @param {Object} updateParams - 包含要更新的内容对象
|
95
91
|
* @returns {Promise<boolean>} 返回操作是否成功
|
96
92
|
*/
|
97
|
-
async update({query={},
|
93
|
+
async update({query={}, params={}}) {
|
98
94
|
if (Object.keys(query).length === 0) {
|
99
95
|
return false;
|
100
96
|
}
|
101
97
|
|
102
|
-
if (Object.keys(
|
98
|
+
if (Object.keys(params).length === 0) {
|
103
99
|
return false;
|
104
100
|
}
|
105
101
|
|
106
|
-
const result = await this.knex(this.model).where(query).update(
|
102
|
+
const result = await this.knex(this.model).where(query).update(params);
|
107
103
|
return result > 0;
|
108
104
|
}
|
109
105
|
|
@@ -136,10 +132,10 @@ async update({query={}, updateParams={}}) {
|
|
136
132
|
}
|
137
133
|
|
138
134
|
//并行执行获取总记录数和分页数据的查询
|
139
|
-
const [totalResult,
|
135
|
+
const [totalResult, list] = await Promise.all([countQuery.first(), dataQuery.offset(offset).limit(pageSize)]);
|
140
136
|
// 提取总记录数
|
141
137
|
const total = totalResult?.total || 0;
|
142
|
-
return {
|
138
|
+
return { list, total, current, pageSize };
|
143
139
|
}
|
144
140
|
|
145
141
|
|