chanjs 2.0.20 → 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/helper/ip.js +0 -2
- package/index.js +3 -3
- 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/helper/ip.js
CHANGED
|
@@ -6,8 +6,6 @@
|
|
|
6
6
|
export const getIp = (req) => {
|
|
7
7
|
// 优先级从高到低获取可能的IP来源
|
|
8
8
|
const ipSources = [
|
|
9
|
-
req.headers["x-forwarded-for"], // 代理场景下的真实IP(可能多个,逗号分隔)
|
|
10
|
-
req.headers["x-real-ip"], // 部分代理服务器使用
|
|
11
9
|
req.ip, // Express内置的IP获取(已处理代理)
|
|
12
10
|
req.connection?.remoteAddress, // 底层连接的远程地址
|
|
13
11
|
req.socket?.remoteAddress, // 套接字的远程地址
|
package/index.js
CHANGED
|
@@ -67,9 +67,10 @@ class Chan {
|
|
|
67
67
|
statics,
|
|
68
68
|
logger,
|
|
69
69
|
cors,
|
|
70
|
+
PROXY,
|
|
70
71
|
} = Chan.config;
|
|
71
72
|
|
|
72
|
-
this.app.set("trust proxy", true);
|
|
73
|
+
this.app.set("trust proxy", PROXY === "true" ? true : false);
|
|
73
74
|
log(this.app, logger);
|
|
74
75
|
setFavicon(this.app);
|
|
75
76
|
setCookie(this.app, cookieKey);
|
|
@@ -166,11 +167,10 @@ class Chan {
|
|
|
166
167
|
path: r.route.path,
|
|
167
168
|
methods: Object.keys(r.route.methods),
|
|
168
169
|
}));
|
|
169
|
-
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
run(cb) {
|
|
173
|
-
const port = parseInt(
|
|
173
|
+
const port = parseInt(Chan.config.PORT) || 3000;
|
|
174
174
|
this.app.listen(port, () => {
|
|
175
175
|
cb ? cb(port) : console.log(`Server is running on port ${port}`);
|
|
176
176
|
});
|