Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2ac37bd

Browse files
committed
Refactor and add TypeScript type definitions
1 parent 46a7ced commit 2ac37bd

File tree

2 files changed

+126
-16
lines changed

2 files changed

+126
-16
lines changed

β€Žindex.d.tsβ€Ž

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
export = DbHelper;
2+
3+
/**
4+
* Database helper for CodeceptJS
5+
*
6+
* @author Thiago Delgado Pinto
7+
*/
8+
declare class DbHelper {
9+
10+
/**
11+
* Constructor
12+
*
13+
* @param {object} config CodeceptJS configuration.
14+
*/
15+
constructor(config: object);
16+
17+
/**
18+
* Connects to the database described by the given connection string.
19+
*
20+
* @param {string|number} key Identification for using in other commands.
21+
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
22+
* @param {object|undefined} driver [OPTIONAL] Driver object, used by `database-js`.
23+
*
24+
* @returns {Connection} DatabaseJS' connection
25+
*/
26+
connect( key: string | number, conn: string | object, driver?: object ): any;
27+
28+
29+
/**
30+
* Disconnects and removes the database connection identified by the given key.
31+
*
32+
* @param {string|number} key Database identification key set in connect().
33+
*
34+
* @returns {Promise<boolean>} If it was successful.
35+
*/
36+
disconnect( key: string | number ): Promise< boolean >;
37+
38+
/**
39+
* Disconnects and removes the database connection identified by the given key.
40+
*
41+
* @param {string|number} key Database identification key set in connect().
42+
*
43+
* @returns {Promise<boolean>} If it was successful.
44+
*/
45+
removeConnection( key: string | number ): Promise< boolean >;
46+
47+
/**
48+
* Performs a query.
49+
*
50+
* @param {string|number} key Database identification key set in connect().
51+
* @param {string} command Query to run.
52+
* @param {...any[]|undefined} params [OPTIONAL] Query parameters.
53+
*
54+
* @returns {Promise<any[]>} Query results.
55+
*/
56+
query( key: string | number, command: string, ...params: any[] ): Promise< any[] >;
57+
58+
59+
/**
60+
* Executes a command.
61+
*
62+
* @param {string|number} key Database identification key set in connect().
63+
* @param {string} command Command to run.
64+
* @param {any[]} params [OPTIONAL] Command parameters.
65+
*
66+
* @returns {Promise<any[]>} Command results.
67+
*/
68+
run( key: string | number, command: string, ...params: any[] ): Promise< any[] >;
69+
70+
/**
71+
* Creates a database connection.
72+
*
73+
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
74+
* @param {object|undefined} driver [OPTIONAL] Driver object, used by `database-js`.
75+
*
76+
* @returns {Connection} DatabaseJS' connection
77+
*/
78+
createConnection( conn: string | object, driver?: object ): any;
79+
80+
/**
81+
* Checks if there is a database connection with the given key.
82+
*
83+
* @param {string|number} key Database identification key set in connect().
84+
*
85+
* @returns {boolean}
86+
*/
87+
hasConnection( key: string | number ): boolean;
88+
89+
/**
90+
* Gets the database connection with the given key.
91+
*
92+
* @param {string|number} key Database identification key set in connect().
93+
*
94+
* @returns {Connection} DatabaseJS' connection.
95+
*/
96+
getConnection( key: string | number ): any;
97+
98+
}

β€Žindex.jsβ€Ž

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class DbHelper extends Helper {
2929
* @param {string|number} key Identification for using in other commands.
3030
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
3131
* @param {object|undefined} driver [OPTIONAL] Driver object, used by `database-js`.
32+
*
33+
* @returns {Connection} DatabaseJS' connection
3234
*/
3335
connect( key, conn, driver ) {
3436
return this.connectionMap[ key ] = this.createConnection( conn, driver );
@@ -37,20 +39,24 @@ class DbHelper extends Helper {
3739
/**
3840
* Disconnects from the database identified by the given key.
3941
*
40-
* @param {string|number} key Database identification key set in connect()
42+
* @param {string|number} key Database identification key set in connect().
43+
*
44+
* @returns {Promise<boolean>} If it was successful.
4145
*/
4246
async disconnect( key ) {
4347
let conn = this.connectionMap[ key ];
4448
if ( ! conn ) {
4549
throw this._keyNotFoundError( key );
4650
}
47-
return awaitconn.close();
51+
return conn.close();
4852
}
4953

5054
/**
5155
* Disconnects and removes the database connection identified by the given key.
5256
*
53-
* @param {string|number} key Database identification key set in connect()
57+
* @param {string|number} key Database identification key set in connect().
58+
*
59+
* @returns {boolean} If it was successful.
5460
*/
5561
async removeConnection( key ) {
5662
if ( ! this.connectionMap[ key ] ) {
@@ -69,11 +75,11 @@ class DbHelper extends Helper {
6975
/**
7076
* Performs a query.
7177
*
72-
* @param {string|number} key Database identification key set in connect()
78+
* @param {string|number} key Database identification key set in connect().
7379
* @param {string} command Query to run.
74-
* @param {any[]} params [OPTIONAL] Query parameters
80+
* @param {any[]} params [OPTIONAL] Query parameters.
7581
*
76-
* @returns {Promise<any[]>} Query results.
82+
* @returns {Promise<any[]>} Query results.
7783
*/
7884
async query( key, command, ... params ) {
7985
let conn = this.connectionMap[ key ];
@@ -82,19 +88,19 @@ class DbHelper extends Helper {
8288
}
8389
let stmt = conn.prepareStatement( command );
8490
if ( ! params ) {
85-
return awaitstmt.query();
91+
return stmt.query();
8692
}
87-
return awaitstmt.query( ... params );
93+
return stmt.query( ... params );
8894
}
8995

9096
/**
9197
* Executes a command.
9298
*
93-
* @param {string|number} key Database identification key set in connect()
99+
* @param {string|number} key Database identification key set in connect().
94100
* @param {string} command Command to run.
95-
* @param {any[]} params [OPTIONAL] Command parameters
101+
* @param {any[]} params [OPTIONAL] Command parameters.
96102
*
97-
* @returns {Promise<any[]>} Command results.
103+
* @returns {Promise<any[]>} Command results.
98104
*/
99105
async run( key, command, ... params ) {
100106
let conn = this.connectionMap[ key ];
@@ -103,16 +109,18 @@ class DbHelper extends Helper {
103109
}
104110
let stmt = conn.prepareStatement( command );
105111
if ( ! params ) {
106-
return awaitstmt.execute();
112+
return stmt.execute();
107113
}
108-
return awaitstmt.execute( ... params );
114+
return stmt.execute( ... params );
109115
}
110116

111117
/**
112118
* Creates a database connection.
113119
*
114120
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
115121
* @param {object|undefined} driver [OPTIONAL] Driver object, used by `database-js`.
122+
*
123+
* @returns {Connection} DatabaseJS' connection.
116124
*/
117125
createConnection( conn, driver ) {
118126
return new dbjs.Connection( conn, driver );
@@ -121,7 +129,9 @@ class DbHelper extends Helper {
121129
/**
122130
* Checks if there is a database connection with the given key.
123131
*
124-
* @param {string|number} key Database identification key set in connect()
132+
* @param {string|number} key Database identification key set in connect().
133+
*
134+
* @returns {boolean}
125135
*/
126136
hasConnection( key ) {
127137
return !! this.connectionMap[ key ];
@@ -130,7 +140,9 @@ class DbHelper extends Helper {
130140
/**
131141
* Gets the database connection with the given key.
132142
*
133-
* @param {string|number} key Database identification key set in connect()
143+
* @param {string|number} key Database identification key set in connect().
144+
*
145+
* @returns {Connection} DatabaseJS' connection.
134146
*/
135147
getConnection( key ) {
136148
return this.connectionMap[ key ];
@@ -139,7 +151,7 @@ class DbHelper extends Helper {
139151
/**
140152
* Creates an error that represents a database not found.
141153
*
142-
* @param {string|number} key Database identification key
154+
* @param {string|number} key Database identification key.
143155
*/
144156
_keyNotFoundError( key ) {
145157
return new Error( 'Database not found with the key ' + key );

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /