-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress-session-mysqlx.js
143 lines (124 loc) · 4.31 KB
/
express-session-mysqlx.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*!
* Based On
* Connect - Redis
*/
module.exports = function (session) {
const Store = session.Store
const noop = () => {}
class MysqlXStore extends Store {
constructor(options = {}) {
super(options)
if (!options.client) {
throw new Error('A client must be directly provided to the MysqlXStore')
}
this.client = options.client
this.ttl = options.ttl || 86400 // One day in seconds.
this.disableTouch = options.disableTouch || false
this.schema = options.schema
this.collection = options.collection
}
get(sid, cb = noop) {
this.client.getSession().then(session => {
var db = session.getSchema(this.schema);
var coll = db.getCollection(this.collection);
coll.find("sid = :sid").limit(1).bind("sid", sid).execute()
.then(doc => {
cb(null, doc.fetchOne());
session.close();
});
}).catch(err => cb(err))
}
set(sid, sess, cb = noop) {
Object.assign(sess, {sid: sid, expires: Date.now() + (this.ttl * 1000)});
this.client.getSession().then(session => {
var db = session.getSchema(this.schema);
var coll = db.getCollection(this.collection);
var doc = coll.find("sid = :sid").bind("sid", sid).execute();
doc.then(d => {
let dd = d.toArray();
if (dd.length == 0) {
coll.add(sess).execute();
} else {
coll.modify("sid = :sid").bind("sid", sid).patch(sess).execute();
}
session.close();
return cb(null);
})
}).catch(err => cb(err))
}
touch(sid, sess, cb = noop) {
if (this.disableTouch) return cb();
this.client.getSession().then(session => {
var db = session.getSchema(this.schema);
var coll = db.getCollection(this.collection);
coll.find("sid = :sid").bind("sid", sid).execute()
.then(result => {
let doc = result.fetchOne();
// console.log(Number(new Date(sess.cookie.expires)) - Date.now());
if (Number(new Date(sess.cookie.expires)) - Date.now() <= 0) this.destroy(sid)
cb(null);
session.close();
})
}).catch(err => cb(err))
}
destroy(sid, cb = noop) {
this.client.getSession().then(session => {
var db = session.getSchema(this.schema);
var coll = db.getCollection(this.collection);
// Delete everything related to this session id in sessionTweet while we're at it.
let deleteSQL = `DELETE FROM sessionTweet WHERE session_id = ${sid}`;
session.sql(deleteSQL).execute();
coll.find("sid = :sid").bind("sid", sid).execute()
.then(result => {
let doc = result.fetchOne();
coll.removeOne(doc._id);
session.close();
})
return cb(null);
}).catch(err => cb(err));
}
clear(cb = noop) {
this.client.getSession().then(session => {
var db = session.getSchema(this.schema);
var coll = db.getCollection(this.collection);
// Delete everything in sessionTweet while we're at it.
let deleteSQL = `DELETE FROM sessionTweet`;
session.sql(deleteSQL).execute();
coll.remove("true").execute()
.then(() => session.close());
return cb(null);
}).catch(err => cb(err));
}
length(cb = noop) {
this.client.getSession().then(session => {
var db = session.getSchema(this.schema);
var coll = db.getCollection(this.collection);
coll.find().execute().then(result => {
let length = result.toArray().length;
cb(null, length);
session.close();
});
}).catch(err => cb(err));
}
all(cb = noop) {
this.client.getSession().then(session => {
var db = session.getSchema(this.schema);
var coll = db.getCollection(this.collection);
coll.find().execute().then(result => {
cb(null, result.toArray());
})
}).catch(err => cb(err));
}
// _getTTL(sess) {
// let ttl
// if (sess && sess.cookie && sess.cookie.expires) {
// let ms = Number(new Date(sess.cookie.expires)) - Date.now()
// ttl = Math.ceil(ms / 1000)
// } else {
// ttl = this.ttl
// }
// return ttl
// }
}
return MysqlXStore
}