Comparing version 4.0.2 to 4.1.0
/// <reference types="node" /> | ||
import { EventEmitter } from 'events'; | ||
import { Db, MongoClient, MongoClientOptions } from 'mongodb'; | ||
import * as retry from 'retry'; | ||
export declare class DatabaseClient extends EventEmitter { | ||
@@ -21,5 +22,7 @@ client: Promise<MongoClient>; | ||
* @param [client] Optional instantiated MongoDB connection | ||
* @param [retryOptions] Optional retry options for when initial connection fails. See https://github.com/tim-kos/node-retry#retryoperationoptions | ||
* @param [onRetryCallback] Optional. Will be invoked with current number of retry attempts and error reason for current attempt | ||
* @memberof DatabaseClient | ||
*/ | ||
connect(uri: string, userOpts?: MongoClientOptions, client?: MongoClient | Promise<MongoClient>): Promise<Db>; | ||
connect(uri: string, userOpts?: MongoClientOptions, client?: MongoClient | Promise<MongoClient>, retryOptions?: retry.OperationOptions, onRetryCallback?: (attempts: number, err: any) => void): Promise<Db>; | ||
/** | ||
@@ -26,0 +29,0 @@ * Close the connection |
@@ -7,2 +7,12 @@ "use strict"; | ||
const Deferred_1 = require("./Deferred"); | ||
const retryOptsDefault = { | ||
retries: 5, | ||
factor: 3, | ||
minTimeout: 1000, | ||
maxTimeout: 60000, | ||
randomize: true, | ||
forever: false, | ||
maxRetryTime: undefined, | ||
unref: false | ||
}; | ||
class DatabaseClient extends events_1.EventEmitter { | ||
@@ -26,5 +36,7 @@ /** | ||
* @param [client] Optional instantiated MongoDB connection | ||
* @param [retryOptions] Optional retry options for when initial connection fails. See https://github.com/tim-kos/node-retry#retryoperationoptions | ||
* @param [onRetryCallback] Optional. Will be invoked with current number of retry attempts and error reason for current attempt | ||
* @memberof DatabaseClient | ||
*/ | ||
async connect(uri, userOpts, client) { | ||
async connect(uri, userOpts, client, retryOptions, onRetryCallback) { | ||
this.uri = uri; | ||
@@ -35,3 +47,3 @@ if (client !== undefined) { | ||
else { | ||
this.deferredClient.resolve(this.createClient(this.uri, userOpts)); | ||
this.deferredClient.resolve(this.createClient(this.uri, userOpts, retryOptions, onRetryCallback)); | ||
} | ||
@@ -58,5 +70,6 @@ this.deferredDb.resolve((await this.client).db()); | ||
*/ | ||
createClient(uri, userOpts) { | ||
createClient(uri, userOpts, retryOptions, onRetryCallback) { | ||
return new Promise((resolve, reject) => { | ||
const operation = retry.operation(); | ||
const retryOpts = Object.assign({}, retryOptsDefault, retryOptions); | ||
const operation = retry.operation(retryOpts); | ||
const opts = Object.assign({}, { useNewUrlParser: true }, userOpts); | ||
@@ -70,4 +83,7 @@ operation.attempt(async (attempt) => { | ||
catch (e) { | ||
if (operation.retry(e)) | ||
if (operation.retry(e)) { | ||
if (onRetryCallback) | ||
onRetryCallback(attempt, e); | ||
return; | ||
} | ||
this.emit('error', e); | ||
@@ -74,0 +90,0 @@ reject(e); |
{ | ||
"name": "mongtype", | ||
"version": "4.0.2", | ||
"version": "4.1.0", | ||
"description": "Mongo Repository for Node written in TypeScript", | ||
@@ -53,3 +53,3 @@ "main": "dist/index.js", | ||
"ts-node": "^8.6.2", | ||
"tslint": "^6.0.0", | ||
"tslint": "^6.1.1", | ||
"tslint-config-security": "^1.16.0", | ||
@@ -61,3 +61,3 @@ "tslint-config-swimlane": "^4.0.0", | ||
"@types/mongodb": "^3.3.16", | ||
"mongodb": "^3.5.3", | ||
"mongodb": "^3.5.5", | ||
"retry": "^0.12.0" | ||
@@ -64,0 +64,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44225
792
Updatedmongodb@^3.5.5