import { Injectable } from '@nestjs/common'; import { TypeOrmCrudService } from '@nestjsx/crud-typeorm'; import { Category } from '../entities/category.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, Transaction, TransactionRepository } from 'typeorm'; import { CategoryDto } from './category.dto'; @Injectable() export class CategoriesService extends TypeOrmCrudService { constructor( @InjectRepository(Category) public categoriesRepository: Repository, ) { super(categoriesRepository); } async markDeleted(id: number) { await this.categoriesRepository.softDelete({ id }); return; } @Transaction() async bulkUpdate( data: Array>, @TransactionRepository(Category) fr?: Repository, ) { const hasUpdatesWithoutId = !!data.find(d => !('id' in d)); if (hasUpdatesWithoutId) return this.throwBadRequestException('id is required'); const ops = data .map(f => { const change = Object.keys(f).reduce((c, key) => { if (f[key] !== undefined) { c[key] = f[key]; } return c; }, {}); return fr.update(f.id, change); }) .filter(r => r instanceof Promise); return await Promise.all(ops); } }