-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connector.ts
52 lines (43 loc) · 1.33 KB
/
Connector.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
import {DBConfig} from "./types.d.ts";
import {Client} from "https://deno.land/x/mysql/mod.ts";
export class Connector {
private static _instance: Connector;
private _client: Client;
public constructor() {
this._client = new Client();
}
public async connect(config: DBConfig) {
await this._client.connect(config);
}
public static async connect(config: DBConfig)
{
if (this._instance && this._instance.hasConfig()) {
return this._instance;
}
this._instance = new this();
await this._instance.connect(config);
return this._instance;
}
public static async drop() {
await this._instance._client.close();
this._instance = new this();
}
public static get instance()
{
return this._instance || (this._instance = new this());
}
public hasConfig() {
return this._client.config.db != undefined &&
this._client.config.hostname != undefined &&
this._client.config.username != undefined &&
this._client.config.password != undefined;
}
public checkConnection () {
if (!this.hasConfig()) {
throw new Error("the client isn't connect, make sure to connect first!");
}
}
get client() : Client {
return this._client;
}
}