Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 71 additions & 70 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export interface QueryResult {

export default class Client {
credentials: Credentials
session: QuerySession | null

constructor(credentials: Credentials) {
this.credentials = credentials
Expand All @@ -70,6 +69,39 @@ export default class Client {
}
}

async execute(query: string): Promise<ExecutedQuery> {
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<QuerySession> {
const saved = await this.postJSON<QueryExecuteResponse>(
`${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<T>(url: string, body?: unknown): Promise<Result<T>> {
try {
const result = await apiResult<T>(
Expand All @@ -78,96 +110,65 @@ export default class Client {
body: body ? JSON.stringify(body) : undefined,
headers: {
'Content-Type': 'application/json',
...this.authorizationHeader()
...this.client.authorizationHeader()
},
credentials: 'include'
})
)

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<QuerySession> {
async execute(query: string): Promise<ExecutedQuery> {
const startTime = new Date().getTime()
const saved = await this.postJSON<QueryExecuteResponse>(
`${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<ExecutedQuery> {
try {
const startTime = new Date().getTime()
const saved = await this.postJSON<QueryExecuteResponse>(
`${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<QuerySession | null> {
if (this.session) {
return this.session
return {
statement: query,
errorCode: errorCode,
errorMessage: apiMessage(saved.err),
time: elapsedTime
}
}

return await this.createSession()
}
}

Expand Down