This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
89 lines (82 loc) · 2.23 KB
/
storage.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
/*global require, exports, applicationContext */
'use strict';
const _ = require('underscore');
const joi = require('joi');
const httperr = require('http-errors');
const Foxx = require('org/arangodb/foxx');
const request = require('org/arangodb/request');
const errors = require('./errors');
const cfg = applicationContext.configuration;
const Session = Foxx.Model.extend({
schema: {
_key: joi.string().required(),
uid: joi.string().allow(null).default(null),
sessionData: joi.object().default('Empty object', Object),
userData: joi.object().default('Empty object', Object),
created: joi.number().integer().default('Current date', Date.now),
lastAccess: joi.number().integer().default('Current date', Date.now),
lastUpdate: joi.number().integer().default('Current date', Date.now)
}
});
function createSession(sessionData) {
const res = request.post({
url: cfg.sessionsRoot,
body: {sessionData: sessionData || {}},
json: true
});
if (res.status >= 400) {
throw new httperr[res.status](res.message);
}
return new Session(res.body);
}
function deleteSession(sid) {
const res = request.delete({
url: cfg.sessionsRoot + '/' + sid,
json: true
});
if (res.status >= 400) {
throw new httperr[res.status](res.message);
}
return null;
}
Session.fromClient = function (sid) {
const res = request.get({
url: cfg.sessionsRoot + '/' + sid,
json: true
});
if (res.status === 404) {
throw new errors.SessionNotFound(sid);
}
if (res.status >= 400) {
throw new httperr[res.status](res.message);
}
return new Session(res.body);
};
_.extend(Session.prototype, {
forClient() {
return this.get('_key');
},
save() {
const res = request.put({
url: cfg.sessionsRoot + '/' + sid,
body: _.pick(this.attributes, 'sessionData'),
json: true
});
if (res.status === 404) {
throw new errors.SessionNotFound(sid);
}
if (res.status >= 400) {
throw new httperr[res.status](res.message);
}
this.set(res.body);
return this;
},
delete() {
deleteSession(this.get('_key'));
return true;
}
});
exports.create = createSession;
exports.get = Session.fromClient;
exports.delete = deleteSession;
exports.errors = errors;