-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.ts
50 lines (44 loc) · 1.16 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
import Connection from "./connection.ts";
import Query from "./query.ts";
export interface ClientConfig {
user?: string;
password: string;
database?: string;
hostname?: string;
certFile?: string;
port?: number;
}
export class Client {
private conn: Connection;
private connecting = false;
private connected = false;
private queryable = true;
private ended = false;
constructor(config: ClientConfig) {
this.conn = new Connection(config);
}
async connect() {
if (this.connecting || this.connected) {
throw new Error(
"Client has already been connected. You cannot reuse a client.",
);
}
this.connecting = true;
await this.conn.connect();
this.connecting = false;
this.connected = true;
}
async query(sql: string): Promise<Query> {
if (!this.queryable) throw new Error("Client is not queryable");
if (this.ended) throw new Error("Client was closed and is not queryable");
this.queryable = false;
const result = await this.conn.query(sql);
this.queryable = true;
return result;
}
async end() {
if (this.ended) return;
this.ended = true;
await this.conn.end();
}
}