/* * @author gs * @date 2020/07/29 14:53 * @modified-user songxiwen * @modified-date 2020/12/13 11:16 * @modified-description 修改查询条件参数类型 */ import { CreateQuery, FilterQuery, Model, MongooseFilterQuery, Types, UpdateQuery } from 'mongoose'; import { BaseModel } from './base.model'; import { databaseDefaultValue } from '../constant'; import { ServerError } from '../common/error/server.error'; export abstract class BaseService { protected readonly model: Model; protected constructor(model: Model) { this.model = model; } public async create(doc: CreateQuery): Promise { try { return (await this.model.create(doc)) as T; } catch (error) { throw ServerError.code.DatabaseException; } } public async createBatch(docs: CreateQuery[]): Promise { try { return (await this.model.create(docs)) as T[]; } catch (error) { throw ServerError.code.DatabaseException; } } public async deleteById(id: string): Promise { try { return (await this.model .findByIdAndDelete(BaseService.toObjectId(id)) .exec()) as T; } catch (error) { throw ServerError.code.DatabaseException; } } public async deleteByConditions(conditions: MongooseFilterQuery) { try { return (await this.model.deleteMany(conditions).exec()) as T; } catch (error) { throw ServerError.code.DatabaseException; } } public async update(id: string, updatedDoc: UpdateQuery): Promise { try { return (await this.model .findByIdAndUpdate(BaseService.toObjectId(id), updatedDoc) .exec()) as T; } catch (error) { throw ServerError.code.DatabaseException; } } public async updateOrInsert( filter: any = {}, updatedDoc: UpdateQuery ): Promise { try { return (await this.model .findOneAndUpdate(filter, updatedDoc, { new: true, upsert: true }) .exec()) as T; } catch (error) { throw ServerError.code.DatabaseException; } } public async updateByConditions( conditions: FilterQuery, doc: UpdateQuery ) { try { return (await this.model.updateMany(conditions, doc).exec()) as T; } catch (error) { throw ServerError.code.DatabaseException; } } public async findById(id: string): Promise { try { return (await this.model .findById(BaseService.toObjectId(id)) .exec()) as T; } catch (error) { throw ServerError.code.DatabaseException; } } public async find(filter: any = {}): Promise { try { return (await this.model.find(filter).exec()) as T[]; } catch (error) { throw ServerError.code.DatabaseException; } } public async findWithSort(params: { filter: object; sort: { [key: string]: number }; limit: number; }): Promise { try { return (await this.model .find(params.filter) .sort(params.sort) .limit(params.limit) .exec()) as T[]; } catch (error) { throw ServerError.code.DatabaseException; } } public async findOne( filter: any = {}, sort: { [key: string]: number } = {} ): Promise { try { return (await this.model.findOne(filter).sort(sort).exec()) as T; } catch (error) { throw ServerError.code.DatabaseException; } } public async findPageList( param: { filter: any; skip: number; limit: number; order?: { [key: string]: number }; } = { filter: {}, skip: databaseDefaultValue.pageId, limit: databaseDefaultValue.pageSize, order: {} } ): Promise { try { return (await this.model .find(param.filter) .sort(param.order) .skip(param.skip) .limit(param.limit) .exec()) as T[]; } catch (error) { throw ServerError.code.DatabaseException; } } public async count(filter: any = {}): Promise { try { return (await this.model.count(filter)) as number; } catch (error) { throw ServerError.code.DatabaseException; } } private static toObjectId(id: string): Types.ObjectId { try { return Types.ObjectId(id); } catch (error) { throw ServerError.code.DatabaseException; } } }