-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
116 lines (107 loc) · 3.35 KB
/
database.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const dbName = 'todo';
const mongoClient = require('mongodb').MongoClient;
// Allow to create a connection for each query
let executeQuery = function (callback) {
mongoClient.connect('mongodb://localhost/' + dbName, function (err, client) {
if (err)
throw err;
// Call lambda function pass in parameter
callback(client);
client.close();
});
}
function connectionTest() {
return new Promise(function (resolve, reject) {
executeQuery(function (client) {
if(!client) {
resolve(false);
}
resolve(true);
})
})
}
function getAllTasks() {
return new Promise(function (resolve, reject) {
executeQuery(function (client) {
let db = client.db(dbName);
db.collection(dbName).find({}).toArray(function (findErr, result) {
if (findErr)
throw findErr;
resolve(result);
});
});
});
}
function addTask(task) {
return new Promise(function (resolve, reject) {
executeQuery(function (client) {
console.log(task);
let db = client.db(dbName);
db.collection(dbName).insertOne({
'title': task.title,
'dateBegin': task.dateBegin,
'dateEnd': task.dateEnd,
'statut': task.statut,
'tags': task.tags,
'completed': false
},
// Callback function
function (err, inserted) {
resolve(inserted.insertedId);
}
);
});
});
}
function removeCompletedTasks() {
return new Promise(function (resolve, reject) {
executeQuery(function (client) {
let db = client.db(dbName);
db.collection(dbName).remove({completed: true}, true);
resolve();
});
});
}
function updateTask(id, task) {
return new Promise(function (resolve, reject) {
executeQuery(function (client) {
let db = client.db(dbName);
db.collection(dbName).updateOne({'_id': id}, {$set: {
'title': task.title,
'dateBegin': task.dateBegin,
'dateEnd': task.dateEnd,
'statut': task.statut,
'tags': task.tags,
'completed': false
}});
resolve();
});
});
}
function removeTask(id){
return new Promise(function (resolve, reject) {
executeQuery(function (client) {
let db = client.db(dbName);
db.collection(dbName).remove({'_id': id});
resolve();
});
});
}
function removeAllTasks() {
return new Promise(function (resolve, reject) {
executeQuery(function (client) {
let db = client.db(dbName);
db.collection(dbName).remove({});
resolve();
});
});
}
// Functions to export
module.exports.connectionTest = connectionTest;
module.exports.getAllTasks = getAllTasks;
module.exports.executeQuery = executeQuery;
module.exports.addTask = addTask;
module.exports.removeCompletedTasks = removeCompletedTasks;
module.exports.updateTask = updateTask;
module.exports.removeTask = removeTask;
module.exports.removeAllTasks = removeAllTasks;