-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathe4store.js
121 lines (109 loc) · 3.61 KB
/
e4store.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
// Generated by CoffeeScript 1.9.3
(function() {
var Sequelize, _,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
_ = require('lodash');
Sequelize = require('sequelize');
module.exports = function(Store) {
var E4Store;
E4Store = (function(superClass) {
extend(E4Store, superClass);
function E4Store(Session) {
this.Session = Session;
}
E4Store.prototype.clear = function(callback) {
return this.Session.sync({
force: true
}).then(function() {
return typeof callback === "function" ? callback(null, null) : void 0;
})["catch"](function(err) {
return typeof callback === "function" ? callback(err, null) : void 0;
});
};
E4Store.prototype.destroy = function(sid, callback) {
return this.Session.destroy({
where: {
sid: sid
}
}).then(function() {
return typeof callback === "function" ? callback(null, null) : void 0;
})["catch"](function(err) {
return typeof callback === "function" ? callback(err, null) : void 0;
});
};
E4Store.prototype.length = function(callback) {
return this.Session.count().then(function(count) {
return typeof callback === "function" ? callback(count) : void 0;
})["catch"](function(err) {
return typeof callback === "function" ? callback(err) : void 0;
});
};
E4Store.prototype.get = function(sid, callback) {
return this.Session.findOne({
where: {
sid: sid
},
attributes: ['data']
}).then(function(session) {
if (session != null) {
return typeof callback === "function" ? callback(null, JSON.parse(session.data)) : void 0;
} else {
return typeof callback === "function" ? callback(null, null) : void 0;
}
})["catch"](function(err) {
return typeof callback === "function" ? callback(err, null) : void 0;
});
};
E4Store.prototype.set = function(sid, session, callback) {
var data;
data = JSON.stringify(session);
return this.Session.findOne({
where: {
sid: sid
}
}).then((function(_this) {
return function(s) {
if (s != null) {
return s;
} else {
return _this.Session.build({
sid: sid
});
}
};
})(this)).then(function(s) {
s.data = data;
return s.save();
}).then(function(s) {
return typeof callback === "function" ? callback(null, s) : void 0;
})["catch"](function(err) {
return typeof callback === "function" ? callback(err, null) : void 0;
});
};
return E4Store;
})(Store);
return function(sequelize, name, model) {
if (name == null) {
name = 'Session';
}
if (model == null) {
model = {};
}
return new E4Store(sequelize.define(name, _.extend(model, {
sid: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
validate: {
notEmpty: true
}
},
data: {
type: Sequelize.TEXT,
allowNull: true
}
})));
};
};
}).call(this);