import { BadRequestException , NotFoundException } from '@nestjs/common' ; import { ParsedRequestParams } from '@nestjsx/crud-request' ; import { objKeys } from '@nestjsx/util' ; import { CreateManyDto , CrudRequest , CrudRequestOptions , GetManyDefaultResponse , QueryOptions , } from '../interfaces' ; export abstract class CrudService < T > { abstract getMany ( req ) ; abstract getOne ( req ) ; abstract createOne ( req , dto ) ; abstract createMany ( req , dto ) ; abstract updateOne ( req , dto ) ; abstract replaceOne ( req , dto ) ; abstract deleteOne ( req ) ; abstract recoverOne ( req ) ; throwBadRequestException ( msg ? ) { throw new BadRequestException ( msg ) ; } throwNotFoundException ( name ) { throw new NotFoundException ( `${ name } not found` ) ; } param data param total param limit param offset createPageInfo ( data , total , limit , offset ) { return { data , count : data . length , total , page : limit ? Math . floor ( offset / limit ) + 1 : 1 , pageCount : limit && total ? Math . ceil ( total / limit ) : 1 , } ; } param parsed param options decidePagination ( parsed , options ) { return ( options . query . alwaysPaginate || ( ( Number . isFinite ( parsed . page ) || Number . isFinite ( parsed . offset ) ) && ! ! this . getTake ( parsed , options . query ) ) ) ; } param query param options getTake ( query , options ) { if ( query . limit ) { return options . maxLimit ? query . limit <= options . maxLimit ? query . limit : options . maxLimit : query . limit ; } if ( options . limit ) { return options . maxLimit ? options . limit <= options . maxLimit ? options . limit : options . maxLimit : options . limit ; } return options . maxLimit ? options . maxLimit : null ; } param query param take getSkip ( query , take ) { return query . page && take ? take * ( query . page - 1 ) : query . offset ? query . offset : null ; } param options getPrimaryParams ( options ) { const params = objKeys ( options . params ) . filter ( ( n ) => options . params [ n ] && options . params [ n ] . primary ) ; return params . map ( ( p ) => options . params [ p ] . field ) ; } }