2020-07-06 14:53:03 +00:00
|
|
|
import {QueryResult} from './types.ts'
|
|
|
|
|
2020-08-17 14:44:23 +00:00
|
|
|
/**
|
|
|
|
* Error thrown when a connection is used before it is ready.
|
|
|
|
* @extends Error
|
|
|
|
*/
|
2020-07-06 14:53:03 +00:00
|
|
|
export class ConnectionNotReadyError extends Error {
|
|
|
|
constructor(name = '') {
|
|
|
|
super(`The connection ${name} is not ready and cannot execute queries.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 14:44:23 +00:00
|
|
|
/**
|
|
|
|
* Abstract base class for database connections.
|
|
|
|
* @abstract
|
|
|
|
*/
|
2020-07-06 14:53:03 +00:00
|
|
|
export abstract class Connection {
|
|
|
|
|
|
|
|
constructor(
|
2020-08-17 14:44:23 +00:00
|
|
|
/**
|
|
|
|
* The name of this connection
|
|
|
|
* @type string
|
|
|
|
*/
|
2020-07-06 14:53:03 +00:00
|
|
|
public readonly name: string,
|
2020-08-17 14:44:23 +00:00
|
|
|
/**
|
|
|
|
* This connection's config object
|
|
|
|
*/
|
2020-07-06 14:53:03 +00:00
|
|
|
public readonly config: any = {},
|
|
|
|
) {}
|
|
|
|
|
2020-08-17 14:44:23 +00:00
|
|
|
/**
|
|
|
|
* Open the connection.
|
|
|
|
* @return Promise<void>
|
|
|
|
*/
|
2020-07-06 14:53:03 +00:00
|
|
|
public abstract async init(): Promise<void>
|
2020-08-17 14:44:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute an SQL query and get the result.
|
|
|
|
* @param {string} query
|
|
|
|
* @return Promise<QueryResult>
|
|
|
|
*/
|
2020-07-06 14:53:03 +00:00
|
|
|
public abstract async query(query: string): Promise<QueryResult> // TODO query result
|
2020-08-17 14:44:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the connection.
|
|
|
|
* @return Promise<void>
|
|
|
|
*/
|
2020-07-06 14:53:03 +00:00
|
|
|
public abstract async close(): Promise<void>
|
|
|
|
|
|
|
|
}
|