-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
39 lines (32 loc) · 986 Bytes
/
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
var firebase = require("firebase")
var Database = function() {
this.connection;
}
Database.prototype.establishConnection = function() {
this.connection = new Firebase("https://crackling-torch-1034.firebaseio.com");
}
Database.prototype.getTasks = function (callback) {
this.connection.on("value", function (snapshot) {
var tasks = [];
snapshot.forEach(function (child) {
tasks.push({ name: child.val().name, completed: child.val().completed, key: child.key() });
});
callback(tasks);
});
}
Database.prototype.addTask = function(task) {
this.connection.push(task);
}
Database.prototype.removeCompletedTasks = function(tasks) {
var self = this;
tasks.forEach(function(task){
self.connection.child(task.key).remove();
});
}
Database.prototype.update = function(task, newValue) {
this.connection.child(task.key).set(newValue);
}
Database.prototype.removeAll = function() {
this.connection.remove();
}
module.exports = new Database();