|
@@ -6,6 +6,7 @@ import _ = require('lodash');
|
|
import { FrameworkErrorEnum, ServiceError } from '../error/service.error';
|
|
import { FrameworkErrorEnum, ServiceError } from '../error/service.error';
|
|
import { GetModel } from '../util/getModel';
|
|
import { GetModel } from '../util/getModel';
|
|
import { SearchBase } from '../../dist';
|
|
import { SearchBase } from '../../dist';
|
|
|
|
+import { pageOptions, resultOptions } from './options';
|
|
/**
|
|
/**
|
|
* Service基类,实现了一些基础的crud
|
|
* Service基类,实现了一些基础的crud
|
|
*/
|
|
*/
|
|
@@ -26,9 +27,15 @@ export abstract class BaseService<T extends AnyParamConstructor<any>> {
|
|
* @param {Boolean} populate 是否进行ref关联数据
|
|
* @param {Boolean} populate 是否进行ref关联数据
|
|
* @returns {Promise<object>} 返回列表
|
|
* @returns {Promise<object>} 返回列表
|
|
*/
|
|
*/
|
|
- async query(filter: SearchBase, pageOptions: object = {}, lean = true, populate = true): Promise<object> {
|
|
|
|
|
|
+ async query(filter: SearchBase, pageOptions: pageOptions = {}, resultOptions: resultOptions = { lean: true, populate: true }): Promise<object> {
|
|
const dup = _.cloneDeep(filter.getFilter());
|
|
const dup = _.cloneDeep(filter.getFilter());
|
|
- const data = await this.model.find(dup, {}, { ...pageOptions }).lean(lean);
|
|
|
|
|
|
+ const { lean, populate } = resultOptions;
|
|
|
|
+ let refs = [];
|
|
|
|
+ if (populate) refs = this.getRefs();
|
|
|
|
+ const data = await this.model
|
|
|
|
+ .find(dup, {}, { ...pageOptions })
|
|
|
|
+ .populate(refs)
|
|
|
|
+ .lean(lean)
|
|
return data;
|
|
return data;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -49,8 +56,11 @@ export abstract class BaseService<T extends AnyParamConstructor<any>> {
|
|
* @param {boolean} lean 是否使用JavaScript形式数据
|
|
* @param {boolean} lean 是否使用JavaScript形式数据
|
|
* @returns {object|undefined}
|
|
* @returns {object|undefined}
|
|
*/
|
|
*/
|
|
- async fetch(id: string, lean = true): Promise<object | undefined> {
|
|
|
|
- const data = await this.model.findById(id).lean(lean);
|
|
|
|
|
|
+ async fetch(id: string, resultOptions: resultOptions = { lean: true, populate: true }): Promise<object | undefined> {
|
|
|
|
+ const { lean, populate } = resultOptions;
|
|
|
|
+ let refs = [];
|
|
|
|
+ if (populate) refs = this.getRefs();
|
|
|
|
+ const data = await this.model.findById(id).populate(refs).lean(lean);
|
|
return data;
|
|
return data;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -127,7 +137,7 @@ export abstract class BaseService<T extends AnyParamConstructor<any>> {
|
|
/**
|
|
/**
|
|
* 获取本服务默认表的ref关系
|
|
* 获取本服务默认表的ref关系
|
|
*/
|
|
*/
|
|
- async getRefs() {
|
|
|
|
|
|
+ getRefs(): Array<object> {
|
|
const schema = _.get(this.model, 'schema.tree');
|
|
const schema = _.get(this.model, 'schema.tree');
|
|
const refs = [];
|
|
const refs = [];
|
|
for (const key in schema) {
|
|
for (const key in schema) {
|