Skip to content

Commit 787e3b6

Browse files
authored
Merge pull request #48 from hapinessjs/next
Next into master
2 parents 0cbda9d + 666e857 commit 787e3b6

16 files changed

+501
-1141
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: node_js
22
node_js:
3-
- "node"
3+
- 9
44
script:
55
- yarn run test
66
after_script:
7-
- yarn run coveralls
7+
- yarn run coveralls

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ To set up your development environment:
486486

487487
## Change History
488488

489+
* v2.0.2
490+
* More debug and events for mongoose adapter
489491
* v2.0.1
490492
* Fix Mongo Utils - prepareUpdateObject
491493
* v2.0.0

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hapiness/mongo",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "Hapiness Module for MongoDB usage",
55
"main": "commonjs/index.js",
66
"types": "index.d.ts",

src/module/adapters/hapiness-mongo-adapter.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export class HapinessMongoAdapter extends EventEmitter {
6464
}
6565

6666
public tryConnect(): Observable<void> {
67+
__debugger.debug('tryConnect', `connecting to ${this._uri}`);
68+
this.emit('connecting', { uri: this._uri });
6769
return this
6870
._tryConnect()
6971
.switchMap(_ => this._afterConnect());
@@ -94,7 +96,7 @@ export class HapinessMongoAdapter extends EventEmitter {
9496
* This function should be overriden by all inherited classes.
9597
*
9698
*/
97-
public getLibrary(): any {
99+
public getLibrary<T = any>(): T {
98100
throw new Error('`getLibrary` is not implemented');
99101
}
100102

@@ -125,9 +127,9 @@ export class HapinessMongoAdapter extends EventEmitter {
125127
}
126128

127129
protected onDisconnected(): Observable<void> {
128-
__debugger.debug('onDisconnected', '');
130+
__debugger.debug('onDisconnected', `disconnected from ${this._uri}`);
129131

130-
this.emit('disconnected');
132+
this.emit('disconnected', { uri: this._uri });
131133

132134
return this
133135
.tryConnect()
@@ -177,7 +179,7 @@ export class HapinessMongoAdapter extends EventEmitter {
177179
return this._uri;
178180
}
179181

180-
public getConnection(): any {
182+
public getConnection<T = any>(): T {
181183
return this._connection;
182184
}
183185

src/module/adapters/mongoose-adapter.ts

+28-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as mongoose from 'mongoose';
2+
import { Connection, Mongoose } from 'mongoose';
23

34
import { Observable } from 'rxjs/Observable';
45
import { HapinessMongoAdapter } from './hapiness-mongo-adapter';
@@ -16,6 +17,8 @@ export class MongooseAdapter extends HapinessMongoAdapter {
1617

1718
constructor(options) {
1819
super(options);
20+
21+
this.on('error', (...args) => __debugger.debug('on#error', JSON.stringify(args)));
1922
}
2023

2124
protected _tryConnect(): Observable<void> {
@@ -29,7 +32,17 @@ export class MongooseAdapter extends HapinessMongoAdapter {
2932
reconnectInterval: 5000,
3033
};
3134

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+
});
3346

3447
// Seems that typings are not up to date at the moment
3548
this._connection['then'](() => {
@@ -43,42 +56,37 @@ export class MongooseAdapter extends HapinessMongoAdapter {
4356
protected _afterConnect(): Observable<void> {
4457
return Observable
4558
.create(observer => {
46-
4759
this.onConnected().subscribe(_ => {
4860
__debugger.debug('_afterConnect', '(subscribe) On connected success');
4961
}, (e) => {
5062
__debugger.debug('_afterConnect', `(subscribe) On connected failed ${JSON.stringify(e, null, 2)}`);
63+
this.emit('error', e);
5164
});
5265

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+
});
6871

6972
observer.next();
7073
observer.complete();
7174
});
7275
}
7376

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;
7683
}
7784

7885
public registerValue(schema: any, collection: string, collectionName?: string) {
79-
if (!!collectionName && collectionName.length) {
86+
if (collectionName && collectionName.length) {
8087
return this._connection.model(collection, schema, collectionName);
8188
}
89+
8290
return this._connection.model(collection, schema);
8391
}
8492

Original file line numberDiff line numberDiff line change
@@ -1,93 +1,26 @@
11
import * as mongoose from 'mongoose';
22
import * as mongo from 'mongodb';
33

4-
import { Observable } from 'rxjs/Observable';
5-
import { HapinessMongoAdapter } from './hapiness-mongo-adapter';
6-
import { Debugger } from '../shared';
7-
8-
const __debugger = new Debugger('MongooseGridfsBucketAdapter');
4+
import { MongooseAdapter } from './mongoose-adapter';
95

106
(<any>mongoose).Promise = global.Promise;
117

12-
export class MongooseGridFsBucketAdapter extends HapinessMongoAdapter {
13-
private _gridfsBucket: mongo.GridFSBucket;
8+
export class MongooseGridFsBucketAdapter extends MongooseAdapter {
9+
private _gridfsBucket;
1410

1511
public static getInterfaceName(): string {
1612
return 'mongoose-gridfs-bucket';
1713
}
1814

1915
constructor(options) {
2016
super(options);
21-
}
22-
23-
protected _tryConnect(): Observable<void> {
24-
return Observable
25-
.create(observer => {
26-
this._isReady = false;
27-
28-
const connectOptions: mongoose.ConnectionOptions = {
29-
reconnectTries: Number.MAX_VALUE,
30-
reconnectInterval: 5000,
31-
};
32-
33-
this._connection = mongoose.createConnection(this._uri, connectOptions);
34-
35-
// Seems that typings are not up to date at the moment
36-
this._connection['then'](connection => {
37-
observer.next();
38-
observer.complete();
39-
})
40-
.catch(err => {
41-
observer.error(err);
42-
});
43-
});
44-
}
45-
46-
protected _afterConnect(): Observable<void> {
47-
return Observable
48-
.create(observer => {
49-
this._gridfsBucket = new mongoose.mongo.GridFSBucket((<mongoose.Connection>this._connection).db);
50-
51-
this.onConnected().subscribe(_ => {
52-
__debugger.debug('_afterConnect', '(subscribe) On connected success');
53-
}, (e) => {
54-
__debugger.debug('_afterConnect', `(subscribe) On connected failed ${JSON.stringify(e, null, 2)}`);
55-
});
56-
57-
this._connection.once('error', err =>
58-
this.onError(err).subscribe(_ => {
59-
__debugger.debug('_afterConnect', '(subscribe) On connection error #success');
60-
}, (e) => {
61-
__debugger.debug('_afterConnect', `(subscribe) On connection error #failed ${JSON.stringify(e, null, 2)}`);
62-
})
63-
);
64-
65-
this._connection.once('disconnected', () =>
66-
this.onDisconnected().subscribe(_ => {
67-
__debugger.debug('_afterConnect', '(subscribe) On connection disconnected #success');
68-
}, (e) => {
69-
__debugger.debug('_afterConnect', `(subscribe) On connection disconnected #failed ${JSON.stringify(e, null, 2)}`);
70-
})
71-
);
72-
73-
observer.next();
74-
observer.complete();
75-
});
76-
}
77-
78-
public registerValue(schema: any, collection: string, collectionName?: string) {
79-
if (!!collectionName && collectionName.length) {
80-
return this._connection.model(collection, schema, collectionName);
81-
}
82-
return this._connection.model(collection, schema);
83-
}
8417

85-
public getLibrary(): any {
86-
return this._gridfsBucket;
18+
this.on('connected', () => {
19+
this._gridfsBucket = new mongoose.mongo.GridFSBucket((<mongoose.Connection>this._connection).db);
20+
});
8721
}
8822

89-
// It seems that there is a bug here and it never really close the connection it always try to reconnect afterwards.
90-
public close(): Observable<void> {
91-
return Observable.fromPromise(this._connection.client.close(true));
23+
public getLibrary<T = mongo.GridFSBucket>(): T {
24+
return <any>this._gridfsBucket;
9225
}
9326
}
+7-72
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import * as mongoose from 'mongoose';
22
import * as util from 'util';
33

4-
import { Observable } from 'rxjs/Observable';
5-
64
import { CreateGridFsStream, GridFsStream } from '../shared/gridfs-stream';
7-
import { HapinessMongoAdapter } from './hapiness-mongo-adapter';
5+
import { MongooseAdapter } from './mongoose-adapter';
86

97
/**
108
* Gridfs adapter using mongoose for connection purposes
@@ -14,7 +12,7 @@ import { HapinessMongoAdapter } from './hapiness-mongo-adapter';
1412
* @class MongooseGridFsAdapter
1513
* @extends {HapinessMongoAdapter}
1614
*/
17-
export class MongooseGridFsAdapter extends HapinessMongoAdapter {
15+
export class MongooseGridFsAdapter extends MongooseAdapter {
1816

1917
private _gridfs: GridFsStream.Grid;
2018
protected _client: any;
@@ -26,80 +24,17 @@ export class MongooseGridFsAdapter extends HapinessMongoAdapter {
2624
constructor(options) {
2725
super(options)
2826
util.deprecate((() => null), 'MongooseGridFsAdapter is deprecated use MongooseGridfsBucketAdapter instead.')();
29-
}
30-
31-
protected _tryConnect(): Observable<void> {
32-
return Observable
33-
.create(observer => {
34-
this._isReady = false;
35-
36-
if ((this._db && !this._db.close) || (this._client && !this._client.close)) {
37-
return observer.error(new Error('_db or _client needs a close function.'));
38-
}
39-
40-
if (this._db && this._db.close) {
41-
this._db.close();
42-
} else if (this._client && this._client.close) {
43-
this._client.close();
44-
}
4527

46-
const connectOptions = {
47-
reconnectTries: Number.MAX_VALUE,
48-
reconnectInterval: 5000,
49-
};
50-
51-
this._connection = mongoose.createConnection(this._uri, connectOptions);
52-
53-
// Seems that typings are not up to date at the moment
54-
this._connection['then'](connection => {
55-
observer.next();
56-
observer.complete();
57-
})
58-
.catch(err => {
59-
observer.error(err);
60-
});
61-
});
28+
this.on('connected', () => {
29+
this._gridfs = this._createGridFsStream(this._connection.db, mongoose.mongo);
30+
});
6231
}
6332

6433
protected _createGridFsStream(db, mongo) {
6534
return CreateGridFsStream(db, mongo);
6635
}
6736

68-
protected _afterConnect(): Observable<void > {
69-
return Observable
70-
.create(observer => {
71-
this._db = this._connection.db;
72-
this._client = this._connection.client;
73-
74-
this._gridfs = this._createGridFsStream(this._db, mongoose.mongo);
75-
76-
this.onConnected().subscribe(_ => {}, (e) => {});
77-
78-
this._connection.once('error', err =>
79-
this.onError(err).subscribe(_ => {}, (e) => {})
80-
);
81-
82-
this._connection.once('disconnected', () =>
83-
this.onDisconnected().subscribe(_ => {}, (e) => {})
84-
);
85-
86-
observer.next();
87-
observer.complete();
88-
});
89-
}
90-
91-
public registerValue(schema: any, collection: string, collectionName ?: string) {
92-
if (!!collectionName && collectionName.length) {
93-
return this._connection.model(collection, schema, collectionName);
94-
}
95-
return this._connection.model(collection, schema);
96-
}
97-
98-
public getLibrary(): any {
99-
return this._gridfs;
100-
}
101-
102-
public close(): Observable<void > {
103-
return Observable.fromPromise(this._client.close());
37+
public getLibrary<T = GridFsStream.Grid>(): T {
38+
return <any>this._gridfs;
10439
}
10540
}

src/module/services/mongo-client.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class MongoClientService {
1313
@Inject(MongoClientExt) private _mongoManager: MongoManager
1414
) { }
1515

16-
get() {
16+
get(): MongoManager {
1717
return this._mongoManager;
1818
}
1919

test/mocha.opts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
--require ts-node/register
66
--throw-deprecation
77
--colors
8-
--timeout 10000
8+
-b
9+
--timeout 10000

0 commit comments

Comments
 (0)