Created
March 14, 2019 17:05
-
-
Save C-Duv/543f4be151aa3ae42499a09536d56571 to your computer and use it in GitHub Desktop.
Mongoose - GH7608: Pass data from debug to post hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { MongoMemoryServer } = require('mongodb-memory-server'); | |
const mongoose = require('mongoose'); | |
const mongoServer = new MongoMemoryServer({ | |
'instance': { | |
'dbName': 'temp', | |
'port': 35555, | |
} | |
}); | |
const debugHook = function(collectionName, method, query, queryOptions) { | |
console.log(`[${new Date().toISOString()}] DEBUG: debug-mongoose: collectionName: `, collectionName); | |
console.log(`[${new Date().toISOString()}] DEBUG: debug-mongoose: method: `, method); | |
console.log(`[${new Date().toISOString()}] DEBUG: debug-mongoose: query: `, query); | |
console.log(`[${new Date().toISOString()}] DEBUG: debug-mongoose: queryOptions: `, queryOptions); | |
query._debugHookVar = 'fooFromDebug'; | |
if (queryOptions) { | |
queryOptions._debugHookVar = 'fooFromDebug-viaOptions'; | |
} | |
}; | |
const preHook = function() { | |
console.log(`[${new Date().toISOString()}] DEBUG: pre-hook: query is ${this.constructor.name}`); | |
if (this.hasOwnProperty('_debugHookVar')) { | |
console.log(`[${new Date().toISOString()}] DEBUG: pre-hook: Read this._debugHookVar: `, this._debugHookVar); | |
} | |
if ( | |
this.hasOwnProperty('options') | |
&& this.options.hasOwnProperty('_debugHookVar') | |
) { | |
console.log(`[${new Date().toISOString()}] DEBUG: pre-hook: Read this.options._debugHookVar: `, this.options._debugHookVar); | |
} | |
this._preHookVar = 'fooFromPre'; | |
if (this.hasOwnProperty('options')) { | |
this.options._preHookVar = 'fooFromPre-viaOptions'; | |
} | |
}; | |
const postHook = function(result) { | |
console.log(`[${new Date().toISOString()}] DEBUG: post-hook: query is ${this.constructor.name}`); | |
if (this.hasOwnProperty('_debugHookVar')) { | |
console.log(`[${new Date().toISOString()}] DEBUG: post-hook: Read this._debugHookVar: `, this._debugHookVar); | |
} | |
if ( | |
this.hasOwnProperty('options') | |
&& this.options.hasOwnProperty('_debugHookVar') | |
) { | |
console.log(`[${new Date().toISOString()}] DEBUG: post-hook: Read this.options._debugHookVar: `, this.options._debugHookVar); | |
} | |
if (this.hasOwnProperty('_preHookVar')) { | |
console.log(`[${new Date().toISOString()}] DEBUG: post-hook: Read this._preHookVar: `, this._preHookVar); | |
} | |
if ( | |
this.hasOwnProperty('options') | |
&& this.options.hasOwnProperty('_preHookVar') | |
) { | |
console.log(`[${new Date().toISOString()}] DEBUG: post-hook: Read this.options._preHookVar: `, this.options._preHookVar); | |
} | |
this._postHookVar = 'fooFromPost'; | |
if (this.hasOwnProperty('options')) { | |
this.options._postHookVar = 'fooFromPost-viaOptions'; | |
} | |
} | |
mongoose.set('debug', debugHook); | |
mongoose.Promise = Promise; | |
mongoServer.getConnectionString().then((mongoUri) => { | |
const mongooseOpts = { | |
autoReconnect: true, | |
reconnectTries: Number.MAX_VALUE, | |
reconnectInterval: 1000, | |
}; | |
mongoose.connect(mongoUri, mongooseOpts); | |
mongoose.connection.on('error', (e) => { | |
if (e.message.code === 'ETIMEDOUT') { | |
console.log(e); | |
mongoose.connect(mongoUri, mongooseOpts); | |
} | |
console.log(e); | |
}); | |
mongoose.connection.once('open', () => { | |
const MySchema = new mongoose.Schema({ | |
'field1': [], | |
}); | |
MySchema.pre('find', preHook); | |
MySchema.post('find', postHook); | |
MySchema.pre('aggregate', preHook); | |
MySchema.post('aggregate', postHook); | |
const MyModel = mongoose.model('my_collection', MySchema); | |
/** Find query **/ | |
const findQuery = MyModel.find( | |
{'field1': 2} | |
); | |
findQuery.options.findOption = 'find'; | |
findQuery.exec((err, results) => { | |
if (err) { | |
throw err; | |
} | |
}); | |
/** | |
[2019-03-14T16:43:14.413Z] DEBUG: pre-hook: query is Query | |
[2019-03-14T16:43:14.415Z] DEBUG: debug-mongoose: collectionName: my_collections | |
[2019-03-14T16:43:14.415Z] DEBUG: debug-mongoose: method: find | |
[2019-03-14T16:43:14.415Z] DEBUG: debug-mongoose: query: { field1: 2 } | |
[2019-03-14T16:43:14.417Z] DEBUG: debug-mongoose: queryOptions: { findOption: 'find', | |
_preHookVar: 'fooFromPre-viaOptions', | |
projection: {} } | |
[2019-03-14T16:43:14.424Z] DEBUG: post-hook: query is Query | |
[2019-03-14T16:43:14.424Z] DEBUG: post-hook: Read this._preHookVar: fooFromPre | |
[2019-03-14T16:43:14.424Z] DEBUG: post-hook: Read this.options._preHookVar: fooFromPre-viaOptions | |
**/ | |
/** /Find query **/ | |
/** Aggregate query **/ | |
const aggregateQuery = MyModel.aggregate([ | |
{ $project: { a: 1, b: 1 } }, | |
{ $limit: 1 } | |
]); | |
aggregateQuery.options.aggregateOption = 'aggregate'; | |
aggregateQuery.exec((err, results) => { | |
if (err) { | |
throw err; | |
} | |
}); | |
/** | |
[2019-03-14T16:43:39.474Z] DEBUG: pre-hook: query is Aggregate | |
[2019-03-14T16:43:39.485Z] DEBUG: debug-mongoose: collectionName: my_collections | |
[2019-03-14T16:43:39.486Z] DEBUG: debug-mongoose: method: aggregate | |
[2019-03-14T16:43:39.486Z] DEBUG: debug-mongoose: query: [ { '$project': { a: 1, b: 1 } }, { '$limit': 1 } ] | |
[2019-03-14T16:43:39.488Z] DEBUG: debug-mongoose: queryOptions: { aggregateOption: 'aggregate' } | |
[2019-03-14T16:43:39.496Z] DEBUG: post-hook: query is Aggregate | |
[2019-03-14T16:43:39.496Z] DEBUG: post-hook: Read this._preHookVar: fooFromPre | |
[2019-03-14T16:43:39.496Z] DEBUG: post-hook: Read this.options._preHookVar: fooFromPre-viaOptions | |
**/ | |
/** /Aggregate query **/ | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "", | |
"version": "0.0.0", | |
"license": "UNLICENSED", | |
"private": false, | |
"dependencies": { | |
"mongoose": "^5.4.17", | |
"mongodb-memory-server": "^3.1.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment