forked from denodrivers/postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.ts
56 lines (46 loc) · 1.42 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Connection } from "./connection.ts";
import { Query, QueryConfig, QueryResult } from "./query.ts";
import { ConnectionParams, IConnectionParams } from "./connection_params.ts";
export class Client {
protected _connection: Connection;
constructor(config?: IConnectionParams | string) {
const connectionParams = new ConnectionParams(config);
this._connection = new Connection(connectionParams);
}
async connect(): Promise<void> {
await this._connection.startup();
await this._connection.initSQL();
}
// TODO: can we use more specific type for args?
async query(
text: string | QueryConfig,
...args: any[]
): Promise<QueryResult> {
const query = new Query(text, ...args);
return await this._connection.query(query);
}
async end(): Promise<void> {
await this._connection.end();
}
// Support `using` module
_aenter = this.connect;
_aexit = this.end;
}
export class PoolClient {
protected _connection: Connection;
private _releaseCallback: () => void;
constructor(connection: Connection, releaseCallback: () => void) {
this._connection = connection;
this._releaseCallback = releaseCallback;
}
async query(
text: string | QueryConfig,
...args: any[]
): Promise<QueryResult> {
const query = new Query(text, ...args);
return await this._connection.query(query);
}
async release(): Promise<void> {
await this._releaseCallback();
}
}