1
1
import * as mongoose from 'mongoose' ;
2
+ import { Connection , Mongoose } from 'mongoose' ;
2
3
3
4
import { Observable } from 'rxjs/Observable' ;
4
5
import { HapinessMongoAdapter } from './hapiness-mongo-adapter' ;
@@ -16,6 +17,8 @@ export class MongooseAdapter extends HapinessMongoAdapter {
16
17
17
18
constructor ( options ) {
18
19
super ( options ) ;
20
+
21
+ this . on ( 'error' , ( ...args ) => __debugger . debug ( 'on#error' , JSON . stringify ( args ) ) ) ;
19
22
}
20
23
21
24
protected _tryConnect ( ) : Observable < void > {
@@ -29,7 +32,17 @@ export class MongooseAdapter extends HapinessMongoAdapter {
29
32
reconnectInterval : 5000 ,
30
33
} ;
31
34
32
- this . _connection = mongoose . createConnection ( this . _uri , connectOptions )
35
+ this . _connection = mongoose . createConnection ( this . _uri , connectOptions ) ;
36
+
37
+ this . _connection . on ( 'connected' , ( ) => {
38
+ __debugger . debug ( 'on#connected' , `connected to ${ this . _uri } ` ) ;
39
+ this . emit ( 'connected' , { uri : this . _uri } ) ;
40
+ } ) ;
41
+
42
+ this . _connection . on ( 'reconnectFailed' , ( ) => {
43
+ __debugger . debug ( 'on#reconnectFailed' , `reconnectFailed on ${ this . _uri } ` ) ;
44
+ this . emit ( 'reconnectFailed' , { uri : this . _uri } ) ;
45
+ } ) ;
33
46
34
47
// Seems that typings are not up to date at the moment
35
48
this . _connection [ 'then' ] ( ( ) => {
@@ -43,42 +56,37 @@ export class MongooseAdapter extends HapinessMongoAdapter {
43
56
protected _afterConnect ( ) : Observable < void > {
44
57
return Observable
45
58
. create ( observer => {
46
-
47
59
this . onConnected ( ) . subscribe ( _ => {
48
60
__debugger . debug ( '_afterConnect' , '(subscribe) On connected success' ) ;
49
61
} , ( e ) => {
50
62
__debugger . debug ( '_afterConnect' , `(subscribe) On connected failed ${ JSON . stringify ( e , null , 2 ) } ` ) ;
63
+ this . emit ( 'error' , e ) ;
51
64
} ) ;
52
65
53
- this . _connection . once ( 'error' , err =>
54
- this . onError ( err ) . subscribe ( _ => {
55
- __debugger . debug ( '_afterConnect' , '(subscribe) On connection error #success' ) ;
56
- } , ( e ) => {
57
- __debugger . debug ( '_afterConnect' , `(subscribe) On connection error #failed ${ JSON . stringify ( e , null , 2 ) } ` ) ;
58
- } )
59
- ) ;
60
-
61
- this . _connection . once ( 'disconnected' , ( ) =>
62
- this . onDisconnected ( ) . subscribe ( _ => {
63
- __debugger . debug ( '_afterConnect' , '(subscribe) On connection disconnected #success' ) ;
64
- } , ( e ) => {
65
- __debugger . debug ( '_afterConnect' , `(subscribe) On connection disconnected #failed ${ JSON . stringify ( e , null , 2 ) } ` ) ;
66
- } )
67
- ) ;
66
+ this . _connection . on ( 'error' , ( ...args ) => this . emit ( 'error' , ...args ) ) ;
67
+ this . _connection . on ( 'disconnected' , ( ) => {
68
+ __debugger . debug ( 'on#disconnected' , `disconnected from ${ this . _uri } ` ) ;
69
+ this . emit ( 'disconnected' , { uri : this . _uri } ) ;
70
+ } ) ;
68
71
69
72
observer . next ( ) ;
70
73
observer . complete ( ) ;
71
74
} ) ;
72
75
}
73
76
74
- public getLibrary ( ) : any {
75
- return mongoose ;
77
+ public getLibrary < T = Mongoose > ( ) : T {
78
+ return < any > mongoose ;
79
+ }
80
+
81
+ public getConnection < T = Connection > ( ) : T {
82
+ return this . _connection ;
76
83
}
77
84
78
85
public registerValue ( schema : any , collection : string , collectionName ?: string ) {
79
- if ( ! ! collectionName && collectionName . length ) {
86
+ if ( collectionName && collectionName . length ) {
80
87
return this . _connection . model ( collection , schema , collectionName ) ;
81
88
}
89
+
82
90
return this . _connection . model ( collection , schema ) ;
83
91
}
84
92
0 commit comments