diff --git a/src/index.ts b/src/index.ts index ea47c3e..cf1ef4b 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,88 +118,57 @@ 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 { + async execute(query: string): Promise { + const startTime = new Date().getTime() const saved = await this.postJSON( - `${this.credentials.mysqlAddress}/psdb.v1alpha1.Database/CreateSession`, - {} + `${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) { - if (saved.ok.session) { - this.session = saved.ok.session - } - return saved.ok.session - } else { - throw saved.err - } - } + const body = saved.ok + const result = body.result + const rows = result ? parse(result) : null + const headers = result?.fields?.map((f) => f.name) - async execute(query: string): Promise { - try { - const startTime = new Date().getTime() - const saved = await this.postJSON( - `${this.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 - } + 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 } - } catch (e) { + } else if (saved.ok && saved.ok.error) { return { statement: query, - errorMessage: 'An unexpected error occurred. Please try again later.' + errorMessage: saved.ok.error.message, + time: elapsedTime + } + } else { + let errorCode: string | null = null + if (saved.err instanceof ClientError) { + errorCode = saved.err.body.code } - } - } - async Session(): Promise { - if (this.session) { - return this.session + return { + statement: query, + errorCode: errorCode, + errorMessage: apiMessage(saved.err), + time: elapsedTime + } } - - return await this.createSession() } }