Skip to content

Commit

Permalink
feat: Add support for low level connection configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Oct 8, 2015
1 parent 00f135e commit 14faf60
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 2 deletions.
116 changes: 116 additions & 0 deletions lib/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,125 @@
import MongoDB = require('mongodb');

export interface Configuration {
host?: string;
port?: number;
hosts?: { address: string; port?: number }[];
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;
}
}
4 changes: 2 additions & 2 deletions lib/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Core {
this._config = config;
}

private mongoConnectAsyc = Bluebird.promisify(MongoDB.MongoClient.connect);
private mongoConnectAsyc = Bluebird.promisify<MongoDB.Db, string, any>(MongoDB.MongoClient.connect);

private _plugins: Plugin[] = [];
private _url: string;
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit 14faf60

Please sign in to comment.