Skip to content

Commit

Permalink
Merge pull request #48 from hapinessjs/next
Browse files Browse the repository at this point in the history
Next into master
  • Loading branch information
juneil authored Apr 25, 2018
2 parents 0cbda9d + 666e857 commit 787e3b6
Show file tree
Hide file tree
Showing 16 changed files with 501 additions and 1,141 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
node_js:
- "node"
- 9
script:
- yarn run test
after_script:
- yarn run coveralls
- yarn run coveralls
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ To set up your development environment:

## Change History

* v2.0.2
* More debug and events for mongoose adapter
* v2.0.1
* Fix Mongo Utils - prepareUpdateObject
* v2.0.0
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hapiness/mongo",
"version": "2.0.1",
"version": "2.0.2",
"description": "Hapiness Module for MongoDB usage",
"main": "commonjs/index.js",
"types": "index.d.ts",
Expand Down
10 changes: 6 additions & 4 deletions src/module/adapters/hapiness-mongo-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export class HapinessMongoAdapter extends EventEmitter {
}

public tryConnect(): Observable<void> {
__debugger.debug('tryConnect', `connecting to ${this._uri}`);
this.emit('connecting', { uri: this._uri });
return this
._tryConnect()
.switchMap(_ => this._afterConnect());
Expand Down Expand Up @@ -94,7 +96,7 @@ export class HapinessMongoAdapter extends EventEmitter {
* This function should be overriden by all inherited classes.
*
*/
public getLibrary(): any {
public getLibrary<T = any>(): T {
throw new Error('`getLibrary` is not implemented');
}

Expand Down Expand Up @@ -125,9 +127,9 @@ export class HapinessMongoAdapter extends EventEmitter {
}

protected onDisconnected(): Observable<void> {
__debugger.debug('onDisconnected', '');
__debugger.debug('onDisconnected', `disconnected from ${this._uri}`);

this.emit('disconnected');
this.emit('disconnected', { uri: this._uri });

return this
.tryConnect()
Expand Down Expand Up @@ -177,7 +179,7 @@ export class HapinessMongoAdapter extends EventEmitter {
return this._uri;
}

public getConnection(): any {
public getConnection<T = any>(): T {
return this._connection;
}

Expand Down
48 changes: 28 additions & 20 deletions src/module/adapters/mongoose-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as mongoose from 'mongoose';
import { Connection, Mongoose } from 'mongoose';

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

constructor(options) {
super(options);

this.on('error', (...args) => __debugger.debug('on#error', JSON.stringify(args)));
}

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

this._connection = mongoose.createConnection(this._uri, connectOptions)
this._connection = mongoose.createConnection(this._uri, connectOptions);

this._connection.on('connected', () => {
__debugger.debug('on#connected', `connected to ${this._uri}`);
this.emit('connected', { uri: this._uri });
});

this._connection.on('reconnectFailed', () => {
__debugger.debug('on#reconnectFailed', `reconnectFailed on ${this._uri}`);
this.emit('reconnectFailed', { uri: this._uri });
});

// Seems that typings are not up to date at the moment
this._connection['then'](() => {
Expand All @@ -43,42 +56,37 @@ export class MongooseAdapter extends HapinessMongoAdapter {
protected _afterConnect(): Observable<void> {
return Observable
.create(observer => {

this.onConnected().subscribe(_ => {
__debugger.debug('_afterConnect', '(subscribe) On connected success');
}, (e) => {
__debugger.debug('_afterConnect', `(subscribe) On connected failed ${JSON.stringify(e, null, 2)}`);
this.emit('error', e);
});

this._connection.once('error', err =>
this.onError(err).subscribe(_ => {
__debugger.debug('_afterConnect', '(subscribe) On connection error #success');
}, (e) => {
__debugger.debug('_afterConnect', `(subscribe) On connection error #failed ${JSON.stringify(e, null, 2)}`);
})
);

this._connection.once('disconnected', () =>
this.onDisconnected().subscribe(_ => {
__debugger.debug('_afterConnect', '(subscribe) On connection disconnected #success');
}, (e) => {
__debugger.debug('_afterConnect', `(subscribe) On connection disconnected #failed ${JSON.stringify(e, null, 2)}`);
})
);
this._connection.on('error', (...args) => this.emit('error', ...args));
this._connection.on('disconnected', () => {
__debugger.debug('on#disconnected', `disconnected from ${this._uri}`);
this.emit('disconnected', { uri: this._uri });
});

observer.next();
observer.complete();
});
}

public getLibrary(): any {
return mongoose;
public getLibrary<T = Mongoose>(): T {
return <any>mongoose;
}

public getConnection<T = Connection>(): T {
return this._connection;
}

public registerValue(schema: any, collection: string, collectionName?: string) {
if (!!collectionName && collectionName.length) {
if (collectionName && collectionName.length) {
return this._connection.model(collection, schema, collectionName);
}

return this._connection.model(collection, schema);
}

Expand Down
83 changes: 8 additions & 75 deletions src/module/adapters/mongoose-gridfs-bucket-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,26 @@
import * as mongoose from 'mongoose';
import * as mongo from 'mongodb';

import { Observable } from 'rxjs/Observable';
import { HapinessMongoAdapter } from './hapiness-mongo-adapter';
import { Debugger } from '../shared';

const __debugger = new Debugger('MongooseGridfsBucketAdapter');
import { MongooseAdapter } from './mongoose-adapter';

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

export class MongooseGridFsBucketAdapter extends HapinessMongoAdapter {
private _gridfsBucket: mongo.GridFSBucket;
export class MongooseGridFsBucketAdapter extends MongooseAdapter {
private _gridfsBucket;

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

constructor(options) {
super(options);
}

protected _tryConnect(): Observable<void> {
return Observable
.create(observer => {
this._isReady = false;

const connectOptions: mongoose.ConnectionOptions = {
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 5000,
};

this._connection = mongoose.createConnection(this._uri, connectOptions);

// Seems that typings are not up to date at the moment
this._connection['then'](connection => {
observer.next();
observer.complete();
})
.catch(err => {
observer.error(err);
});
});
}

protected _afterConnect(): Observable<void> {
return Observable
.create(observer => {
this._gridfsBucket = new mongoose.mongo.GridFSBucket((<mongoose.Connection>this._connection).db);

this.onConnected().subscribe(_ => {
__debugger.debug('_afterConnect', '(subscribe) On connected success');
}, (e) => {
__debugger.debug('_afterConnect', `(subscribe) On connected failed ${JSON.stringify(e, null, 2)}`);
});

this._connection.once('error', err =>
this.onError(err).subscribe(_ => {
__debugger.debug('_afterConnect', '(subscribe) On connection error #success');
}, (e) => {
__debugger.debug('_afterConnect', `(subscribe) On connection error #failed ${JSON.stringify(e, null, 2)}`);
})
);

this._connection.once('disconnected', () =>
this.onDisconnected().subscribe(_ => {
__debugger.debug('_afterConnect', '(subscribe) On connection disconnected #success');
}, (e) => {
__debugger.debug('_afterConnect', `(subscribe) On connection disconnected #failed ${JSON.stringify(e, null, 2)}`);
})
);

observer.next();
observer.complete();
});
}

public registerValue(schema: any, collection: string, collectionName?: string) {
if (!!collectionName && collectionName.length) {
return this._connection.model(collection, schema, collectionName);
}
return this._connection.model(collection, schema);
}

public getLibrary(): any {
return this._gridfsBucket;
this.on('connected', () => {
this._gridfsBucket = new mongoose.mongo.GridFSBucket((<mongoose.Connection>this._connection).db);
});
}

// It seems that there is a bug here and it never really close the connection it always try to reconnect afterwards.
public close(): Observable<void> {
return Observable.fromPromise(this._connection.client.close(true));
public getLibrary<T = mongo.GridFSBucket>(): T {
return <any>this._gridfsBucket;
}
}
79 changes: 7 additions & 72 deletions src/module/adapters/mongoose-gridfs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as mongoose from 'mongoose';
import * as util from 'util';

import { Observable } from 'rxjs/Observable';

import { CreateGridFsStream, GridFsStream } from '../shared/gridfs-stream';
import { HapinessMongoAdapter } from './hapiness-mongo-adapter';
import { MongooseAdapter } from './mongoose-adapter';

/**
* Gridfs adapter using mongoose for connection purposes
Expand All @@ -14,7 +12,7 @@ import { HapinessMongoAdapter } from './hapiness-mongo-adapter';
* @class MongooseGridFsAdapter
* @extends {HapinessMongoAdapter}
*/
export class MongooseGridFsAdapter extends HapinessMongoAdapter {
export class MongooseGridFsAdapter extends MongooseAdapter {

private _gridfs: GridFsStream.Grid;
protected _client: any;
Expand All @@ -26,80 +24,17 @@ export class MongooseGridFsAdapter extends HapinessMongoAdapter {
constructor(options) {
super(options)
util.deprecate((() => null), 'MongooseGridFsAdapter is deprecated use MongooseGridfsBucketAdapter instead.')();
}

protected _tryConnect(): Observable<void> {
return Observable
.create(observer => {
this._isReady = false;

if ((this._db && !this._db.close) || (this._client && !this._client.close)) {
return observer.error(new Error('_db or _client needs a close function.'));
}

if (this._db && this._db.close) {
this._db.close();
} else if (this._client && this._client.close) {
this._client.close();
}

const connectOptions = {
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 5000,
};

this._connection = mongoose.createConnection(this._uri, connectOptions);

// Seems that typings are not up to date at the moment
this._connection['then'](connection => {
observer.next();
observer.complete();
})
.catch(err => {
observer.error(err);
});
});
this.on('connected', () => {
this._gridfs = this._createGridFsStream(this._connection.db, mongoose.mongo);
});
}

protected _createGridFsStream(db, mongo) {
return CreateGridFsStream(db, mongo);
}

protected _afterConnect(): Observable<void > {
return Observable
.create(observer => {
this._db = this._connection.db;
this._client = this._connection.client;

this._gridfs = this._createGridFsStream(this._db, mongoose.mongo);

this.onConnected().subscribe(_ => {}, (e) => {});

this._connection.once('error', err =>
this.onError(err).subscribe(_ => {}, (e) => {})
);

this._connection.once('disconnected', () =>
this.onDisconnected().subscribe(_ => {}, (e) => {})
);

observer.next();
observer.complete();
});
}

public registerValue(schema: any, collection: string, collectionName ?: string) {
if (!!collectionName && collectionName.length) {
return this._connection.model(collection, schema, collectionName);
}
return this._connection.model(collection, schema);
}

public getLibrary(): any {
return this._gridfs;
}

public close(): Observable<void > {
return Observable.fromPromise(this._client.close());
public getLibrary<T = GridFsStream.Grid>(): T {
return <any>this._gridfs;
}
}
2 changes: 1 addition & 1 deletion src/module/services/mongo-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class MongoClientService {
@Inject(MongoClientExt) private _mongoManager: MongoManager
) { }

get() {
get(): MongoManager {
return this._mongoManager;
}

Expand Down
3 changes: 2 additions & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
--require ts-node/register
--throw-deprecation
--colors
--timeout 10000
-b
--timeout 10000
Loading

0 comments on commit 787e3b6

Please sign in to comment.