Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. const { MongoMemoryServer } = require('mongodb-memory-server');
  2. const mongoose = require('mongoose');
  3.  
  4. const mongoServer = new MongoMemoryServer({
  5. 'instance': {
  6. 'dbName': 'temp',
  7. 'port': 35555,
  8. }
  9. });
  10.  
  11. mongoose.set('debug', (collectionName, method, query, queryOptions) => {
  12. console.log(`[${new Date().toISOString()}] debug-mongoose: query (${method}):`, query);
  13. console.log(`[${new Date().toISOString()}] debug-mongoose: queryOptions:`, queryOptions);
  14.  
  15. if (
  16. queryOptions.hasOwnProperty('profiler')
  17. && queryOptions.profiler.hasOwnProperty('startTime')
  18. ) {
  19. console.log(`[${new Date().toISOString()}] debug-mongoose: query started at ${queryOptions.profiler.startTime.toISOString()}`);
  20. }
  21. if (
  22. queryOptions.hasOwnProperty('profiler')
  23. && queryOptions.profiler.hasOwnProperty('endTime')
  24. ) {
  25. console.log(`[${new Date().toISOString()}] debug-mongoose: query ended at ${queryOptions.profiler.endTime.toISOString()}`);
  26. }
  27. });
  28.  
  29. mongoose.Promise = Promise;
  30. mongoServer.getConnectionString().then((mongoUri) => {
  31. const mongooseOpts = {
  32. autoReconnect: true,
  33. reconnectTries: Number.MAX_VALUE,
  34. reconnectInterval: 1000,
  35. };
  36.  
  37. mongoose.connect(mongoUri, mongooseOpts);
  38.  
  39. mongoose.connection.on('error', (e) => {
  40. if (e.message.code === 'ETIMEDOUT') {
  41. console.log(e);
  42. mongoose.connect(mongoUri, mongooseOpts);
  43. }
  44. console.log(e);
  45. });
  46.  
  47. mongoose.connection.once('open', () => {
  48. const MySchema = new mongoose.Schema({
  49. 'field1': [],
  50. });
  51.  
  52. const preHook = function() {
  53. this.options.profiler = {
  54. 'startTime': new Date(),
  55. };
  56. console.log(`[${new Date().toISOString()}] pre-hook: query started at ${this.options.profiler.startTime.toISOString()}`);
  57. console.log(`[${new Date().toISOString()}] pre-hook: this.options:`, this.options);
  58. };
  59. const postHook = function(result) {
  60. this.options.profiler.endTime = new Date();
  61. console.log(`[${new Date().toISOString()}] post-hook: query ended at ${this.options.profiler.endTime.toISOString()}`);
  62. console.log(`[${new Date().toISOString()}] post-hook: this.options:`, this.options);
  63. }
  64.  
  65. MySchema.pre('find', preHook);
  66. MySchema.post('find', postHook);
  67.  
  68. MySchema.pre('aggregate', preHook);
  69. MySchema.post('aggregate', postHook);
  70.  
  71. const MyModel = mongoose.model('my_collection', MySchema);
  72.  
  73. /** Find query **/
  74. MyModel.find(
  75. {'foo': 42}
  76. ).exec((err, results) => {
  77. if (err) {
  78. throw err;
  79. }
  80. });
  81.  
  82. /**
  83. [2019-03-14T10:27:55.411Z] pre-hook: query started at 2019-03-14T10:27:55.411Z
  84. [2019-03-14T10:27:55.411Z] pre-hook: this.options: { profiler: { startTime: 2019-03-14T10:27:55.411Z } }
  85. [2019-03-14T10:27:55.414Z] debug-mongoose: query (find): { foo: 42 }
  86. [2019-03-14T10:27:55.415Z] debug-mongoose: queryOptions: { profiler: { startTime: 2019-03-14T10:27:55.411Z },
  87. projection: {} }
  88. [2019-03-14T10:27:55.415Z] debug-mongoose: query started at 2019-03-14T10:27:55.411Z
  89. [2019-03-14T10:27:55.424Z] post-hook: query ended at 2019-03-14T10:27:55.424Z
  90. [2019-03-14T10:27:55.424Z] post-hook: this.options: { profiler:
  91. { startTime: 2019-03-14T10:27:55.411Z,
  92. endTime: 2019-03-14T10:27:55.424Z } }
  93. **/
  94. /** /Find query **/
  95.  
  96. /** Aggregate query **/
  97. MyModel.aggregate([
  98. { $project: { a: 1, b: 1 } },
  99. { $limit: 1 }
  100. ]).exec((err, results) => {
  101. if (err) {
  102. throw err;
  103. }
  104. });
  105.  
  106. /**
  107. [2019-03-14T10:27:34.742Z] pre-hook: query started at 2019-03-14T10:27:34.742Z
  108. [2019-03-14T10:27:34.742Z] pre-hook: this.options: { profiler: { startTime: 2019-03-14T10:27:34.742Z } }
  109. [2019-03-14T10:27:34.747Z] debug-mongoose: query (aggregate): [ { '$project': { a: 1, b: 1 } }, { '$limit': 1 } ]
  110. [2019-03-14T10:27:34.748Z] debug-mongoose: queryOptions: {}
  111. [2019-03-14T10:27:34.763Z] post-hook: query ended at 2019-03-14T10:27:34.763Z
  112. [2019-03-14T10:27:34.763Z] post-hook: this.options: { profiler:
  113. { startTime: 2019-03-14T10:27:34.742Z,
  114. endTime: 2019-03-14T10:27:34.763Z } }
  115. **/
  116. /** /Aggregate query **/
  117. });
  118. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement