-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathdb.js
More file actions
60 lines (48 loc) · 1.65 KB
/
Copy pathdb.js
File metadata and controls
60 lines (48 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// This file is part of InfiniteSky.
// Copyright (c) InfiniteSky Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder
mongoose = require('mongoose');
//Constructor
// Handles connecting to the database
function DB(connectString) {
global.db = this;
this.mongoose = mongoose;
global.mongoose = mongoose;
console.log('Connecting to MongoDB: '+connectString);
mongoose.connect(connectString);
// When successfully connected
mongoose.connection.on('connected', function () {
console.log('Database Connected');
db.scripts = new vmscript('db','db', function(){
console.log('Clearing all accounts that are logged in.');
db.Account.logoutAll();
});
main.events.emit('db_connected');
//console.log('Mongoose default connection open to ' + dbURI);
console.log('Mongoose connected');
});
// If the connection throws an error
mongoose.connection.on('error',function (err) {
dumpError('Mongoose default connection error: ' + err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
// TODO: How to handle if db disconnects whilst server is up?
});
main.addShutdownMethod(function DBDisconnect(){
console.log('Disconnecting from mongoose');
mongoose.connection.close();
});
this.find = function(name, searchfilter, feilds) {
if (this[name]===undefined) { console.error('No db object defined as '+name); return; }
this[name].find(searchfilter,feilds,function(err,docs) {
if (err) {
dumpError(err);
return;
}
console.log(docs);
});
};
}
module.exports = DB;