Skip to content

Commit

Permalink
extending the protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Feb 1, 2020
1 parent de33745 commit 56c96f4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 18 deletions.
41 changes: 37 additions & 4 deletions src/connection.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
import {IQueryResult} from './result';
import {IConnectionParams} from './params';

export interface IPreparedStatement {
name: string
sql: string
values: any[]
}

export interface IQueryOptions {

}

/**
* Client-side connection options.
*/
export interface IConnectionOptions {

/**
* Number of milliseconds before timing out query with error.
*/
queryTimeout?: number;
}

export class Connection {
constructor(options: IConnectionOptions) {

}

async connect(cn: IConnectionParams): Promise<void> {

}

/**
* Executes Query.
* Executes Sql.
*/
async query(query: string, values?: any[], options?: IQueryOptions): Promise<IQueryResult[]> {
async query(sql: string, values?: any[]): Promise<IQueryResult[]> {
return [];
}

async prepare(ps: IPreparedStatement): Promise<IQueryResult> {
return {command: '', fields: [], rows: []};
}

/**
* Releases connection back to the pool.
* Releases the connection:
*
* - physically, if it was created directly.
* - back into the pool, if it came from one.
*/
release(options: any) {
release() {

}
}
54 changes: 40 additions & 14 deletions src/pool.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import {IConnectionParams} from "./params";

type AcquireOptions = {
/**
* Acquire a detached connection, which won't be returned
*/
detach: boolean
};
import {IConnectionParams} from './params';
import {IQueryResult} from './result';
import {Connection, IPreparedStatement} from './connection';

/**
* Pool Options.
Expand All @@ -15,29 +10,60 @@ export interface IPoolOptions {
* Maximum number of connections.
*/
max?: number;

/**
* Number of milliseconds a connection remains idle
* before it is closed and removed from the pool.
*/
idleTimeout: number;
}

/**
* Connection Pool.
*/
export class Pool {

constructor(options?: IPoolOptions) {
constructor(cn: IConnectionParams, options?: IPoolOptions) {

}

/**
* Creates a direct physical/unmanageable connection.
* Acquires the next available connection from the pool.
*/
connect(cn: IConnectionParams) {

async connect(): Promise<Connection> {
return new Connection({});
}

/**
* Acquires next available connection from the pool.
* Safe query execution;
*
* 1. Acquires next available connection from the pool;
* 2. Executes the query;
* 3. Releases connection back to the pool.
*/
acquire(options: AcquireOptions) {
async query(sql: string, values?: any[]): Promise<IQueryResult[]> {
return this.execute<IQueryResult[]>(c => c.query(sql, values));
}

/**
* Safe prepared statement execution;
*/
async prepare(ps: IPreparedStatement): Promise<IQueryResult> {
return this.execute<IQueryResult>(c => c.prepare(ps));
}

/**
* Safe generic-method execution;
*/
protected async execute<R>(cb: (c: Connection) => Promise<R>): Promise<R> {
const con = await this.connect();
try {
const result = await cb(con);
con.release();
return result;
} catch (e) {
con.release();
throw e;
}
}
}

0 comments on commit 56c96f4

Please sign in to comment.