/* eslint-disable import/no-extraneous-dependencies */ import { CdmLogger } from '@cdm-logger/core'; import { inject, injectable, optional } from 'inversify'; import { BaseMongoRepository } from '@common-stack/store-mongo'; import mongoose from 'mongoose'; import { IFileInfoModel, AsDomainType, IFileInfoRepository } from 'common/server'; import { FileInfoModelFunc } from '../models'; @injectable() export class FileInfoRepository extends BaseMongoRepository implements IFileInfoRepository { constructor( @inject('MongoDBConnection') db: mongoose.Connection, @inject('Logger') logger: CdmLogger.ILogger, @inject('MongoOptions') @optional() options?: any, ) { super(FileInfoModelFunc as any, db, logger, options); this.logger = logger.child({ className: 'FileInfoRepository' }); } public async findByUrl(url: string): Promise | null> { this.logger.trace('findByUrl with params (%j)', { url }); const result = await this.model.findOne({ url }).exec(); return (result as AsDomainType) || null; } public async findByReference(ref: string, refType: string): Promise[]> { this.logger.trace('findByReference with params (%j)', { ref, refType }); const result = await this.model.find({ ref, refType }).exec(); return (result as AsDomainType[]) || []; } public async findByCreator(createdBy: string): Promise[]> { this.logger.trace('findByCreator with params (%j)', { createdBy }); const result = await this.model.find({ createdBy }).exec(); return (result as AsDomainType[]) || []; } public async deleteByReference(ref: string, refType: string): Promise { this.logger.trace('deleteByReference with params (%j)', { ref, refType }); const result = await this.model.deleteMany({ ref, refType }).exec(); return result.deletedCount > 0; } }