daton/orm/src/db/Connection.ts

51 lines
1.1 KiB
TypeScript
Raw Normal View History

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
*/
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
*/
export abstract class Connection {
constructor(
2020-08-17 14:44:23 +00:00
/**
* The name of this connection
* @type string
*/
public readonly name: string,
2020-08-17 14:44:23 +00:00
/**
* This connection's config object
*/
public readonly config: any = {},
) {}
2020-08-17 14:44:23 +00:00
/**
* Open the connection.
* @return Promise<void>
*/
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>
*/
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>
*/
public abstract async close(): Promise<void>
}