File tree 2 files changed +23
-3
lines changed
2 files changed +23
-3
lines changed Original file line number Diff line number Diff line change
1
+ import EventEmitter from "events" ;
1
2
import { Client as PgClient } from "pg" ;
2
3
3
4
import add from "./methods/add" ;
@@ -41,20 +42,27 @@ export type Methods =
41
42
| "all"
42
43
| "type" ;
43
44
44
- export class Client {
45
+ export class Client extends EventEmitter {
45
46
protected dbUrl : string ;
46
47
protected options : ClientOptions | undefined ;
47
- protected client : PgClient ;
48
48
protected tableName : string | undefined ;
49
49
50
+ /**
51
+ * The `pg` client instance for your database.
52
+ */
53
+
54
+ public client : PgClient ;
55
+
50
56
/**
51
57
* Whether the database has been connected or not.
52
58
* @see connect
53
59
* @see end
54
60
*/
61
+
55
62
public connected : boolean ;
56
63
57
64
constructor ( dbUrl : string , options ?: ClientOptions ) {
65
+ super ( ) ;
58
66
this . dbUrl = dbUrl ;
59
67
this . options = options ;
60
68
this . tableName = options ?. table ?? undefined ;
@@ -64,23 +72,28 @@ export class Client {
64
72
65
73
/**
66
74
* Connects to the database specified when creating the client.
67
- * @returns void
75
+ * @returns the client
68
76
* @example await db.connect();
69
77
*/
70
78
71
79
public async connect ( ) {
72
80
await this . client . connect ( ) ;
73
81
this . connected = true ;
82
+ this . emit ( "ready" , this ) ;
83
+ return this ;
74
84
}
75
85
76
86
/**
77
87
* Ends the connection to the database specified when creating the client.
88
+ * @returns the client
78
89
* @example await db.end();
79
90
*/
80
91
81
92
public async end ( ) {
82
93
await this . client . end ( ) ;
83
94
this . connected = false ;
95
+ this . emit ( "end" , this ) ;
96
+ return this ;
84
97
}
85
98
86
99
/**
Original file line number Diff line number Diff line change @@ -30,9 +30,14 @@ class Test {
30
30
}
31
31
}
32
32
33
+ db . on ( "ready" , ( ) => console . log ( "ℹ️ Database connected" ) ) ;
34
+ db . on ( "end" , ( ) => console . log ( "ℹ️ Database connection ended" ) ) ;
35
+
33
36
( async ( ) => {
34
37
await db . connect ( ) ;
35
38
39
+ console . time ( "Time to test (excludes connection & end)" ) ;
40
+
36
41
await db . set ( "users.0" , {
37
42
username : "Zero" ,
38
43
@@ -79,6 +84,8 @@ class Test {
79
84
// Test 13
80
85
new Test ( await db . type ( "users.0.points" ) ) . test ( "number" ) ;
81
86
87
+ console . timeEnd ( "Time to test (excludes connection & end)" ) ;
88
+
82
89
await db . drop ( ) ;
83
90
await db . end ( ) ;
84
91
You can’t perform that action at this time.
0 commit comments