|
@@ -50,7 +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,7 +64,10 @@ class CrudService extends NafService {
|
|
|
sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
|
|
|
}
|
|
|
let condition = cloneDeep(filter);
|
|
|
+ // 分站模式确认
|
|
|
condition = this.dealFilter(condition);
|
|
|
+ const _tenant = this.isMultiTenancy();
|
|
|
+ if (_tenant) condition._tenant = _tenant;
|
|
|
const pipeline = [{ $match: condition }];
|
|
|
// 先排序
|
|
|
if (sort) pipeline.push({ $sort: sort });
|
|
@@ -83,6 +86,9 @@ class CrudService extends NafService {
|
|
|
async count(filter) {
|
|
|
let condition = cloneDeep(filter);
|
|
|
condition = this.dealFilter(condition);
|
|
|
+ // 分站模式确认
|
|
|
+ const _tenant = this.isMultiTenancy();
|
|
|
+ if (_tenant) condition._tenant = _tenant;
|
|
|
let count = 0;
|
|
|
const res = await this.model.aggregate([{ $match: condition }, { $count: 'id' }]);
|
|
|
if (res && isArray(res)) {
|
|
@@ -91,6 +97,15 @@ class CrudService extends NafService {
|
|
|
}
|
|
|
return count;
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 判断默认model是否是分站模式
|
|
|
+ * 是分站模式且不是master,就返回分站标识
|
|
|
+ */
|
|
|
+ isMultiTenancy() {
|
|
|
+ const is_multi = this.model.prototype.schema.options['multi-tenancy'];
|
|
|
+ const tenant = this.model.prototype.schema.options['x-tenant'];
|
|
|
+ if (is_multi && tenant !== 'master') return tenant;
|
|
|
+ }
|
|
|
|
|
|
async queryAndCount(filter, options) {
|
|
|
filter = this.dealFilter(filter);
|