-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
64 lines (51 loc) · 2 KB
/
db.js
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
function Database(){
var config = {
apiKey: "AIzaSyA1UzOxSBnrWcRAZbz542O3UTr_E5dT5kQ",
authDomain: "lcs-schedule-extension.firebaseapp.com",
databaseURL: "https://lcs-schedule-extension.firebaseio.com",
projectId: "lcs-schedule-extension",
storageBucket: "",
messagingSenderId: "68674409085"
};
firebase.initializeApp(config);
// connection to the firebase
this.dbConnection = firebase.database()
this.moment = moment();
this.region = 'NA';
this.setRegion = function(reg){
this.region = reg;
}
this.getRegion = function(){
return this.region;
}
this.setMoment = function(newTime){
this.moment = moment(newTime, 'YYYY-MM-DD');
}
// function to get todays gameday or the next gameday relative to a moment
// returns a Promise
this.getTodaysGameday = function(){
var date = this.moment.format('YYYY-MM-DD');
// console.log(date);
var ref = this.dbConnection.ref('/' + this.region + '/matches');
return ref.orderByKey().startAt(date).limitToFirst(1).once('child_added');
};
// function to get the next gameday relative to a moment
// returns a Promise
this.getNextGameday = function(){
// a little hack to make a copy of our moment and add 1 day
var date = moment(this.moment.format('YYYY-MM-DD'), 'YYYY-MM-DD').add(1, 'days').format('YYYY-MM-DD');
// console.log(date);
var ref = this.dbConnection.ref('/' + this.region + '/matches');
return ref.orderByKey().startAt(date).limitToFirst(1).once('child_added');
};
// function to get the previous gameday relative to a moment
// returns a Promise
this.getPreviousGameday = function(){
var date = moment(this.moment.format('YYYY-MM-DD'), 'YYYY-MM-DD').subtract(1, 'days').format('YYYY-MM-DD');
// console.log(date);
var ref = this.dbConnection.ref('/' + this.region + '/matches');
return ref.orderByKey().endAt(date).limitToLast(1).once('child_added');
};
}
// -------------------------------- DATABASE INITIALIZATON --------------------------------
db = new Database();