import { Injectable } from '@nestjs/common' ; import { Pool } from 'pg' ; import { Config } from '../configure' ; @ Injectable ( ) export class ProjectService { constructor ( private readonly pool , private readonly config ) { } async getProjectSchema ( name ) { const result = await this . pool . query < { schema_name : string ; } , any [ ] > ( `SELECT schema_name FROM information_schema.schemata` ) . then ( ( obj ) => obj . rows . map ( ( x ) => x . schema_name ) ) . catch ( ( e ) => { throw new Error ( `Unable to fetch all database schemas: ${ e }` ) ; } ) ; if ( result . includes ( name ) ) { return name ; } else { const { rows } = await this . pool . query ( `SELECT *
        FROM public.subqueries
        WHERE name = $1` , [ name ] ) ; if ( rows . length === 0 ) { throw new Error ( `unknown project name ${ this . config . get ( 'name' ) }` ) ; } return rows [ 0 ] . db_schema ; } } }