import { processTSQueryAST } from './preprocessor-ts' ; import { processSQLQueryAST } from './preprocessor-sql' ; import { Query as QueryAST } from './loader/sql' ; import { parseTSQuery , TSQueryAST } from './loader/typescript' ; export interface IDatabaseConnection { query : ( query : string , bindings : any [ ] ) => Promise < { rows : any [ ] ; } > ; } export class TaggedQuery < TTypePair extends { params : any ; result : any ; } > { public run : ( params : TTypePair [ 'params' ] , dbConnection : IDatabaseConnection ) => Promise < Array < TTypePair [ 'result' ] > > ; private readonly query : TSQueryAST ; constructor ( query ) { this . query = query ; this . run = async ( params , connection ) => { const { query : processedQuery , bindings } = processTSQueryAST ( this . query , params as any ) ; const result = await connection . query ( processedQuery , bindings ) ; return result . rows ; } ; } } interface ITypePair { params : any ; result : any ; } const sql = < TTypePair extends ITypePair > ( stringsArray ) => { const { query } = parseTSQuery ( stringsArray [ 0 ] ) ; return new TaggedQuery < TTypePair > ( query ) ; } ; export class PreparedQuery < TParamType , TResultType > { public run : ( params : TParamType , dbConnection : IDatabaseConnection ) => Promise < Array < TResultType > > ; private readonly query : QueryAST ; constructor ( query ) { this . query = query ; this . run = async ( params , connection ) => { const { query : processedQuery , bindings } = processSQLQueryAST ( this . query , params as any ) ; const result = await connection . query ( processedQuery , bindings ) ; return result . rows ; } ; } } export default sql ;