chanjs 2.1.0 → 2.1.1

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.
Files changed (2) hide show
  1. package/core/service.js +12 -1
  2. package/package.json +1 -1
package/core/service.js CHANGED
@@ -212,9 +212,10 @@ async find(query = {}) {
212
212
  * @param {number} options.pageSize - 每页条数
213
213
  * @param {Object} options.query - 查询条件
214
214
  * @param {Array} options.field - 返回字段
215
+ * @param {Object} options.sort - 排序条件 { field: 'asc|desc' }
215
216
  * @returns {Promise} 查询结果
216
217
  */
217
- async query({ current = 1, pageSize = 10, query = {}, field = [] }) {
218
+ async query({ current = 1, pageSize = 10, query = {}, field = [], sort = {} }) {
218
219
  try {
219
220
  const size = pageSize; // 如需要可再做非空校验
220
221
  const offset = (current - 1) * size;
@@ -231,6 +232,16 @@ async find(query = {}) {
231
232
 
232
233
  if (field && field.length) dataQuery = dataQuery.select(field);
233
234
 
235
+ // 应用排序(仅支持对象模式)
236
+ if (sort && typeof sort === 'object' && Object.keys(sort).length) {
237
+ for (const [field, dir] of Object.entries(sort)) {
238
+ const direction = ['asc', 'desc'].includes(dir.toLowerCase())
239
+ ? dir.toLowerCase()
240
+ : 'asc';
241
+ dataQuery = dataQuery.orderBy(field, direction);
242
+ }
243
+ }
244
+
234
245
  const [totalResult, list] = await Promise.all([
235
246
  countQuery.first(),
236
247
  dataQuery.offset(offset).limit(size),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "chanjs",
4
- "version": "2.1.0",
4
+ "version": "2.1.1",
5
5
  "description": "chanjs基于express5 纯js研发的轻量级mvc框架。",
6
6
  "main": "index.js",
7
7
  "module": "index.js",