import { Inject, Injectable } from '@nestjs/common';
import { DatabaseName } from '@domain/value-objects/database-name.vo';
import { CollectionName } from '@domain/value-objects/collection-name.vo';
import { DocumentEntity } from '@domain/entities/document.entity';
import {
IMongodbRepository,
MONGODB_REPOSITORY,
} from '@domain/repositories/mongodb.repository.interface';
import { InsertDocumentDto, InsertManyDocumentsDto } from './insert-document.dto';
@Injectable()
export class InsertDocumentUseCase {
constructor(
@Inject(MONGODB_REPOSITORY)
private readonly mongoRepository: IMongodbRepository,
) {}
async execute(dto: InsertDocumentDto): Promise<DocumentEntity> {
const dbName = new DatabaseName(dto.database);
const collName = new CollectionName(dto.collection);
return await this.mongoRepository.insertOne(dbName, collName, dto.document);
}
}
@Injectable()
export class InsertManyDocumentsUseCase {
constructor(
@Inject(MONGODB_REPOSITORY)
private readonly mongoRepository: IMongodbRepository,
) {}
async execute(dto: InsertManyDocumentsDto): Promise<DocumentEntity[]> {
const dbName = new DatabaseName(dto.database);
const collName = new CollectionName(dto.collection);
return await this.mongoRepository.insertMany(dbName, collName, dto.documents);
}
}