Skip to content

Commit

Permalink
adding option to set the DB name (#205)
Browse files Browse the repository at this point in the history
* adding option to set the DB name

* adding ts def

Co-authored-by: Dominick Miller <[email protected]>
  • Loading branch information
millerdk12 and Dominick Miller authored Sep 19, 2022
1 parent 61d6594 commit 4702ba3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ The MongoDB transport takes the following options. 'db' is required:
false.
* __db:__ MongoDB connection uri, pre-connected `MongoClient` object or promise
which resolves to a pre-connected `MongoClient` object.
* __dbName:__ The database name to connect to, defaults to DB name based on
connection URI if not provided, ignored if using a pre-connected mongoose connection.
* __options:__ MongoDB connection parameters (optional, defaults to
`{poolSize: 2, autoReconnect: true, useNewUrlParser: true}`).
* __collection__: The name of the collection you want to store log messages in,
Expand Down
9 changes: 8 additions & 1 deletion lib/winston-mongodb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ declare module 'winston-mongodb' {
* @memberof MongoDBConnectionOptions
*/
options?: any;
/**
/**
* The database name to connect to, defaults to DB name based on connection URI if not provided, ignored if using a pre-connected mongoose connection.
*
* @type {string}
* @memberof MongoDBConnectionOptions
*/
dbName?: string;
/**
* The name of the collection you want to store log messages in, defaults to 'log'.
*
* @type {string}
Expand Down
9 changes: 6 additions & 3 deletions lib/winston-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const helpers = require('./helpers');
* suppress output.
* @param {string|Object} options.db MongoDB connection uri or preconnected db
* object.
* @param {string} options.dbName The database name to connect to,
* defaults to DB name based on connection URI if not provided
* @param {Object} options.options MongoDB connection parameters
* (optional, defaults to `{poolSize: 2, autoReconnect: true, useNewUrlParser: true}`).
* @param {string=logs} options.collection The name of the collection you want
Expand Down Expand Up @@ -60,6 +62,7 @@ let MongoDB = exports.MongoDB = function(options) {
}
this.name = options.name || 'mongodb';
this.db = options.db;
this.dbName = (options.dbName || undefined);
this.options = options.options;
if (!this.options) {
this.options = {
Expand Down Expand Up @@ -135,7 +138,7 @@ let MongoDB = exports.MongoDB = function(options) {
return mongodb.MongoClient.connect(logger.db, logger.options
).then(client=>{
logger.mongoClient = client;
setupDatabaseAndEmptyQueue(client.db());
setupDatabaseAndEmptyQueue(client.db(self.dbName));
}, err=>{
console.error('winston-mongodb: error initialising logger', err);
if (options.tryReconnect) {
Expand All @@ -151,11 +154,11 @@ let MongoDB = exports.MongoDB = function(options) {
} else if ('function' === typeof this.db.then) {
this.db.then(client=>{
this.mongoClient = client;
setupDatabaseAndEmptyQueue(client.db());
setupDatabaseAndEmptyQueue(client.db(self.dbName));
}, err=>console.error('winston-mongodb: error initialising logger from promise', err));
} else if ('function' === typeof this.db.db) {
this.mongoClient = this.db;
setupDatabaseAndEmptyQueue(this.db.db())
setupDatabaseAndEmptyQueue(this.db.db(self.dbName))
} else { // preconnected object
console.warn(
'winston-mongodb: preconnected object support is deprecated and will be removed in v5');
Expand Down
5 changes: 2 additions & 3 deletions package-lock.json

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

11 changes: 11 additions & 0 deletions test/winston-mongodb-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const MongoDB = require('../lib/winston-mongodb').MongoDB;

const dbUrl = process.env.USER_WINSTON_MONGODB_URL
||process.env.WINSTON_MONGODB_URL||'mongodb://localhost:27017/winston';
const dbName = process.env.WINSTON_MONGODB_DBNAME
||'otherWinston';

mongoose.connect(dbUrl, {useNewUrlParser: true});

Expand All @@ -27,9 +29,18 @@ describe('winston-mongodb-manual-tests', function() {
});

test_suite({name: '{db: url}', Transport: MongoDB, construct: {db: dbUrl}});
test_suite({name: '{db: url, dbName: string}', Transport: MongoDB,
construct: {db: dbUrl, dbName}});
test_suite({name: '{db: url} on capped collection', Transport: MongoDB,
construct: {db: dbUrl, capped: true, collection: 'cappedLog'}});
test_suite({name: '{db: url, dbName: string} on capped collection', Transport: MongoDB,
construct: {db: dbUrl, dbName, capped: true, collection: 'cappedLog'}});
test_suite({name: '{db: client promise}', Transport: MongoDB,
construct: {db: mongodb.MongoClient.connect(dbUrl, {useNewUrlParser: true})}});
test_suite({name: '{db: client promise, dbName: string}', Transport: MongoDB,
construct: {
dbName,
db: mongodb.MongoClient.connect(dbUrl, {useNewUrlParser: true})},
});
test_suite({name: '{db: mongoose client}', Transport: MongoDB,
construct: {db: mongoose.connection}});

0 comments on commit 4702ba3

Please sign in to comment.