Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ defaults to false.
* __metaKey:__ Configure which key is used to store metadata in the logged info object.
Defaults to `'metadata'` to remain compatible with the [metadata format](https://github.com/winstonjs/logform/blob/master/examples/metadata.js)
* __expireAfterSeconds:__ Seconds before the entry is removed. Works only if __capped__ is not set.
* __partialFilterExpression:__ Optional condition for the entry to be removed. Works only if __capped__ is not set and __expireAfterSeconds__ is set.

*Metadata:* Logged as a native JSON object in 'meta' property.

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 @@ -141,7 +141,14 @@ declare module 'winston-mongodb' {
* @memberof MongoDBConnectionOptions
*/
expireAfterSeconds?: number;
/**
* Optional condition for the entry to be removed. Works only if capped is not set and expireAfterSeconds is set.
*
* @type {*}
* @memberof MongoDBConnectionOptions
*/
partialFilterExpression?: any;
}

const MongoDB: MongoDBTransportInstance;
}
12 changes: 11 additions & 1 deletion lib/winston-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const helpers = require('./helpers');
* after transport shut down.
* @param {number} options.expireAfterSeconds Seconds before the entry is removed.
* Do not use if capped is set.
* @param {Object} options.partialFilterExpression Condition for the entry to be removed.
* Do not use if capped is set or expireAfterSeconds is not set.
*/
let MongoDB = exports.MongoDB = function(options) {
Transport.call(this, options);
Expand Down Expand Up @@ -79,6 +81,7 @@ let MongoDB = exports.MongoDB = function(options) {
this.decolorize = options.decolorize;
this.leaveConnectionOpen = options.leaveConnectionOpen;
this.expireAfterSeconds = !this.capped && options.expireAfterSeconds;
this.partialFilterExpression = !this.capped && this.expireAfterSeconds && options.partialFilterExpression ? options.partialFilterExpression : null;
this.metaKey = options.metaKey || 'metadata';
if (this.storeHost) {
this.hostname = os.hostname();
Expand Down Expand Up @@ -113,15 +116,22 @@ let MongoDB = exports.MongoDB = function(options) {
if (self.expireAfterSeconds) {
indexOpts.expireAfterSeconds = self.expireAfterSeconds;
}
if (self.partialFilterExpression) {
indexOpts.partialFilterExpression = self.partialFilterExpression;
}
return col.indexInformation({full: true}).then(info=>{
info = info.filter(i=>i.name === ttlIndexName);
if (info.length === 0) { // if its a new index then create it
return col.createIndex({timestamp: -1}, indexOpts);
} else { // if index existed with the same name check if expireAfterSeconds param has changed
} else { // if index existed with the same name check if expireAfterSeconds or partialFilterExpression param has changed
if (info[0].expireAfterSeconds !== undefined &&
info[0].expireAfterSeconds !== self.expireAfterSeconds) {
return col.dropIndex(ttlIndexName)
.then(()=>col.createIndex({timestamp: -1}, indexOpts));
} else if (info[0].partialFilterExpression !== undefined &&
JSON.stringify(info[0].partialFilterExpression) !== JSON.stringify(self.partialFilterExpression)) {
return col.dropIndex(ttlIndexName)
.then(()=>col.createIndex({timestamp: -1}, indexOpts));
}
}
});
Expand Down