import { inject , injectable } from 'inversify' ; import * as apid from '../../../api' ; import DropLogFile from '../../db/entities/DropLogFile' ; import Recorded from '../../db/entities/Recorded' ; import Thumbnail from '../../db/entities/Thumbnail' ; import VideoFile from '../../db/entities/VideoFile' ; import IPromiseRetry from '../IPromiseRetry' ; import IDBOperator from './IDBOperator' ; import IDropLogFileDB , { UpdateCntOption } from './IDropLogFileDB' ; @ injectable ( ) export default class DropLogFileDB implements IDropLogFileDB { private op : IDBOperator ; private promieRetry : IPromiseRetry ; constructor ( @ inject ( 'IDBOperator' ) op , @ inject ( 'IPromiseRetry' ) promieRetry ) { this . op = op ; this . promieRetry = promieRetry ; } param items return public async restore ( items ) { const connection = await this . op . getConnection ( ) ; const queryRunner = connection . createQueryRunner ( ) ; await queryRunner . startTransaction ( ) ; let hasError = false ; try { await queryRunner . manager . delete ( Thumbnail , { } ) ; await queryRunner . manager . delete ( VideoFile , { } ) ; await queryRunner . manager . delete ( Recorded , { } ) ; await queryRunner . manager . delete ( DropLogFile , { } ) ; for ( const item of items ) { await queryRunner . manager . insert ( DropLogFile , item ) ; } await queryRunner . commitTransaction ( ) ; } catch ( err ) { console . error ( err ) ; hasError = err ; await queryRunner . rollbackTransaction ( ) ; } finally { await queryRunner . release ( ) ; } if ( hasError ) { throw new Error ( 'restore error' ) ; } } param dropLogFile return public async insertOnce ( dropLogFile ) { const connection = await this . op . getConnection ( ) ; const queryBuilder = connection . createQueryBuilder ( ) . insert ( ) . into ( DropLogFile ) . values ( dropLogFile ) ; const insertedResult = await this . promieRetry . run ( ( ) => { return queryBuilder . execute ( ) ; } ) ; return insertedResult . identifiers [ 0 ] . id ; } param updateOption return public async updateCnt ( updateOption ) { const connection = await this . op . getConnection ( ) ; const queryBuilder = connection . createQueryBuilder ( ) . update ( DropLogFile ) . set ( { errorCnt : updateOption . errorCnt , dropCnt : updateOption . dropCnt , scramblingCnt : updateOption . scramblingCnt , } ) . where ( { id : updateOption . id } ) ; await this . promieRetry . run ( ( ) => { return queryBuilder . execute ( ) ; } ) ; } param dropLogFileId return public async deleteOnce ( dropLogFileId ) { const connection = await this . op . getConnection ( ) ; const queryBuilder = connection . createQueryBuilder ( ) . delete ( ) . from ( DropLogFile ) . where ( { id : dropLogFileId , } ) ; await this . promieRetry . run ( ( ) => { return queryBuilder . execute ( ) ; } ) ; } param dropLogFileId return public async findId ( dropLogFileId ) { const connection = await this . op . getConnection ( ) ; const queryBuilder = connection . getRepository ( DropLogFile ) . createQueryBuilder ( ) . where ( { id : dropLogFileId , } ) ; const result = await this . promieRetry . run ( ( ) => { return queryBuilder . getOne ( ) ; } ) ; return typeof result === 'undefined' ? null : result ; } return public async findAll ( ) { const connection = await this . op . getConnection ( ) ; const queryBuilder = connection . getRepository ( DropLogFile ) . createQueryBuilder ( ) ; return await this . promieRetry . run ( ( ) => { return queryBuilder . getMany ( ) ; } ) ; } }