UNPKG

@hymns/mongo-nestjs-utils

Version:
59 lines (46 loc) 1.81 kB
import { Logger, NotFoundException } from '@nestjs/common'; import { FilterQuery, Model, Types, UpdateQuery } from 'mongoose'; import { CreateIndexesOptions } from "mongodb"; import { AbstractDocument } from './abstract.schema'; export abstract class AbstractRepository<TDocument extends AbstractDocument> { protected abstract readonly logger: Logger; constructor(protected readonly model: Model<TDocument>) {} async create(document: Omit<TDocument, '_id'>): Promise<TDocument> { const createdDocument = new this.model({ ...document, _id: new Types.ObjectId(), }); return (await createdDocument.save()).toJSON() as unknown as TDocument; } async findOne(filterQuery: FilterQuery<TDocument>): Promise<TDocument> { const document = await this.model.findOne(filterQuery, {}, { lean: true }); if (!document) { this.logger.warn('Document not found with filterQuery', filterQuery); throw new NotFoundException('Document not found.'); } return document; } async findOneAndUpdate( filterQuery: FilterQuery<TDocument>, update: UpdateQuery<TDocument>, ) { const document = await this.model.findOneAndUpdate(filterQuery, update, { lean: true, new: true, }); if (!document) { this.logger.warn('Document not found with filterQuery', filterQuery); throw new NotFoundException('Document not found.'); } return document; } async find(filterQuery: FilterQuery<TDocument>) { return this.model.find(filterQuery, {}, { lean: true }); } async findOneAndDelete(filterQuery: FilterQuery<TDocument>) { return this.model.findOneAndDelete(filterQuery, { lean: true }); } async createIndex(options: CreateIndexesOptions) { return this.model.createIndexes(options as any); } }