|
@@ -1,11 +1,11 @@
|
|
|
-"use strict";
|
|
|
+'use strict';
|
|
|
|
|
|
-const { isString, isArray, get } = require("lodash");
|
|
|
-const { isNullOrUndefined, trimData } = require("naf-core").Util;
|
|
|
-const assert = require("assert");
|
|
|
-const { ObjectId } = require("mongoose").Types;
|
|
|
-const { BusinessError, ErrorCode } = require("naf-core").Error;
|
|
|
-const { NafService } = require("./naf-service");
|
|
|
+const { isString, isArray, get } = require('lodash');
|
|
|
+const { isNullOrUndefined, trimData } = require('naf-core').Util;
|
|
|
+const assert = require('assert');
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+const { NafService } = require('./naf-service');
|
|
|
|
|
|
class CrudService extends NafService {
|
|
|
async create(data) {
|
|
@@ -22,8 +22,7 @@ class CrudService extends NafService {
|
|
|
if (_id || id) filter = { _id: ObjectId(_id || id) };
|
|
|
// TODO:检查数据是否存在
|
|
|
const entity = await this.model.findOne(filter).exec();
|
|
|
- if (isNullOrUndefined(entity))
|
|
|
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
|
|
|
+ if (isNullOrUndefined(entity)) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
|
|
|
|
|
|
// TODO: 修改数据
|
|
|
entity.set(trimData(update));
|
|
@@ -39,7 +38,7 @@ class CrudService extends NafService {
|
|
|
} else {
|
|
|
await this.model.deleteMany(filter).exec();
|
|
|
}
|
|
|
- return "deleted";
|
|
|
+ return 'deleted';
|
|
|
}
|
|
|
|
|
|
async fetch(filter, { sort, desc, projection } = {}) {
|
|
@@ -51,9 +50,7 @@ class CrudService extends NafService {
|
|
|
if (sort && isString(sort)) {
|
|
|
sort = { [sort]: desc ? -1 : 1 };
|
|
|
} else if (sort && isArray(sort)) {
|
|
|
- sort = sort
|
|
|
- .map((f) => ({ [f]: desc ? -1 : 1 }))
|
|
|
- .reduce((p, c) => ({ ...p, ...c }), {});
|
|
|
+ sort = sort.map((f) => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
|
|
|
}
|
|
|
|
|
|
return await this.model.findOne(filter, projection).exec();
|
|
@@ -64,14 +61,10 @@ class CrudService extends NafService {
|
|
|
if (sort && isString(sort)) {
|
|
|
sort = { [sort]: desc ? -1 : 1 };
|
|
|
} else if (sort && isArray(sort)) {
|
|
|
- sort = sort
|
|
|
- .map((f) => ({ [f]: desc ? -1 : 1 }))
|
|
|
- .reduce((p, c) => ({ ...p, ...c }), {});
|
|
|
+ sort = sort.map((f) => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
|
|
|
}
|
|
|
filter = this.dealFilter(filter);
|
|
|
- const rs = await this.model
|
|
|
- .find(trimData(filter), projection, { skip, limit, sort })
|
|
|
- .exec();
|
|
|
+ const rs = await this.model.find(trimData(filter), projection, { skip, limit, sort }).exec();
|
|
|
return rs;
|
|
|
}
|
|
|
|
|
@@ -100,7 +93,7 @@ class CrudService extends NafService {
|
|
|
let res = key.match(str);
|
|
|
if (res) {
|
|
|
let newKey = key.slice(1, key.length - 1);
|
|
|
- if(!ObjectId.isValid(filter[key])) filter[newKey] = new RegExp(filter[key]);
|
|
|
+ if (!ObjectId.isValid(filter[key])) filter[newKey] = new RegExp(filter[key]);
|
|
|
delete filter[key];
|
|
|
}
|
|
|
}
|
|
@@ -110,25 +103,27 @@ class CrudService extends NafService {
|
|
|
turnDateRangeQuery(filter) {
|
|
|
const keys = Object.keys(filter);
|
|
|
for (const k of keys) {
|
|
|
- if (k.includes("@")) {
|
|
|
- const karr = k.split("@");
|
|
|
+ if (k.includes('@')) {
|
|
|
+ const karr = k.split('@');
|
|
|
// 因为是针对处理范围日期,所以必须只有,开始时间和结束时间
|
|
|
if (karr.length === 2) {
|
|
|
const type = karr[1];
|
|
|
- if (type === "start")
|
|
|
- if (filter[k] && filter[k] !== "") {
|
|
|
+ if (type === 'start') {
|
|
|
+ if (filter[k] && filter[k] !== '') {
|
|
|
filter[karr[0]] = {
|
|
|
...get(filter, karr[0], {}),
|
|
|
$gte: filter[k],
|
|
|
};
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ } else {
|
|
|
if (filter[k] && filter[k] !== '') {
|
|
|
filter[karr[0]] = {
|
|
|
...get(filter, karr[0], {}),
|
|
|
$lte: filter[k],
|
|
|
};
|
|
|
}
|
|
|
- delete filter[k];
|
|
|
+ delete filter[k];
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|