-
Notifications
You must be signed in to change notification settings - Fork 92
/
client.ts
66 lines (53 loc) · 1.62 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
57
58
59
60
61
62
63
64
65
66
import { Connection } from "./connection.ts";
import { ConnectionOptions, createParams } from "./connection_params.ts";
import { Query, QueryConfig, QueryResult } from "./query.ts";
export class Client {
protected _connection: Connection;
constructor(config?: ConnectionOptions | string) {
const connectionParams = createParams(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 multiQuery(queries: QueryConfig[]): Promise<QueryResult[]> {
const result: QueryResult[] = [];
for (const query of queries) {
result.push(await this.query(query));
}
return result;
}
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();
}
}