-
Notifications
You must be signed in to change notification settings - Fork 92
/
client.ts
41 lines (32 loc) · 1.19 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
import { dial } from "deno";
import { Connection } from "./connection.ts";
import { Query, QueryConfig, QueryResult } from "./query.ts";
import { IConnectionParams, ConnectionParams } from "./connection_params.ts";
export class Client {
connection: Connection;
connectionParams: ConnectionParams;
constructor(config?: IConnectionParams | string) {
this.connectionParams = new ConnectionParams(config);
}
async connect() {
const { host, port } = this.connectionParams;
let addr = `${host}:${port}`;
const conn = await dial("tcp", addr);
this.connection = new Connection(conn, this.connectionParams);
await this.connection.startup({ ...this.connectionParams });
}
// TODO: can we use more specific type for args?
async query(text: string | QueryConfig, ...args: any[]): Promise<QueryResult> {
let config: QueryConfig;
if (typeof(text) === "string") {
config = { text, args }
} else {
config = text;
}
const query = new Query(this.connection, config);
return await query.execute();
}
async end(): Promise<void> {
await this.connection.end();
}
}