|
@@ -6,7 +6,8 @@ import _ = require('lodash');
|
|
|
import { FrameworkErrorEnum, ServiceError } from '../error/service.error';
|
|
|
import { GetModel } from '../util/getModel';
|
|
|
import { SearchBase } from '../interface/SearchBase';
|
|
|
-import { pageOptions, resultOptions } from './options';
|
|
|
+import { PageOptions, ResultOptions } from './options';
|
|
|
+import { PopulateOptions } from 'mongoose';
|
|
|
/**
|
|
|
* Service基类,实现了一些基础的crud
|
|
|
*/
|
|
@@ -27,7 +28,7 @@ export abstract class BaseService<T extends AnyParamConstructor<any>> {
|
|
|
* @param {Boolean} populate 是否进行ref关联数据
|
|
|
* @returns {Promise<object>} 返回列表
|
|
|
*/
|
|
|
- async query(filter: SearchBase, pageOptions: pageOptions = {}, resultOptions: resultOptions = { lean: true, populate: true }): Promise<Array<any>> {
|
|
|
+ async query(filter: SearchBase, pageOptions: PageOptions = {}, resultOptions: ResultOptions = { lean: true, populate: true }): Promise<Array<any>> {
|
|
|
const dup = _.cloneDeep(filter.getFilter());
|
|
|
const { lean, populate } = resultOptions;
|
|
|
let refs = [];
|
|
@@ -56,7 +57,7 @@ export abstract class BaseService<T extends AnyParamConstructor<any>> {
|
|
|
* @param {boolean} lean 是否使用JavaScript形式数据
|
|
|
* @returns {object|undefined}
|
|
|
*/
|
|
|
- async fetch(id: string, resultOptions: resultOptions = { lean: true, populate: true }): Promise<object | undefined> {
|
|
|
+ async fetch(id: string, resultOptions: ResultOptions = { lean: true, populate: true }): Promise<object | undefined> {
|
|
|
const { lean, populate } = resultOptions;
|
|
|
let refs = [];
|
|
|
if (populate) refs = this.getRefs();
|
|
@@ -68,7 +69,7 @@ export abstract class BaseService<T extends AnyParamConstructor<any>> {
|
|
|
* @param query 查询条件
|
|
|
* @param resultOptions 结果处理
|
|
|
*/
|
|
|
- async findOne(query: object = {}, resultOptions: resultOptions = { lean: true, populate: true }): Promise<object | undefined> {
|
|
|
+ async findOne(query: object = {}, resultOptions: ResultOptions = { lean: true, populate: true }): Promise<object | undefined> {
|
|
|
const { lean, populate } = resultOptions;
|
|
|
let refs = [];
|
|
|
if (populate) refs = this.getRefs();
|
|
@@ -149,8 +150,8 @@ export abstract class BaseService<T extends AnyParamConstructor<any>> {
|
|
|
/**
|
|
|
* 获取本服务默认表的ref关系
|
|
|
*/
|
|
|
- getRefs(): Array<object> {
|
|
|
- const schema = _.get(this.model, 'schema.tree');
|
|
|
+ getRefs(): Array<PopulateOptions> {
|
|
|
+ const schema: any = _.get(this.model, 'schema.tree');
|
|
|
const refs = [];
|
|
|
for (const key in schema) {
|
|
|
const f = schema[key];
|