11# codeceptjs-dbhelper
22
3- [ ![ npm version] ( https://badge.fury .io/js/ codeceptjs-dbhelper.svg )] ( https://badge.fury.io/js/codeceptjs-dbhelper )
4- [ ![ Downloads] ( https://img.shields.io/npm/dt/codeceptjs-dbhelper.svg )] ( https://npmjs.org/package/codeceptjs-dbhelper )
3+ [ ![ npm version] ( https://img.shields .io/npm/v/ codeceptjs-dbhelper.svg?color=green&label=NPM&style=for-the-badge )] ( https://badge.fury.io/js/codeceptjs-dbhelper )
4+ [ ![ Downloads] ( https://img.shields.io/npm/dt/codeceptjs-dbhelper.svg?style=for-the-badge )] ( https://npmjs.org/package/codeceptjs-dbhelper )
55
66> Let your CodeceptJS tests talk to databases
77
8- This is a [ Helper] ( https://codecept.io/helpers/ ) for [ CodeceptJS] ( https://codecept.io/ ) that allows you to execute queries or commands to databases using [ database-js] ( https://github.com/mlaanderson/database-js ) . That is, your tests written for CodeceptJS now will be able to access databases easily. ** It is especially useful for preparing databases before/after executing test cases. **
8+ This is a [ Helper] ( https://codecept.io/helpers/ ) for [ CodeceptJS] ( https://codecept.io/ ) that allows you to execute database queries and commands using [ database-js] ( https://github.com/mlaanderson/database-js ) .
99
1010π It works with ** CodeceptJS 1, 2, and 3** .
1111
@@ -57,15 +57,15 @@ In your CodeceptJS configuration file (e.g., `codecept.conf.js`, `codecept.json`
5757
5858### Syntax differences between CodeceptJS 2 and CodeceptJS 3
5959
60- In CodeceptJS 2, your callbacks receive ` I ` as argument:
60+ In CodeceptJS 2, every callback receives ` I ` as an argument:
6161
6262``` javascript
6363Scenario (' test something' , async ( I ) => { // CodeceptJS 2 notation
6464 /* ... */
6565} );
6666```
6767
68- In CodeceptJS 3, your callbacks receive an object with ` I ` - that is, ` { I } ` :
68+ In CodeceptJS 3, every callback receives an ** object** that contains the property ` I ` - that is, ` { I } ` :
6969
7070``` javascript
7171Scenario (' test something' , async ( { I } ) => { // CodeceptJS 3 notation
@@ -75,12 +75,10 @@ Scenario('test something', async ( { I } ) => { // CodeceptJS 3 notation
7575
7676See the [ CodeceptJS docs] ( https://github.com/codeceptjs/CodeceptJS/wiki/Upgrading-to-CodeceptJS-3 ) for more information on how to upgrade your codebase.
7777
78- ### Examples
78+ ### Usage
7979
8080> The following examples are written with ** CodeceptJS 3** .
8181
82- Now the object ` I ` (of your callbacks) has [ new methods] ( #api ) .
83- 8482#### Example 1
8583
8684``` js
@@ -134,52 +132,78 @@ Scenario( 'Bar', async( { I } ) => {
134132
135133## API
136134
137- 138- ``` js
139- /**
140- * Connects to the database described by the given connection string.
141- *
142- * @param {string|number} key Identification for using in other commands.
143- * @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
144- * @param {object|undefined} driver [OPTIONAL] Driver object, used by `database-js`.
145- */
146- connect ( key, conn, driver );
147- 148- /**
149- * Disconnects from the database identified by the given key.
150- *
151- * @param {string|number} key Database identification key set in connect()
152- */
153- async disconnect ( key );
154- 155- /**
156- * Disconnects and removes the database connection identified by the given key.
157- *
158- * @param {string|number} key Database identification key set in connect()
159- */
160- async removeConnection ( key );
161- 162- /**
163- * Performs a query.
164- *
165- * @param {string|number} key Database identification key set in connect()
166- * @param {string} command Query to run.
167- * @param {any[]} params [OPTIONAL] Query parameters
168- *
169- * @returns {Promise<any[]>} Query results.
170- */
171- async query ( key, command, ... params );
172- 173- /**
174- * Executes a command.
175- *
176- * @param {string|number} key Database identification key set in connect()
177- * @param {string} command Command to run.
178- * @param {any[]} params [OPTIONAL] Command parameters
179- *
180- * @returns {Promise<any[]>} Command results.
181- */
182- async run ( key, command, ... params );
135+ ``` ts
136+ /**
137+ * Connects to the database described by the given connection string.
138+ *
139+ * @param {string|number} key Identification for using in other commands.
140+ * @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
141+ * @param {object|undefined} [driver] [OPTIONAL] Driver object, used by `database-js`.
142+ *
143+ * @returns {Connection} DatabaseJS' connection
144+ */
145+ connect (key : string | number , conn : string | object , driver ?: object | undefined ): any ;
146+ /**
147+ * Disconnects and removes the database connection identified by the given key.
148+ *
149+ * @param {string|number} key Database identification key set in connect().
150+ *
151+ * @returns {Promise<boolean>} If it was successful.
152+ */
153+ disconnect (key : string | number ): Promise < boolean > ;
154+ /**
155+ * Disconnects and removes the database connection identified by the given key.
156+ *
157+ * @param {string|number} key Database identification key set in connect().
158+ *
159+ * @returns {Promise<boolean>} If it was successful.
160+ */
161+ removeConnection (key : string | number ): Promise < boolean > ;
162+ /**
163+ * Performs a query.
164+ *
165+ * @param {string|number} key Database identification key set in connect().
166+ * @param {string} command Query to run.
167+ * @param {...any[]|undefined} [params] [OPTIONAL] Query parameters.
168+ *
169+ * @returns {Promise<any[]>} Query results.
170+ */
171+ query (key : string | number , command : string , ... params ?: (any [] | undefined )[]): Promise < any []> ;
172+ /**
173+ * Executes a command.
174+ *
175+ * @param {string|number} key Database identification key set in connect().
176+ * @param {string} command Command to run.
177+ * @param {any[]} [params] [OPTIONAL] Command parameters.
178+ *
179+ * @returns {Promise<any[]>} Command results.
180+ */
181+ run (key : string | number , command : string , ... params ?: any []): Promise < any []> ;
182+ /**
183+ * Creates a database connection.
184+ *
185+ * @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
186+ * @param {object|undefined} [driver] [OPTIONAL] Driver object, used by `database-js`.
187+ *
188+ * @returns {Connection} DatabaseJS' connection
189+ */
190+ createConnection (conn : string | object , driver ?: object | undefined ): any ;
191+ /**
192+ * Checks if there is a database connection with the given key.
193+ *
194+ * @param {string|number} key Database identification key set in connect().
195+ *
196+ * @returns {boolean}
197+ */
198+ hasConnection (key : string | number ): boolean ;
199+ /**
200+ * Gets the database connection with the given key.
201+ *
202+ * @param {string|number} key Database identification key set in connect().
203+ *
204+ * @returns {Connection} DatabaseJS' connection.
205+ */
206+ getConnection (key : string | number ): any ;
183207```
184208
185209## See also
0 commit comments