-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsession.js
42 lines (37 loc) · 969 Bytes
/
session.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
const Keyv = require('keyv');
const { Store } = require('express-session');
class KeyvStore extends Store {
constructor(options) {
super();
this.keyv = new Keyv(options.uri, options);
this.keyv.on('error', err => console.error('Keyv connection error:', err));
}
async get(sid, callback) {
try {
const data = await this.keyv.get(sid);
callback(null, data);
} catch (err) {
console.error(`Error getting session: ${sid}`, err);
callback(err);
}
}
async set(sid, session, callback) {
try {
await this.keyv.set(sid, session);
callback(null);
} catch (err) {
console.error(`Error setting session: ${sid}`, err);
callback(err);
}
}
async destroy(sid, callback) {
try {
await this.keyv.delete(sid);
callback(null);
} catch (err) {
console.error(`Error destroying session: ${sid}`, err);
callback(err);
}
}
}
module.exports = KeyvStore;