-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdelete-old-game-data.js
More file actions
69 lines (59 loc) · 1.95 KB
/
delete-old-game-data.js
File metadata and controls
69 lines (59 loc) · 1.95 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
61
62
63
64
65
66
67
68
var MongoClient = require('mongodb').MongoClient;
var secrets = require('./secrets.js');
var mongoConnectionURL = secrets.mongoKey;
var Q = require('q');
// get current date string to compare to dates in database
var getDateString = function() {
var d = new Date();
var result = (d.getMonth() + 1).toString();
result += '/' + d.getDate();
result += '/' + d.getFullYear();
return result;
};
var currentDateString = getDateString();
// returns a promise that resolves when the database opens
var openGameDatabase = function() {
return Q.ninvoke(MongoClient, 'connect', mongoConnectionURL).then(function(db) {
console.log('open!');
return {
db: db,
gameDataCollection: db.collection('jsBattleGameData')
};
});
};
// delete all game data information with date strings less than current
module.exports = function(enteredDate) {
// if no date passed in use the current date
var dateDeleteBefore = enteredDate || currentDateString;
// opens connection to mongo database
var openDatabasePromise = openGameDatabase();
// access the open database promise
openDatabasePromise.then(function(mongoData) {
var gameDataCollection = mongoData.gameDataCollection;
var db = mongoData.db;
var removeData = false;
gameDataCollection.find({date: dateDeleteBefore}, function(err, todaysData) {
if(err) {
console.log('error in find: ', err);
}
if(todaysData) {
removeData = true;
console.log('todaysData: ', todaysData);
}
if(removeData) {
// remove all data with date less than current
gameDataCollection.remove({date: { $ne: dateDeleteBefore}}, function(err, removed) {
// error handling
if (err) {
console.log('err: ', err);
}
console.log('number removed: ', removed);
});
}
// close the database
db.close();
console.log('closed the collection');
});
});
};
module.exports();