1
- /*eslint no-invalid-this: 0 no-console: 0 */
1
+ /*eslint no-invalid-this: 0 */
2
2
'use strict' ;
3
3
const co = require ( 'co' ) ;
4
4
const cosql = require ( 'co-mssql' ) ;
5
+ const logger = require ( '@elastic.io/component-logger' ) ( ) ;
5
6
6
7
let pstmt ;
7
8
@@ -26,18 +27,18 @@ function init(cfg) {
26
27
+ cfg . database
27
28
+ ( ( cfg . domain ) ? '?domain=' + cfg . domain + '&encrypt=' + cfg . encrypt
28
29
: '?encrypt=' + cfg . encrypt ) ;
29
- console . log ( conString ) ;
30
+ logger . trace ( conString ) ;
30
31
return co ( function * gen ( ) {
31
- console . log ( 'Connecting to the database' ) ;
32
+ logger . info ( 'Connecting to the database' ) ;
32
33
const connection = new cosql . Connection ( conString ) ;
33
34
// Always attach an error listener
34
35
connection . on ( 'error' , ( err ) => this . emit ( 'error' , err ) ) ;
35
36
let sql = cfg . query ;
36
37
yield connection . connect ( ) ;
37
- console . log ( 'Connection established' ) ;
38
- console . log ( 'Preparing query=%s' , sql ) ;
38
+ logger . info ( 'Connection established' ) ;
39
+ logger . trace ( 'Preparing query=%s' , sql ) ;
39
40
const vars = sql . match ( VARS_REGEXP ) ;
40
- console . log ( 'Found following prepared variable:type pairs=%j' , vars ) ;
41
+ logger . trace ( 'Found following prepared variable:type pairs=%j' , vars ) ;
41
42
pstmt = new cosql . PreparedStatement ( connection ) ;
42
43
for ( const tuple of vars ) {
43
44
const [ placeholder , type ] = tuple . split ( ':' ) ;
@@ -69,14 +70,14 @@ function init(cfg) {
69
70
pstmt . input ( name , cosql . BigInt ) ;
70
71
break ;
71
72
default :
72
- console . log ( 'WARNING: Can figure out the type key=%s type=%s' , name , type ) ;
73
+ logger . warn ( 'WARNING: Can figure out the type key=%s type=%s' , name , type ) ;
73
74
}
74
75
// Now let's remove all :string :boolean :date etc to the name only
75
76
sql = sql . replace ( tuple , placeholder ) ;
76
77
}
77
- console . log ( 'Resulting SQL=%s' , sql ) ;
78
+ logger . trace ( 'Resulting SQL=%s' , sql ) ;
78
79
yield pstmt . prepare ( sql ) ;
79
- console . log ( 'Preparing statement created' ) ;
80
+ logger . info ( 'Preparing statement created' ) ;
80
81
} . bind ( this ) ) ;
81
82
}
82
83
@@ -132,10 +133,11 @@ function getMetaModel(cfg, cb) {
132
133
* @param cfg configuration that is account information and configuration field values
133
134
*/
134
135
function processAction ( msg ) {
136
+ const that = this ;
135
137
return co ( function * gen ( ) {
136
- console . log ( 'Executing statement' ) ;
138
+ that . logger . info ( 'Executing statement' ) ;
137
139
yield pstmt . execute ( msg . body ) ;
138
- console . log ( 'Execution completed' ) ;
140
+ that . logger . info ( 'Execution completed' ) ;
139
141
return msg ;
140
142
} . bind ( this ) ) ;
141
143
}
0 commit comments