chanjs 1.0.41 → 1.0.43

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/helper.js CHANGED
@@ -40,7 +40,6 @@ exports.createKnex = function(opt) {
40
40
  password:config.password,
41
41
  database:config.database,
42
42
  charset:config.charset,
43
- filename:config.filename
44
43
  },
45
44
  debug: config.debug,
46
45
  //默认为{min: 2, max: 10}
@@ -0,0 +1,8 @@
1
+ const express = require("express");
2
+ module.exports = (app)=>{
3
+ app.plus = {
4
+ setStatic:function ({prefix,dir,maxAge}) {
5
+ app.use(prefix, express.static(dir, { maxAge: maxAge || 0 }));
6
+ }
7
+ }
8
+ }
@@ -8,15 +8,6 @@ class BaseService {
8
8
  this.model = ''; // 默认为空字符串
9
9
  }
10
10
 
11
- /**
12
- * @description 查找所有符合条件的记录
13
- * @param {*} query - 查询条件,例如:{name: 'test', age: 18}
14
- * @returns {Promise} 返回符合条件的记录
15
- */
16
- query(query) {
17
- return this.knex(this.model).where(query);
18
- }
19
-
20
11
  /**
21
12
  * @description 查询表所有记录,慎用
22
13
  * @returns {Promise} 返回所有记录
@@ -25,165 +16,149 @@ class BaseService {
25
16
  return this.knex(this.model).select();
26
17
  }
27
18
 
28
- /**
29
- * @description 查询指定id的记录
30
- * @param {*} id
31
- * @returns {Promise} 返回指定id的记录
32
- */
33
- findById(id) {
34
- return this.knex(this.model).where("id", id).first();
35
- }
36
-
37
- /**
38
- * @description 查询分页数据
39
- * @param {*} current
40
- * @param {*} pageSize
41
- * @param {*} query
42
- * @returns {Promise} 返回分页数据
43
- */
44
- async findListAndQuery(current = 1, pageSize = 10, query = {}) {
45
- const offset = (current - 1) * pageSize;
46
-
47
- let countQuery = this.knex(this.model).count("* as total");
48
- let dataQuery = this.knex(this.model);
49
-
50
- // 应用查询条件
51
- for (const key in query) {
52
- dataQuery = dataQuery.where(key, query[key]);
53
- countQuery = countQuery.where(key, query[key]);
54
- }
55
-
56
- // 获取总记录数
57
- const total = await countQuery.first();
58
-
59
- // 获取分页数据
60
- const data = await dataQuery.offset(offset).limit(pageSize);
19
+ /**
20
+ * @description 根据指定条件查询记录
21
+ * @param {Object} options - 包含查询参数的对象
22
+ * @param {string} options.id - 查询字段名,默认为 'id'
23
+ * @param {*} options.value - 查询字段值,默认为 0
24
+ * @param {Array} options.field - 需要返回的字段数组,默认为空(即选择所有字段)
25
+ * @param {number} options.len - 期望获取的记录数量,默认为 1(如果为 1 则使用 `.first()`,否则使用 `.limit(len)`)
26
+ * @returns {Promise} 返回匹配条件的记录或记录列表
27
+ */
28
+ findById({ id = "id", value = 0, field = [], len = 1 }) {
29
+ let dataQuery = this.knex(this.model).where(id, value);
61
30
 
62
- return { data, total: parseInt(total.total, 10) };
31
+ // 字段筛选
32
+ if (field.length > 0) {
33
+ dataQuery = dataQuery.select(field);
63
34
  }
64
35
 
65
- /**
66
- * @description 查询指定id的记录
67
- * @param {*} id - 记录id
68
- * @returns {Promise} 返回指定id的记录
69
- */
70
- findOneById(id) {
71
- return this.knex(this.model).where("id", id).select();
36
+ // 根据len决定是获取单条记录还是多条记录
37
+ if (len === 1) {
38
+ dataQuery = dataQuery.first();
39
+ } else if (len > 1) {
40
+ dataQuery = dataQuery.limit(len);
72
41
  }
73
42
 
74
- /**
75
- * @description 查询指定条件的记录
76
- * @param {Object} query - {<key>:<value>}
77
- * @returns {Promise} 返回指定条件的记录
78
- */
79
- findOne(query) {
80
- return this.knex(this.model).where(query).first();
81
- }
43
+ return dataQuery;
44
+ }
82
45
 
83
- /**
84
- * @description 更新指定id的记录
85
- * @param {*} id - 记录id
86
- * @param {*} params - {<key>:<value>}
87
- * @returns {Promise} 返回更新后的记录
88
- */
89
- findOneAndUpdate(id, params) {
90
- return this.knex(this.model).where("id", id).update(params).returning("*");
91
- }
92
46
 
93
- /**
94
- * @description 查询指定条件的记录数量
95
- * @param {*} query - {<key>:<value>}
96
- * @returns {Promise} 返回指定条件的记录数量
97
- */
98
- count(query) {
99
- return this.knex(this.model).where(query).count("* as total").first();
100
- }
101
-
102
- /**
103
- * @description 插入一条记录
104
- * @param {*} params - {<key>:<value>}
105
- * @returns {Promise} 返回插入后的记录
106
- */
107
- insert(params) {
108
- return this.knex(this.model).insert(params);
47
+ /**
48
+ * @description 创建新记录
49
+ * @param {Object} data - 包含要插入的数据对象
50
+ * @returns {Promise<boolean>} 返回操作是否成功
51
+ */
52
+ async insert(data = {}) {
53
+ if (Object.keys(data).length === 0) {
54
+ return false;
109
55
  }
56
+ const result = await this.knex(this.model).insert(data);
57
+ return result?.length > 0 || !!result;
58
+ }
110
59
 
111
60
  /**
112
- * @description 插入多条记录
113
- * @param {*} records - [{<key>:<value>}, {<key>:<value>}, ...]
114
- * @returns {Promise} 返回插入后的记录
115
- */
116
- insertMany(records) {
117
- return this.knex(this.model).insert(records);
61
+ * @description 插入多条记录
62
+ * @param {Array} records - 包含要插入的数据对象数组 [{<key>:<value>}, {<key>:<value>}, ...]
63
+ * @returns {Promise<Array|Number>} 返回插入后的记录或受影响行数,具体取决于数据库驱动
64
+ */
65
+ async insertMany(records=[]) {
66
+ // 如果没有提供数据或者数据为空,则直接返回 false 或者抛出异常
67
+ if (records.length === 0) {
68
+ console.log('插入数据不能为空')
69
+ return false;
70
+ }
71
+
72
+ // 执行插入操作并获取结果
73
+ const result = await this.knex(this.model).insert(records);
74
+
75
+ // 返回插入的结果
76
+ return result;
77
+ }
78
+
79
+ /**
80
+ * @description 根据指定条件删除记录
81
+ * @param {Object} query - 包含查询条件的对象
82
+ * @returns {Promise<boolean>} 返回操作是否成功(即是否有任何记录被删除)
83
+ */
84
+ async delete(query = {}) {
85
+ if (Object.keys(query).length === 0) {
86
+ return false;
87
+ }
88
+ const affectedRows = await this.knex(this.model).where(query).del();
89
+ return affectedRows > 0;
90
+ }
91
+ /**
92
+ * @description 根据指定条件更新记录
93
+ * @param {Object} query - 包含查询条件的对象
94
+ * @param {Object} updateParams - 包含要更新的内容对象
95
+ * @returns {Promise<boolean>} 返回操作是否成功
96
+ */
97
+ async update({query={}, updateParams={}}) {
98
+ if (Object.keys(query).length === 0) {
99
+ return false;
118
100
  }
119
101
 
120
- /**
121
- * @description 更新指定id的记录
122
- * @param {*} id - 记录id
123
- * @param {*} params - {<key>:<value>}
124
- * @returns {Promise} 返回更新后的记录
125
- */
126
- updateById(id, params) {
127
- return this.knex(this.model).where("id", id).update(params);
102
+ if (Object.keys(updateParams).length === 0) {
103
+ return false;
128
104
  }
129
105
 
130
- /**
131
- * @description 更新数据
132
- * @param {*} query - {<key>:<value>}
133
- * @param {*} update - {<key>:<value>}
134
- * @returns {Promise<*>} - 返回更新后的数据
135
- */
136
- update(query, update) {
137
- return this.knex(this.model).where(query).update(update);
138
- }
106
+ const result = await this.knex(this.model).where(query).update(updateParams);
107
+ return result > 0;
108
+ }
139
109
 
140
- /**
141
- * @description 查询并更新
142
- * @param {*} query - {<key>:<value>}
143
- * @param {*} update - {<key>:<value>}
144
- * @returns {Promise<*>} - 返回更新后的记录
145
- */
146
- findAndModify(query, update) {
147
- return this.knex(this.model).where(query).update(update);
148
- }
149
110
 
150
- /**
151
- *
152
- * @param {*} id - id
153
- * @param {*} update - {<key>:<value>}
154
- * @returns {Promise<*>} - 返回更新后的记录
155
- */
156
- findByIdAndUpdate(id, update) {
157
- return this.knex(this.model).where("id", id).update(update);
158
- }
159
111
 
160
- /**
161
- * @description 根据条件更新记录
162
- * @param {*} query - {<key>:<value>}
163
- * @param {*} update - {<key>:<value>}
164
- * @returns {Promise} - 返回更新后的记录
165
- */
166
- findOneAndUpdate(query, update) {
167
- return this.knex(this.model).where(query).update(update);
168
- }
112
+ /**
113
+ * 查找所有符合条件的记录,并提供分页信息。
114
+ * @param {Object} options - 包含查询参数的对象
115
+ * @param {number} options.current - 当前页码,默认第一页
116
+ * @param {number} options.pageSize - 每页大小,默认10条记录
117
+ * @param {Object} options.query - 查询条件
118
+ * @param {Array} options.field - 需要返回的字段
119
+ * @returns {Promise<Object>} 返回包含数据列表、总记录数、当前页、每页大小的对象
120
+ */
121
+ async query({current = 1, pageSize = 10, query = {},field = []}) {
122
+ const offset = (current - 1) * pageSize;
123
+ let countQuery = this.knex(this.model).count("* as total");
124
+ let dataQuery = this.knex(this.model);
125
+ // 应用查询条件
126
+ if(Object.keys(query).length > 0){
127
+ Object.entries(query).forEach(([key, value]) => {
128
+ dataQuery = dataQuery.where(key, value);
129
+ countQuery = countQuery.where(key,value);
130
+ })
131
+ }
132
+
133
+ //字段筛选
134
+ if(field.length > 0){
135
+ dataQuery = dataQuery.select(field);
136
+ }
137
+
138
+ //并行执行获取总记录数和分页数据的查询
139
+ const [totalResult, data] = await Promise.all([countQuery.first(), dataQuery.offset(offset).limit(pageSize)]);
140
+ // 提取总记录数
141
+ const total = totalResult?.total || 0;
142
+ return { data, total, current, pageSize };
143
+ }
169
144
 
170
- /**
171
- * @description 根据id删除一条记录
172
- * @param {*} id - 记录id
173
- * @returns {Promise} - 返回删除的记录
174
- */
175
- findByIdAndRemove(id) {
176
- return this.knex(this.model).where("id", id).del();
177
- }
178
145
 
179
- /**
180
- * @description 根据条件删除一条记录
181
- * @param {*} query
182
- * @returns {Promise} - 返回删除的记录
183
- */
184
- findOneAndRemove(query) {
185
- return this.knex(this.model).where(query).del();
186
- }
146
+ /**
147
+ * @description 查询指定条件的记录数量
148
+ * @param {Array} query - 数组形式的多个条件 [{<key>:<value>}, {<key>:<value>}, ...]
149
+ * @returns {Promise<number>} 返回符合条件的记录数量
150
+ */
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
+ });
157
+ }
158
+ const result = await dataQuery.count("* as total").first();
159
+ return result.total || 0;
160
+ }
161
+
187
162
  }
188
163
 
189
164
  module.exports = BaseService;
package/core/lib/view.js CHANGED
@@ -22,10 +22,8 @@ template.defaults.imports.dateFormat = function (date, format) {
22
22
 
23
23
  module.exports = (app, config) => {
24
24
  const { APP_PATH, views, env } = config;
25
- //默认home view
26
- const home = path.join(APP_PATH, `modules/web/view`);
27
- //合并插件中的view
28
- const all = [...views, home];
25
+ //合并插件中的view
26
+ const all = [...views, 'app/modules/web/view'];
29
27
 
30
28
  app.set("view options", {
31
29
  debug: env === "dev",
package/index.js CHANGED
@@ -8,6 +8,7 @@ const Controller = require("./core/lib/controller.js");
8
8
  const Service = require("./core/lib/service.js");
9
9
  const cache = require('./core/lib/cache.js');
10
10
  const core = require("./core/lib/index.js");
11
+ const extend = require("./core/lib/extend.js");
11
12
  /**
12
13
  * @description 基于express封装的mvc框架,遵循约定优于配置原则
13
14
  */
@@ -25,6 +26,7 @@ class Chan {
25
26
 
26
27
  init() {
27
28
  this.app = express();
29
+ extend(this.app);
28
30
  this.router = express.Router();
29
31
  this.loadConfig();
30
32
  this.loadExtends();
@@ -33,6 +35,8 @@ class Chan {
33
35
  this.loadCors();
34
36
  }
35
37
 
38
+
39
+
36
40
  loadConfig() {
37
41
  const configPath = path.join(Chan.config.APP_PATH, "config/index.js");
38
42
  if (fs.existsSync(configPath)) {
@@ -83,8 +87,8 @@ class Chan {
83
87
  }
84
88
  //启动
85
89
  start(cb) {
86
- this.loadPlugins();
87
90
  this.loadModules();
91
+ this.loadPlugins();
88
92
  this.loadCommonRouter();
89
93
  cb && cb();
90
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chanjs",
3
- "version": "1.0.41",
3
+ "version": "1.0.43",
4
4
  "description": "chanjs基于express 纯js研发的轻量级mvc框架。",
5
5
  "main": "index.js",
6
6
  "module": "index.js",