Import other modules into monorepo
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
65
src/orm/connection/Connection.ts
Normal file
65
src/orm/connection/Connection.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import {Collection, ErrorWithContext} from "../../util";
|
||||
import {QueryResult} from "../types";
|
||||
import {SQLDialect} from "../dialect/SQLDialect";
|
||||
import {AppClass} from "../../lifecycle/AppClass";
|
||||
|
||||
/**
|
||||
* Error thrown when a connection is used before it is ready.
|
||||
* @extends Error
|
||||
*/
|
||||
export class ConnectionNotReadyError extends ErrorWithContext {
|
||||
constructor(name = '', context: {[key: string]: any} = {}) {
|
||||
super(`The connection ${name} is not ready and cannot execute queries.`)
|
||||
this.context = context
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract base class for database connections.
|
||||
* @abstract
|
||||
*/
|
||||
export abstract class Connection extends AppClass {
|
||||
|
||||
constructor(
|
||||
/**
|
||||
* The name of this connection
|
||||
* @type string
|
||||
*/
|
||||
public readonly name: string,
|
||||
/**
|
||||
* This connection's config object
|
||||
*/
|
||||
public readonly config: any = {},
|
||||
) { super() }
|
||||
|
||||
public abstract dialect(): SQLDialect
|
||||
|
||||
/**
|
||||
* Open the connection.
|
||||
* @return Promise<void>
|
||||
*/
|
||||
public abstract init(): Promise<void>
|
||||
|
||||
/**
|
||||
* Execute an SQL query and get the result.
|
||||
* @param {string} query
|
||||
* @return Promise<QueryResult>
|
||||
*/
|
||||
public abstract query(query: string): Promise<QueryResult>
|
||||
|
||||
/**
|
||||
* Close the connection.
|
||||
* @return Promise<void>
|
||||
*/
|
||||
public abstract close(): Promise<void>
|
||||
|
||||
// public abstract databases(): Promise<Collection<Database>>
|
||||
|
||||
// public abstract database(name: string): Promise<Database | undefined>
|
||||
|
||||
// public abstract database_as_schema(name: string): Promise<Database>
|
||||
|
||||
// public abstract tables(database_name: string): Promise<Collection<Table>>
|
||||
|
||||
// public abstract table(database_name: string, table_name: string): Promise<Table | undefined>
|
||||
}
|
||||
66
src/orm/connection/PostgresConnection.ts
Normal file
66
src/orm/connection/PostgresConnection.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import {Connection, ConnectionNotReadyError} from "./Connection";
|
||||
import {Client} from "pg";
|
||||
import {Inject} from "../../di";
|
||||
import {QueryResult} from "../types";
|
||||
import {collect} from "../../util";
|
||||
import {SQLDialect} from "../dialect/SQLDialect";
|
||||
import {PostgreSQLDialect} from "../dialect/PostgreSQLDialect";
|
||||
import {Logging} from "../../service/Logging";
|
||||
|
||||
/**
|
||||
* Type interface representing the config for a PostgreSQL connection.
|
||||
*/
|
||||
export interface PostgresConnectionConfig {
|
||||
user: string,
|
||||
host: string,
|
||||
database: string,
|
||||
password?: string,
|
||||
port?: string,
|
||||
}
|
||||
|
||||
/**
|
||||
* An implementation of a database Connection for dealing with PostgreSQL servers.
|
||||
*/
|
||||
export class PostgresConnection extends Connection {
|
||||
@Inject()
|
||||
protected readonly logging!: Logging
|
||||
|
||||
/** The `pg` database client. */
|
||||
protected client?: Client
|
||||
|
||||
public dialect(): SQLDialect {
|
||||
return <PostgreSQLDialect> this.app().make(PostgreSQLDialect)
|
||||
}
|
||||
|
||||
public async init() {
|
||||
this.logging.debug(`Initializing PostgreSQL connection ${this.name}...`)
|
||||
this.client = new Client(this.config)
|
||||
await this.client.connect()
|
||||
}
|
||||
|
||||
public async close() {
|
||||
this.logging.debug(`Closing PostgreSQL connection ${this.name}...`)
|
||||
if ( this.client ) {
|
||||
await this.client.end()
|
||||
}
|
||||
}
|
||||
|
||||
public async query(query: string): Promise<QueryResult> {
|
||||
if ( !this.client ) throw new ConnectionNotReadyError(this.name, { config: JSON.stringify(this.config) })
|
||||
this.logging.verbose(`Executing query in connection ${this.name}: \n${query.split('\n').map(x => ' ' + x).join('\n')}`)
|
||||
|
||||
try {
|
||||
const result = await this.client.query(query)
|
||||
|
||||
return {
|
||||
rows: collect(result.rows),
|
||||
rowCount: result.rowCount,
|
||||
}
|
||||
} catch (e) {
|
||||
throw this.app().errorWrapContext(e, {
|
||||
query,
|
||||
connection: this.name,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user