Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function () {
dbClient = new MongoClient(new Server(exports.options.server, exports.options.port), function (err, db) {
if (err) {
console.log("[SEVERE] Database connection failure!");
console.log(err);
}
});
// Open connection
dbClient.open(function (err, dbClient) {
gdb = dbClient.db(exports.options.database_name);
hook('hook_mongodb_ready', {}, function() {});
});
},
hook_db_insert: {
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
const client = new MongoClient(url);
// Use connect method to connect to the Server
client.connect(function(err, client) {
assert.equal(null, err);
console.log("Connected correctly to server");
const db = client.db(dbName);
findDocuments(db, function() {
client.close();
});
});
const clearDB = async () => {
try {
const dbConnection = await MongoDB.MongoClient.connect(config.dbURL);
await dbConnection.dropDatabase();
await dbConnection.close();
} catch (error) {
console.log(error);
}
};
app.post('/collect', (req, res) => {
console.log(req.body);
const client = new MongoClient(process.env.MONGO_URL);
client.connect((err) => {
const db = client.db();
const collection = db.collection('events');
collection.insertOne({ timestamp: new Date(), ...req.body }, ((err, result) => {
client.close();
res.send("ok");
}))
});
});
return new Promise((resolve) => {
mongodb.MongoClient.connect(url, (error, db) => {
if (error) {
this.print("MongoDB " + error, 31);
this.retry("Retrying again in ", () => this.setupConnection().then(resolve), 5);
return void 0;
} else {
this.db.instance = db;
this.loadCollection(CFG.MONGO_COLLECTION_USERS).then(() => {
this.print(`\x1b[36;1mMongoDB\x1b[0m\x1b[32;1m connection established\x1b[0m`);
resolve();
});
}
db.on("close", (error) => {
this.print("MongoDB " + error, 31);
this.retry("Trying to reconnect in ", () => this.setupConnection().then(resolve), 5);
});
});
public constructor(dbConfig: any){
var url: string;
if(dbConfig.user!=="" && dbConfig.pass!=="")
url = "mongodb://"+dbConfig.user+":"+dbConfig.pass+"@"+dbConfig.host+":"+dbConfig.port+"/"+dbConfig.dbName;
else
url = "mongodb://"+dbConfig.host+":"+dbConfig.port+"/"+dbConfig.dbName;
var context: any = Sync.promise(MongoClient, MongoClient.connect, url);
if(context instanceof Error) throw context;
this.context = context;
}
constructor(configs: MongoDBModuleConfigs) {
super();
this.client = new MongoClient(configs.URL, configs.CLIENT_OPTIONS);
this.dbName = configs.DBNAME || 'test';
}
async bootstrap() {
useFactory: async (connections: Map) => {
const key = hash.sha1({
uri,
clientOptions,
});
if (connections.has(key)) {
return connections.get(key);
}
const client = new MongoClient(uri, clientOptions);
connections.set(key, client);
return await client.connect();
},
inject: [getContainerToken(containerName)],
var getManagers = function( pipeObj, callback ) {
var mongoClient = new MongoClient()
;
mongoClient.connect( pipeObj.input.url, function( err, db ) {
db.collection( pipeObj.input.collection ).find({playoffs: {$exists:true} }, {managers:1, playoffs:1}).toArray(
function( err, results ) {
if (err) cb( err, pipeObj );
pipeObj.data.push( results );
pipeObj.prevStep += 1;
callback( err, pipeObj );
db.close();
});
});
};
'use strict';
const MongoClient = require('mongodb').MongoClient;
const config = require('./config');
const client = new MongoClient(config.get('db'));
module.exports = client;