-
Notifications
You must be signed in to change notification settings - Fork 1
/
sessionsDao.js
62 lines (57 loc) · 1.47 KB
/
sessionsDao.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
/**
* Access sessions peristant layer
**/
var config = require("config");
var mongoClient = require("mongodb").MongoClient;
var ObjectID = require('mongodb').ObjectID;
var url = config.get("sessionsDb.connectionString");
var sessionColl = "sessions";
function getSessionById(db, sessionId) {
return new Promise(function(resolve, reject) {
db.then(function(db) {
var collection = db.collection(sessionColl);
collection.find({_id : new ObjectID(sessionId)})
.toArray(function(error, results){
if (error) {
reject(error);
return;
}
resolve(results[0] || []);
})
});
});
}
function insertSession(db, sessionData) {
return new Promise(function(resolve, reject) {
db.then(function(db) {
var collection = db.collection(sessionColl);
console.log('got collection...')
collection.insertOne(sessionData,
function(insertError, result) {
if (insertError) {
reject(insertError);
return;
}
console.log("insert session OK");
resolve(result);
})
})
});
}
function connect() {
return new Promise(function(resolve, reject) {
mongoClient.connect(url, function(err, db) {
if (err) {
reject(err);
return;
}
console.log("connected to " + url);
resolve(db);
})
});
}
module.exports = {
connect: connect,
insertSession: insertSession,
getSessionById: getSessionById
}