|
| 1 | +const mongoose = require('mongoose'); |
| 2 | +const dotenv = require('dotenv'); |
| 3 | +const logCaching = process.env.LOG_CACHING; |
| 4 | +const jsHelpers = require('../lib/helpers/js-helpers'); |
| 5 | + |
| 6 | +function sleep(ms) { |
| 7 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 8 | +} |
| 9 | + |
| 10 | +/** FOR FINDING ERRANT LOGS **/ |
| 11 | +if(process.env.SHOW_LOG_LOCATION == 'true' || 1 == 2){ |
| 12 | + jsHelpers.showLogLocation(); |
| 13 | +} |
| 14 | + |
| 15 | +process.on('uncaughtException', (err) => { |
| 16 | + console.log('Uncaught Exception: ', err); |
| 17 | + console.log(err.stack); |
| 18 | +}); |
| 19 | + |
| 20 | +process.on('unhandledRejection', (err) => { |
| 21 | + console.log('Unhandled Rejection: ', err); |
| 22 | + console.log(err.stack); |
| 23 | +}); |
| 24 | + |
| 25 | +/** |
| 26 | + * Load environment variables from .env file, where API keys and passwords are configured. |
| 27 | + */ |
| 28 | +dotenv.load({ path: '.env.settings' }); |
| 29 | +dotenv.load({ path: '.env.private' }); |
| 30 | + |
| 31 | +const database = process.env.MONGODB_URI || process.env.MONGODB_DOCKER_URI || process.env.MONGO_URI || process.env.MONGOLAB_URI; |
| 32 | + |
| 33 | +mongoose.set('useNewUrlParser', true); |
| 34 | +mongoose.set('useFindAndModify', false); |
| 35 | +mongoose.set('useCreateIndex', true); |
| 36 | +mongoose.set('useUnifiedTopology', true); |
| 37 | + |
| 38 | +/** |
| 39 | + * Connect to MongoDB. |
| 40 | + */ |
| 41 | +mongoose.Promise = global.Promise; |
| 42 | + |
| 43 | +mongoose.Promise = global.Promise; |
| 44 | +mongoose.connect(database, { |
| 45 | + keepAlive: true, |
| 46 | + reconnectTries: Number.MAX_VALUE |
| 47 | +}); |
| 48 | + |
| 49 | +if(process.env.MONGOOSE_DEBUG == 'true' || 1 == 1){ |
| 50 | + mongoose.set('debug', true); |
| 51 | +} |
| 52 | + |
| 53 | +mongoose.connection.on('error', (err) => { |
| 54 | + console.error(err); |
| 55 | + console.log('%s MongoDB connection error. Please make sure MongoDB is running.'); |
| 56 | + process.exit(); |
| 57 | +}); |
| 58 | + |
| 59 | +console.log(`CACHING IS RUNNING AGAINST: ${database} \n`); |
| 60 | + |
| 61 | +// function find (name, query, cb) { |
| 62 | +// mongoose.connection.db.collection(name, function (err, collection) { |
| 63 | +// collection.find(query).toArray(cb); |
| 64 | +// }); |
| 65 | +// } |
| 66 | +// setTimeout(function(){ |
| 67 | +// find('session', {}, function(result){ |
| 68 | +// console.log(result); |
| 69 | +// }) |
| 70 | +// }, 1000) |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +mongoose.connection.on('open', function (ref) { |
| 75 | + console.log('Connected to mongo server.'); |
| 76 | + //trying to get collection names |
| 77 | + mongoose.connection.db.listCollections().toArray(function (err, names) { |
| 78 | + |
| 79 | + |
| 80 | + // console.log(names); // [{ name: 'dbname.myCollection' }] |
| 81 | + |
| 82 | + mongoose.connection.db.collection('sessions', function (err, collection) { |
| 83 | + console.log(collection) |
| 84 | + |
| 85 | + collection.deleteMany({ session: { $not: /.*passport.*/i }}, function(err, result){ |
| 86 | + console.log(result); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + }); |
| 91 | +}) |
0 commit comments