From 14faf60e20665c50afee19bc285372c9617bb1bd Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Thu, 8 Oct 2015 13:58:39 +0200 Subject: [PATCH] feat: Add support for low level connection configuration --- lib/Configuration.ts | 116 +++++++++++++++++++++++++++++++++++++++++++ lib/Core.ts | 4 +- 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/lib/Configuration.ts b/lib/Configuration.ts index 8aa1e48..9a7b91d 100644 --- a/lib/Configuration.ts +++ b/lib/Configuration.ts @@ -1,3 +1,5 @@ +import MongoDB = require('mongodb'); + export interface Configuration { host?: string; port?: number; @@ -5,5 +7,119 @@ export interface Configuration { database?: string; username?: string; password?: string; + + options?: ConnectionOptions; + [key:string]: any; +} + +interface ConnectionOptions { + db?: DatabaseLevelConnectionOptions; + server?: ServerLevelConnectionOptions; + replset?: ReplicasetLevelConnectionOptions; + mongos?: MongosLevelConnectionOptions; +} + +interface DatabaseLevelConnectionOptions { + /** + * The write concern for the operation where < 1 is no acknowledgment of write and w >= 1 or w = ‘majority’ acknowledges the write + */ + w?: string | number; + + /** + * Set the timeout for waiting for write concern to finish (combines with w option) + */ + wtimeout?: number; + + /** + * Write waits for fsync before returning + */ + fsync?: boolean; + + /** + * Write waits for journal sync before returning + */ + j?: boolean; + + /** + * The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). + */ + readPreference?: string; + + /** + * The tags object {‘loc’:‘ny’} used with the readPreference. + */ + readPreferenceTags?: any; + + /** + * Use c++ bson parser. + */ + native_parser?: boolean; + + /** + * Force server to create _id fields instead of client. + */ + forceServerObjectId?: boolean; + + /** + * Object overriding the basic ObjectID primary key generation. + */ + pkFactory?: any; + + /** + * Serialize functions. + */ + serializeFunctions?: boolean; + + /** + * Perform operations using raw bson buffers. + */ + raw?: boolean; + + /** + * Number of milliseconds between retries. + */ + retryMiliseconds?: number; + + /** + * Number of retries off connection. + */ + numberOfRetries?: number; + + /** + * Sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited. + */ + bufferMaxEntries?: number; +} + +interface ServerLevelConnectionOptions extends BasicConnectionOptions { + autoReconnect?: boolean; +} + +interface ReplicasetLevelConnectionOptions extends MongosLevelConnectionOptions { + replicaSet?: string; + connectWithNoPrimary?: boolean; +} + +interface MongosLevelConnectionOptions extends BasicConnectionOptions { + ha?: boolean; + haInterval?: number; + secondaryAcceptableLatencyMS?: number; +} + +interface BasicConnectionOptions { + poolSize?: number; + ssl?: boolean; + sslValidate?: boolean; + sslCA?: Buffer[] | string[]; + sslCert?: Buffer | string; + sslKey?: Buffer | string; + sslPass?: Buffer | string; + + socketOptions?: { + noDelay?: boolean; + keepAlive?: number; + connectTimeoutMS?: number; + socketTimeoutMS?: number; + } } \ No newline at end of file diff --git a/lib/Core.ts b/lib/Core.ts index 4ecead8..6810b55 100644 --- a/lib/Core.ts +++ b/lib/Core.ts @@ -55,7 +55,7 @@ export class Core { this._config = config; } - private mongoConnectAsyc = Bluebird.promisify(MongoDB.MongoClient.connect); + private mongoConnectAsyc = Bluebird.promisify(MongoDB.MongoClient.connect); private _plugins: Plugin[] = []; private _url: string; @@ -167,7 +167,7 @@ export class Core { return Bluebird.bind(this).then(() => { if (this._connection) return this._connection; if (this._connectPromise) return this._connectPromise; - return this._connectPromise = this.mongoConnectAsyc(this.url); + return this._connectPromise = this.mongoConnectAsyc(this.url, this._config.options); }).then((db: MongoDB.Db) => { return this.onConnecting(db); }).then(db => {