From 3a36bdb5a51ecf420c50d10f9ffb3cdf9dff130d Mon Sep 17 00:00:00 2001 From: Iheanyi Ekechukwu Date: Fri, 29 Jul 2022 14:30:49 -0500 Subject: [PATCH 1/2] Add a Connection class. --- src/index.ts | 67 ++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/src/index.ts b/src/index.ts index ea47c3e..d3d7d75 100644 --- a/src/index.ts +++ b/src/index.ts @@ -56,7 +56,6 @@ export interface QueryResult { export default class Client { credentials: Credentials - session: QuerySession | null constructor(credentials: Credentials) { this.credentials = credentials @@ -70,6 +69,39 @@ export default class Client { } } + async execute(query: string): Promise { + return this.connection().execute(query) + } + + connection(): Connection { + return new Connection(this) + } +} + +export class Connection { + client: Client + session: QuerySession | null + + constructor(client: Client) { + this.client = client + this.session = null + } + + async createSession(): Promise { + const saved = await this.postJSON( + `${this.client.credentials.mysqlAddress}/psdb.v1alpha1.Database/CreateSession`, + {} + ) + if (saved.ok && !saved.ok.error) { + if (saved.ok.session) { + this.session = saved.ok.session + } + return saved.ok.session + } else { + throw saved.err + } + } + async postJSON(url: string, body?: unknown): Promise> { try { const result = await apiResult( @@ -78,7 +110,7 @@ export default class Client { body: body ? JSON.stringify(body) : undefined, headers: { 'Content-Type': 'application/json', - ...this.authorizationHeader() + ...this.client.authorizationHeader() }, credentials: 'include' }) @@ -86,31 +118,15 @@ export default class Client { return result.err ? { err: result.err } : { ok: unwrap(result.ok) } } catch (e: any) { - // Catch error in case something goes awry with fetching auth header. return { err: e } } } - async createSession(): Promise { - const saved = await this.postJSON( - `${this.credentials.mysqlAddress}/psdb.v1alpha1.Database/CreateSession`, - {} - ) - if (saved.ok && !saved.ok.error) { - if (saved.ok.session) { - this.session = saved.ok.session - } - return saved.ok.session - } else { - throw saved.err - } - } - async execute(query: string): Promise { try { const startTime = new Date().getTime() const saved = await this.postJSON( - `${this.credentials.mysqlAddress}/psdb.v1alpha1.Database/Execute`, + `${this.client.credentials.mysqlAddress}/psdb.v1alpha1.Database/Execute`, { query: query, session: this.session @@ -155,20 +171,9 @@ export default class Client { } } } catch (e) { - return { - statement: query, - errorMessage: 'An unexpected error occurred. Please try again later.' - } + throw e } } - - async Session(): Promise { - if (this.session) { - return this.session - } - - return await this.createSession() - } } function parseRow(fields: QueryResultField[], rawRow: QueryResultRow): any { From 6b1198a10d390a410a8d5007a518a325fa714860 Mon Sep 17 00:00:00 2001 From: Iheanyi Ekechukwu Date: Fri, 29 Jul 2022 14:31:10 -0500 Subject: [PATCH 2/2] Remove try/catch wrapper. --- src/index.ts | 92 +++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/src/index.ts b/src/index.ts index d3d7d75..cf1ef4b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -123,55 +123,51 @@ export class Connection { } async execute(query: string): Promise { - try { - const startTime = new Date().getTime() - const saved = await this.postJSON( - `${this.client.credentials.mysqlAddress}/psdb.v1alpha1.Database/Execute`, - { - query: query, - session: this.session - } - ) - const endTime = new Date().getTime() - const elapsedTime = endTime - startTime - if (saved.ok && !saved.ok.error) { - const body = saved.ok - const result = body.result - const rows = result ? parse(result) : null - const headers = result?.fields?.map((f) => f.name) - - this.session = body.session - - // Transform response into something we understand, this matches our - // console's `QueryConsole` response format. - return { - headers, - rows, - size: rows.length, - statement: query, - time: elapsedTime - } - } else if (saved.ok && saved.ok.error) { - return { - statement: query, - errorMessage: saved.ok.error.message, - time: elapsedTime - } - } else { - let errorCode: string | null = null - if (saved.err instanceof ClientError) { - errorCode = saved.err.body.code - } - - return { - statement: query, - errorCode: errorCode, - errorMessage: apiMessage(saved.err), - time: elapsedTime - } + const startTime = new Date().getTime() + const saved = await this.postJSON( + `${this.client.credentials.mysqlAddress}/psdb.v1alpha1.Database/Execute`, + { + query: query, + session: this.session + } + ) + const endTime = new Date().getTime() + const elapsedTime = endTime - startTime + if (saved.ok && !saved.ok.error) { + const body = saved.ok + const result = body.result + const rows = result ? parse(result) : null + const headers = result?.fields?.map((f) => f.name) + + this.session = body.session + + // Transform response into something we understand, this matches our + // console's `QueryConsole` response format. + return { + headers, + rows, + size: rows.length, + statement: query, + time: elapsedTime + } + } else if (saved.ok && saved.ok.error) { + return { + statement: query, + errorMessage: saved.ok.error.message, + time: elapsedTime + } + } else { + let errorCode: string | null = null + if (saved.err instanceof ClientError) { + errorCode = saved.err.body.code + } + + return { + statement: query, + errorCode: errorCode, + errorMessage: apiMessage(saved.err), + time: elapsedTime } - } catch (e) { - throw e } } }