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.
- package/core/service.js +12 -1
- 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),
|