-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
163 lines (149 loc) Β· 4.51 KB
/
index.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const dbjs = require('database-js');
if ( ! Helper ) {
var Helper = require( 'codeceptjs/lib/helper' );
}
/**
* Database helper for CodeceptJS
*
* @author Thiago Delgado Pinto
*/
class DbHelper extends Helper {
/**
* Constructor
*
* @param {object} config CodeceptJS configuration.
*/
constructor( config ) {
super( config );
const defaultOptions = {};
// Attributes
this.options = Object.assign( defaultOptions, config ); // Copy from config
this.connectionMap = {};
}
/**
* Connects to the database described by the given connection string.
*
* @param {string|number} key Identification for using in other commands.
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
* @param {object|undefined} [driver] [OPTIONAL] Driver object, used by `database-js`.
*
* @returns {Connection} DatabaseJS' connection
*/
connect( key, conn, driver ) {
return this.connectionMap[ key ] = this.createConnection( conn, driver );
}
/**
* Disconnects and removes the database connection identified by the given key.
*
* @param {string|number} key Database identification key set in connect().
*
* @returns {Promise<boolean>} If it was successful.
*/
async disconnect( key ) {
let conn = this.connectionMap[ key ];
if ( ! conn ) {
throw this._keyNotFoundError( key );
}
return conn.close();
}
/**
* Disconnects and removes the database connection identified by the given key.
*
* @param {string|number} key Database identification key set in connect().
*
* @returns {Promise<boolean>} If it was successful.
*/
async removeConnection( key ) {
if ( ! this.connectionMap[ key ] ) {
return true;
}
let couldClose = true;
try {
await this.disconnect( key );
} catch ( e ) {
couldClose = false;
}
this.connectionMap[ key ] = undefined;
return couldClose;
}
/**
* Performs a query.
*
* @param {string|number} key Database identification key set in connect().
* @param {string} command Query to run.
* @param {...any[]|undefined} [params] [OPTIONAL] Query parameters.
*
* @returns {Promise<any[]>} Query results.
*/
async query( key, command, ... params ) {
let conn = this.connectionMap[ key ];
if ( ! conn ) {
throw this._keyNotFoundError( key );
}
let stmt = conn.prepareStatement( command );
if ( ! params ) {
return stmt.query();
}
return stmt.query( ... params );
}
/**
* Executes a command.
*
* @param {string|number} key Database identification key set in connect().
* @param {string} command Command to run.
* @param {any[]} [params] [OPTIONAL] Command parameters.
*
* @returns {Promise<any[]>} Command results.
*/
async run( key, command, ... params ) {
let conn = this.connectionMap[ key ];
if ( ! conn ) {
throw this._keyNotFoundError( key );
}
let stmt = conn.prepareStatement( command );
if ( ! params ) {
return stmt.execute();
}
return stmt.execute( ... params );
}
/**
* Creates a database connection.
*
* @param {string|object} conn JDBC-like connection string or a connection object accepted by `database-js`.
* @param {object|undefined} [driver] [OPTIONAL] Driver object, used by `database-js`.
*
* @returns {Connection} DatabaseJS' connection
*/
createConnection( conn, driver ) {
return new dbjs.Connection( conn, driver );
}
/**
* Checks if there is a database connection with the given key.
*
* @param {string|number} key Database identification key set in connect().
*
* @returns {boolean}
*/
hasConnection( key ) {
return !! this.connectionMap[ key ];
}
/**
* Gets the database connection with the given key.
*
* @param {string|number} key Database identification key set in connect().
*
* @returns {Connection} DatabaseJS' connection.
*/
getConnection( key ) {
return this.connectionMap[ key ];
}
/**
* Creates an error that represents a database not found.
*
* @param {string|number} key Database identification key.
*/
_keyNotFoundError( key ) {
return new Error( 'Database not found with the key ' + key );
}
}
module.exports = DbHelper;