|
| 1 | +import { Injectable, Logger, NotFoundException } from '@nestjs/common' |
| 2 | +import { AbstractSchema } from './abstract.schema' |
| 3 | +import { FilterQuery, Model, ProjectionType, QueryOptions, Document, Query, Types, SaveOptions, UpdateQuery, ModifyResult } from 'mongoose' |
| 4 | +import { AbstractService, AbstractServiceContext } from './abstract.service' |
| 5 | +import { ServiceSchemaInterface } from './interfaces/service.schema.interface' |
| 6 | + |
| 7 | +@Injectable() |
| 8 | +export abstract class AbstractServiceSchema extends AbstractService implements ServiceSchemaInterface { |
| 9 | + protected abstract _model: Model<AbstractSchema | Document> |
| 10 | + |
| 11 | + public constructor(context?: AbstractServiceContext) { |
| 12 | + super(context) |
| 13 | + } |
| 14 | + |
| 15 | + public get model(): Model<AbstractSchema | Document> { |
| 16 | + return this._model |
| 17 | + } |
| 18 | + |
| 19 | + public async find<T extends AbstractSchema | Document>( |
| 20 | + filter?: FilterQuery<T>, |
| 21 | + projection?: ProjectionType<T> | null | undefined, |
| 22 | + options?: QueryOptions<T> | null | undefined, |
| 23 | + ): Promise<Query<Array<T>, T, any, T>[]> { |
| 24 | + this.logger.debug(['find', JSON.stringify(Object.values(arguments))].join(' ')) |
| 25 | + return await this._model.find<Query<Array<T>, T, any, T>>(filter, projection, options).exec() |
| 26 | + } |
| 27 | + |
| 28 | + public async count<T extends AbstractSchema | Document>(filter?: FilterQuery<T>, options?: QueryOptions<T>): Promise<number> { |
| 29 | + this.logger.debug(['count', JSON.stringify(Object.values(arguments))].join(' ')) |
| 30 | + return await this._model.countDocuments(filter, options).exec() |
| 31 | + } |
| 32 | + |
| 33 | + public async findAndCount<T extends AbstractSchema | Document>( |
| 34 | + filter?: FilterQuery<T>, |
| 35 | + projection?: ProjectionType<T> | null | undefined, |
| 36 | + options?: QueryOptions<T> | null | undefined, |
| 37 | + ): Promise<[Query<Array<T>, T, any, T>[], number]> { |
| 38 | + this.logger.debug(['findAndCount', JSON.stringify(Object.values(arguments))].join(' ')) |
| 39 | + const count = await this._model.countDocuments(filter, options).exec() |
| 40 | + if (!count) return [[], 0] |
| 41 | + return [await this._model.find<Query<Array<T>, T, any, T>>(filter, projection, options).exec(), count] |
| 42 | + } |
| 43 | + |
| 44 | + public async findById<T extends AbstractSchema | Document>( |
| 45 | + _id: Types.ObjectId | any, |
| 46 | + projection?: ProjectionType<T> | null | undefined, |
| 47 | + options?: QueryOptions<T> | null | undefined, |
| 48 | + ): Promise<Query<T, T, any, T>> { |
| 49 | + this.logger.debug(['findById', JSON.stringify(Object.values(arguments))].join(' ')) |
| 50 | + const data = await this._model.findById<Query<T | null, T, any, T>>(_id, projection, options).exec() |
| 51 | + if (!data) throw new NotFoundException() |
| 52 | + return data |
| 53 | + } |
| 54 | + |
| 55 | + public async findOne<T extends AbstractSchema | Document>( |
| 56 | + filter?: FilterQuery<T>, |
| 57 | + projection?: ProjectionType<T> | null | undefined, |
| 58 | + options?: QueryOptions<T> | null | undefined, |
| 59 | + ): Promise<Query<T, T, any, T>> { |
| 60 | + this.logger.debug(['findOne', JSON.stringify(Object.values(arguments))].join(' ')) |
| 61 | + const data = await this._model.findOne<Query<T | null, T, any, T>>(filter, projection, options).exec() |
| 62 | + if (!data) throw new NotFoundException() |
| 63 | + return data |
| 64 | + } |
| 65 | + |
| 66 | + public async create<T extends AbstractSchema | Document>(data?: any, options?: SaveOptions): Promise<Document<T, any, T>> { |
| 67 | + this.logger.debug(['create', JSON.stringify(Object.values(arguments))].join(' ')) |
| 68 | + const document: Document<T, any, T> = new this._model(data) |
| 69 | + return document.save(options) |
| 70 | + } |
| 71 | + |
| 72 | + public async update<T extends AbstractSchema | Document>( |
| 73 | + _id: Types.ObjectId | any, |
| 74 | + update: UpdateQuery<T>, |
| 75 | + options?: QueryOptions<T> & { rawResult: true }, |
| 76 | + ): Promise<ModifyResult<Query<T, T, any, T>>> { |
| 77 | + this.logger.debug(['update', JSON.stringify(Object.values(arguments))].join(' ')) |
| 78 | + const document = await this._model |
| 79 | + .findByIdAndUpdate<Query<T | null, T, any, T>>({ _id }, update, { |
| 80 | + new: true, |
| 81 | + runValidators: true, |
| 82 | + ...options, |
| 83 | + }) |
| 84 | + .exec() |
| 85 | + if (!document) throw new NotFoundException() |
| 86 | + return document |
| 87 | + } |
| 88 | + |
| 89 | + public async delete<T extends AbstractSchema | Document>( |
| 90 | + _id: Types.ObjectId | any, |
| 91 | + options?: QueryOptions<T> | null | undefined, |
| 92 | + ): Promise<Query<T, T, any, T>> { |
| 93 | + this.logger.debug(['delete', JSON.stringify(Object.values(arguments))].join(' ')) |
| 94 | + const document = await this._model.findByIdAndDelete<Query<T | null, T, any, T>>({ _id }, options).exec() |
| 95 | + if (!document) throw new NotFoundException() |
| 96 | + return document |
| 97 | + } |
| 98 | +} |
0 commit comments