chanjs 1.0.43 → 1.0.45

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.
@@ -10,10 +10,14 @@ class BaseService {
10
10
 
11
11
  /**
12
12
  * @description 查询表所有记录,慎用
13
+ * @param {Object} query - 包含查询参数的对象
13
14
  * @returns {Promise} 返回所有记录
14
15
  */
15
- all() {
16
- return this.knex(this.model).select();
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
  /**
@@ -25,8 +29,8 @@ class BaseService {
25
29
  * @param {number} options.len - 期望获取的记录数量,默认为 1(如果为 1 则使用 `.first()`,否则使用 `.limit(len)`)
26
30
  * @returns {Promise} 返回匹配条件的记录或记录列表
27
31
  */
28
- findById({ id = "id", value = 0, field = [], len = 1 }) {
29
- let dataQuery = this.knex(this.model).where(id, value);
32
+ async findById({ query, field = [], len = 1 }) {
33
+ let dataQuery = this.knex(this.model).where(query);
30
34
 
31
35
  // 字段筛选
32
36
  if (field.length > 0) {
@@ -39,8 +43,9 @@ findById({ id = "id", value = 0, field = [], len = 1 }) {
39
43
  } else if (len > 1) {
40
44
  dataQuery = dataQuery.limit(len);
41
45
  }
42
-
43
- return dataQuery;
46
+ let res = await dataQuery;
47
+ //返回结果 undefined 则返回空数组或空对象
48
+ return res || (len === 1 ? {} : []);
44
49
  }
45
50
 
46
51
 
@@ -90,25 +95,18 @@ async insert(data = {}) {
90
95
  }
91
96
  /**
92
97
  * @description 根据指定条件更新记录
93
- * @param {Object} query - 包含查询条件的对象
94
- * @param {Object} updateParams - 包含要更新的内容对象
98
+ * @param {Object} query - 查询条件
99
+ * @param {number} params - 保存内容
95
100
  * @returns {Promise<boolean>} 返回操作是否成功
96
101
  */
97
- async update({query={}, updateParams={}}) {
98
- if (Object.keys(query).length === 0) {
99
- return false;
100
- }
101
-
102
- if (Object.keys(updateParams).length === 0) {
103
- return false;
104
- }
105
-
106
- const result = await this.knex(this.model).where(query).update(updateParams);
107
- return result > 0;
102
+ async update({query, params} = {}) {
103
+ const result = await this.knex(this.model).where(query).update(params);
104
+ return { affectedRows: result };
108
105
  }
109
106
 
110
107
 
111
108
 
109
+
112
110
  /**
113
111
  * 查找所有符合条件的记录,并提供分页信息。
114
112
  * @param {Object} options - 包含查询参数的对象
@@ -136,10 +134,11 @@ async update({query={}, updateParams={}}) {
136
134
  }
137
135
 
138
136
  //并行执行获取总记录数和分页数据的查询
139
- const [totalResult, data] = await Promise.all([countQuery.first(), dataQuery.offset(offset).limit(pageSize)]);
137
+ const [totalResult, list] = await Promise.all([countQuery.first(), dataQuery.offset(offset).limit(pageSize)]);
140
138
  // 提取总记录数
141
139
  const total = totalResult?.total || 0;
142
- return { data, total, current, pageSize };
140
+
141
+ return { list, total, current, pageSize };
143
142
  }
144
143
 
145
144
 
@@ -148,16 +147,16 @@ async update({query={}, updateParams={}}) {
148
147
  * @param {Array} query - 数组形式的多个条件 [{<key>:<value>}, {<key>:<value>}, ...]
149
148
  * @returns {Promise<number>} 返回符合条件的记录数量
150
149
  */
151
- async count(query=[]) {
152
- let dataQuery = this.knex(this.model);
153
- if(query.length >0){
154
- query.forEach((condition) => {
155
- dataQuery = dataQuery.where(condition);
156
- });
150
+ async count(query=[]) {
151
+ let dataQuery = this.knex(this.model);
152
+ if(query.length >0){
153
+ query.forEach((condition) => {
154
+ dataQuery = dataQuery.where(condition);
155
+ });
156
+ }
157
+ const result = await dataQuery.count("* as total").first();
158
+ return result.total || 0;
157
159
  }
158
- const result = await dataQuery.count("* as total").first();
159
- return result.total || 0;
160
- }
161
160
 
162
161
  }
163
162
 
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chanjs",
3
- "version": "1.0.43",
3
+ "version": "1.0.45",
4
4
  "description": "chanjs基于express 纯js研发的轻量级mvc框架。",
5
5
  "main": "index.js",
6
6
  "module": "index.js",