import { DataSource } from 'apollo-datasource' ; import DataLoader from 'dataloader' ; import { Knex } from 'knex' ; import _ from 'lodash' ; import logger from 'common/logger' ; import { aws , es , knex } from 'connectors' ; import { Item , ItemData , TableName } from 'definitions' ; export class BaseService extends DataSource { es : typeof es ; aws : typeof aws ; knex : Knex ; dataloader : DataLoader < string , Item > ; table : TableName ; constructor ( table ) { super ( ) ; this . es = es ; this . knex = knex ; this . table = table ; this . aws = aws ; } baseCount = async ( where ? , table ? ) => { const query = this . knex ( table || this . table ) . count ( ) . first ( ) ; if ( where ) { query . where ( where ) ; } const result = await query ; return parseInt ( result ? ( result . count as string ) : '0' , 10 ) ; } ; /**
     * Find an item by a given id.
     */ baseFindById = async ( id , table ? ) : Promise < any | null > => this . knex . from ( table || this . table ) . where ( { id } ) . first ( ) ; /**
     * Find items by given ids.
     */ baseFindByIds = async ( ids , table ? ) => { let rows = await this . knex . select ( ) . from ( table || this . table ) . whereIn ( 'id' , ids as string [ ] ) ; rows = ids . map ( ( id ) => rows . find ( ( r ) => r . id === id ) ) ; return rows ; } ; /**
     * Find an item by a given uuid.
     *
     */ baseFindByUUID = async ( uuid , table ? ) : Promise < any | null > => { const result = await this . knex . select ( ) . from ( table || this . table ) . where ( 'uuid' , uuid ) ; if ( result && result . length > 0 ) { return result [ 0 ] ; } return null ; } ; /**
     * Find items by given ids.
     */ baseFindByUUIDs = async ( uuids , table ? ) => { let rows = await this . knex . select ( ) . from ( table || this . table ) . whereIn ( 'uuid' , uuids as string [ ] ) ; rows = uuids . map ( ( uuid ) => rows . find ( ( r ) => r . uuid === uuid ) ) ; return rows ; } ; /**
     * Find items by given "where", "offset" and "limit"
     */ baseFind = async ( { table , where , skip , take , } ) => { const query = this . knex . select ( ) . from ( table || this . table ) . orderBy ( 'id' , 'desc' ) ; if ( where ) { query . where ( where ) ; } if ( skip ) { query . offset ( skip ) ; } if ( take || take === 0 ) { query . limit ( take ) ; } return query ; } ; /**
     * Create item
     */ baseCreate = async ( data , table ? ) => { try { const [ result ] = await this . knex ( table || this . table ) . insert ( data ) . returning ( '*' ) ; logger . info ( `Inserted id ${ result . id } to ${ table || this . table }` ) ; return result ; } catch ( err ) { logger . error ( err ) ; throw err ; } } ; /**
     * Create a batch of items
     */ baseBatchCreate = async ( dataItems , table ? ) => this . knex . batchInsert ( table || this . table , dataItems ) . returning ( '*' ) ; /**
     * Create or Update Item
     */ baseUpdateOrCreate = async ( { where , data , table , createOptions , } ) => { const tableName = table || this . table ; const item = await this . knex ( tableName ) . select ( ) . where ( where ) . first ( ) ; if ( ! item ) { let createData = data ; if ( createOptions ) { createData = { ... createData , ... createOptions } ; } return this . baseCreate ( createData , tableName ) ; } const [ updatedItem ] = await this . knex ( tableName ) . where ( where ) . update ( data ) . returning ( '*' ) ; logger . info ( `Updated id ${ updatedItem . id } in ${ tableName }` ) ; return updatedItem ; } ; /**
     * Find or Create Item
     */ baseFindOrCreate = async ( { where , data , table , } ) => { const tableName = table || this . table ; const item = await this . knex ( tableName ) . select ( ) . where ( where ) . first ( ) ; if ( ! item ) { return this . baseCreate ( data , tableName ) ; } return item ; } ; /**
     * Update an item by a given id.
     */ baseUpdate = async ( id , data , table ? ) => { const [ updatedItem ] = await this . knex . where ( 'id' , id ) . update ( data ) . into ( table || this . table ) . returning ( '*' ) ; logger . info ( `Updated id ${ id } in ${ table || this . table }` ) ; return updatedItem ; } ; /**
     * Update a batch of items by given ids.
     */ baseBatchUpdate = async ( ids , data , table ? ) => this . knex . whereIn ( 'id' , ids ) . update ( data ) . into ( table || this . table ) . returning ( '*' ) ; /**
     * Delete an item by a given id.
     */ baseDelete = async ( id , table ? ) => this . knex ( table || this . table ) . where ( { id } ) . del ( ) ; /**
     * Delete a batch of items by  given ids.
     */ baseBatchDelete = async ( ids , table ? ) => this . knex ( table || this . table ) . whereIn ( 'id' , ids ) . del ( ) ; /**
     * Find entity type id by a given type string.
     */ baseFindEntityTypeId = async ( entityType ) => this . knex ( 'entity_type' ) . select ( 'id' ) . where ( { table : entityType } ) . first ( ) ; /**
     * Find entity type table by a given id.
     */ baseFindEntityTypeTable = async ( id ) => this . knex ( 'entity_type' ) . select ( 'table' ) . where ( { id } ) . first ( ) ; }