11// @ts -check
22import * as base from '../utils/base.js'
3+ import dbClientClass from "../utils/database/index.js"
34
45export const command = 'querySimple'
56export const aliases = [ 'qs' , "querysimple" ]
@@ -28,6 +29,11 @@ export const builder = base.getBuilder({
2829 default : "table" ,
2930 type : 'string' ,
3031 desc : base . bundle . getText ( "outputTypeQuery" )
32+ } ,
33+ profile : {
34+ alias : [ 'p' , 'Profile' ] ,
35+ type : 'string' ,
36+ desc : base . bundle . getText ( "profile" )
3137 }
3238} )
3339
@@ -54,6 +60,12 @@ export let inputPrompts = {
5460 description : base . bundle . getText ( "outputTypeQuery" ) ,
5561 type : 'string' ,
5662 required : true
63+ } ,
64+ profile : {
65+ description : base . bundle . getText ( "profile" ) ,
66+ type : 'string' ,
67+ required : false ,
68+ ask : ( ) => { }
5769 }
5870}
5971
@@ -89,10 +101,10 @@ export async function dbQuery(prompts) {
89101 // @ts -ignore
90102 const parser = new AsyncParser ( opts , transformOpts , asyncOpts )
91103 try {
92- base . setPrompts ( prompts )
93- const db = await base . createDBConnection ( )
104+ const dbClient = await dbClientClass . getNewClient ( prompts )
105+ await dbClient . connect ( )
106+ let results = await dbClient . execSQL ( prompts . query )
94107
95- let results = await db . execSQL ( prompts . query )
96108 if ( ! results [ 0 ] ) {
97109 return base . error ( base . bundle . getText ( "errNoResults" ) )
98110 }
@@ -122,27 +134,28 @@ export async function dbQuery(prompts) {
122134 } ] )
123135 await toFile ( prompts . folder , prompts . filename , 'xlsx' , excelOutput )
124136 } else {
125- base . end ( )
126- return base . error ( base . bundle . getText ( "errExcel" ) )
137+ base . error ( base . bundle . getText ( "errExcel" ) )
138+ dbClient . disconnect ( )
139+ return
127140 }
128141 break
129142 case 'json' :
130143 if ( prompts . filename ) {
131144 await toFile ( prompts . folder , prompts . filename , 'json' , JSON . stringify ( results , null , 2 ) )
132145 } else {
133146 console . log ( highlight ( JSON . stringify ( results , null , 2 ) ) )
134- base . end ( )
147+ dbClient . disconnect ( )
135148 return JSON . stringify ( results , null , 2 )
136149 }
137150 break
138- case 'csv' :
151+ case 'csv' :
139152 if ( prompts . filename ) {
140153 const csv = await parser . parse ( results ) . promise ( )
141154 await toFile ( prompts . folder , prompts . filename , 'csv' , csv )
142155 } else {
143156 const csv = await parser . parse ( results ) . promise ( )
144157 console . log ( highlight ( csv ) )
145- base . end ( )
158+ dbClient . disconnect ( )
146159 return csv
147160 }
148161 break
@@ -154,7 +167,7 @@ export async function dbQuery(prompts) {
154167 }
155168 break
156169 }
157- base . end ( )
170+ dbClient . disconnect ( )
158171 return results
159172 } catch ( error ) {
160173 base . error ( error )
0 commit comments