chanjs 1.0.44 → 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,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
- 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
  /**
20
24
  * @description 根据指定条件查询记录
21
25
  * @param {Object} options - 包含查询参数的对象
22
26
  * @param {string} options.id - 查询字段名,默认为 'id'
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
+ * @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({ id = "id", value = null, 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);
34
+
35
+ // 字段筛选
30
36
  if (field.length > 0) {
31
- dataQuery = dataQuery.select(...field);
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
- const result = await dataQuery;
39
- return result;
46
+ let res = await dataQuery;
47
+ //返回结果 undefined 则返回空数组或空对象
48
+ return res || (len === 1 ? {} : []);
40
49
  }
41
50
 
42
51
 
@@ -86,25 +95,18 @@ async insert(data = {}) {
86
95
  }
87
96
  /**
88
97
  * @description 根据指定条件更新记录
89
- * @param {Object} query - 包含查询条件的对象
90
- * @param {Object} updateParams - 包含要更新的内容对象
98
+ * @param {Object} query - 查询条件
99
+ * @param {number} params - 保存内容
91
100
  * @returns {Promise<boolean>} 返回操作是否成功
92
101
  */
93
- async update({query={}, params={}}) {
94
- if (Object.keys(query).length === 0) {
95
- return false;
96
- }
97
-
98
- if (Object.keys(params).length === 0) {
99
- return false;
100
- }
101
-
102
- const result = await this.knex(this.model).where(query).update(params);
103
- return result > 0;
102
+ async update({query, params} = {}) {
103
+ const result = await this.knex(this.model).where(query).update(params);
104
+ return { affectedRows: result };
104
105
  }
105
106
 
106
107
 
107
108
 
109
+
108
110
  /**
109
111
  * 查找所有符合条件的记录,并提供分页信息。
110
112
  * @param {Object} options - 包含查询参数的对象
@@ -135,6 +137,7 @@ async update({query={}, params={}}) {
135
137
  const [totalResult, list] = await Promise.all([countQuery.first(), dataQuery.offset(offset).limit(pageSize)]);
136
138
  // 提取总记录数
137
139
  const total = totalResult?.total || 0;
140
+
138
141
  return { list, total, current, pageSize };
139
142
  }
140
143
 
@@ -144,16 +147,16 @@ async update({query={}, params={}}) {
144
147
  * @param {Array} query - 数组形式的多个条件 [{<key>:<value>}, {<key>:<value>}, ...]
145
148
  * @returns {Promise<number>} 返回符合条件的记录数量
146
149
  */
147
- async count(query=[]) {
148
- let dataQuery = this.knex(this.model);
149
- if(query.length >0){
150
- query.forEach((condition) => {
151
- dataQuery = dataQuery.where(condition);
152
- });
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;
153
159
  }
154
- const result = await dataQuery.count("* as total").first();
155
- return result.total || 0;
156
- }
157
160
 
158
161
  }
159
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.44",
3
+ "version": "1.0.45",
4
4
  "description": "chanjs基于express 纯js研发的轻量级mvc框架。",
5
5
  "main": "index.js",
6
6
  "module": "index.js",