@@ -17,14 +17,15 @@ export interface StackQLConfig {
1717 proxyUser ?: string ;
1818 proxyPassword ?: string ;
1919 proxyScheme ?: "http" | "https" ;
20+ outputFormat ?: "csv" | "json" ;
2021}
2122
2223export class StackQL {
2324 private binaryPath ?: string ; //The full path of the `stackql` executable (not supported in `server_mode`).
2425 private downloader : Downloader = new Downloader ( ) ;
2526 private serverMode = false ;
2627 private connection ?: Client ;
27- private format : "object" = "object " ;
28+ private outputFormat : "csv" | "json" = "json " ;
2829 private params : string [ ] = [ ] ;
2930 private version : string | undefined ; // The version number of the `stackql` executable (not supported in `server_mode`)
3031 private sha : string | undefined ; // The commit (short) sha for the installed `stackql` binary build (not supported in `server_mode`).
@@ -47,6 +48,13 @@ export class StackQL {
4748 return { version : this . version , sha : this . sha } ;
4849 }
4950
51+ async execute ( query : string ) {
52+ if ( this . serverMode ) {
53+ const result = await this . runServerQuery ( query ) ;
54+ }
55+ return await this . runQuery ( query ) ;
56+ }
57+
5058 private async updateVersion ( ) {
5159 if ( ! this . binaryPath ) {
5260 throw new Error ( "Binary path not found" ) ;
@@ -61,12 +69,22 @@ export class StackQL {
6169 this . sha = sha ;
6270 }
6371 }
72+
73+ /**
74+ * Upgrade the `stackql` executable to the latest version
75+ * @returns The version number of the `stackql` executable (not supported in `server_mode`)
76+ */
6477 async upgrade ( ) {
6578 this . binaryPath = await this . downloader . upgradeStackQL ( ) ;
6679 await this . updateVersion ( ) ;
6780 return this . getVersion ( ) ;
6881 }
6982
83+ /**
84+ * Initialize the `stackql` executable
85+ * @param config The configuration object
86+ * @returns The binary path of the `stackql` executable if `server_mode` is `false`, otherwise `undefined`
87+ */
7088 public async initialize ( config : StackQLConfig ) {
7189 this . binaryPath = config . binaryPath ;
7290 this . serverMode = config . serverMode || false ;
@@ -80,6 +98,7 @@ export class StackQL {
8098 this . binaryPath = await this . downloader . setupStackQL ( ) ;
8199 this . setProperties ( config ) ;
82100 }
101+
83102 private binaryExist ( ) {
84103 return ! ! this . binaryPath && osUtils . fileExists ( this . binaryPath ) ;
85104 }
@@ -139,8 +158,24 @@ export class StackQL {
139158 if ( config . proxyHost !== undefined ) {
140159 this . setProxyProperties ( config ) ;
141160 }
161+
162+ if ( config . outputFormat !== undefined ) {
163+ if ( ! [ "csv" , "json" ] . includes ( config . outputFormat ) ) {
164+ throw new Error (
165+ `Invalid outputFormat. Expected one of ['csv', 'json'], got ${ config . outputFormat } .` ,
166+ ) ;
167+ }
168+ this . params . push ( "--output" ) ;
169+ this . params . push ( config . outputFormat ) ;
170+ this . outputFormat = config . outputFormat ;
171+ }
142172 }
143173
174+ /**
175+ * Run a StackQL query
176+ * @param query The StackQL query
177+ * @returns The result of the query
178+ */
144179 public async runQuery ( query : string ) {
145180 assertExists ( this . binaryPath ) ;
146181 const args = [ "exec" , query ] . concat ( this . params ) ;
@@ -165,15 +200,23 @@ export class StackQL {
165200 this . connection = await server . connect ( connectionString ) ;
166201 }
167202
203+ /**
204+ * Close the connection to the stackql server
205+ */
168206 public async closeConnection ( ) {
169207 if ( this . connection ) {
170208 await this . connection . end ( ) ;
171209 }
172210 }
173211
212+ /**
213+ * Run a StackQL query on the server
214+ * @param query The StackQL query
215+ * @returns The result of the query
216+ */
174217 public async runServerQuery ( query : string ) {
175218 try {
176- if ( this . format === "object " ) {
219+ if ( this . outputFormat === "json " ) {
177220 const result = await this . queryObjectFormat ( query ) ;
178221 return result ;
179222 }
0 commit comments