-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdb.js
41 lines (34 loc) · 788 Bytes
/
db.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
var User = require('./user')
, Nonce = require('./nonce');
// Simple in-memory database for the sake of example
function Crudify(Schema) {
this.db = {};
this.Schema = Schema;
}
Crudify.prototype.get = function(id) {
return this.db[id];
}
Crudify.prototype.find = function(check) {
for(id in this.db) {
var m = this.db[id];
if(check(m)) return m;
}
}
Crudify.prototype.create = function() {
var self = this;
var _arguments = arguments;
function S() {
return self.Schema.apply(this, _arguments);
}
S.prototype = this.Schema.prototype;
var model = new S();
this.db[model.id] = model;
return model;
}
Crudify.prototype.delete = function(id) {
delete this.db[id];
}
module.exports = {
users: new Crudify(User),
nonces: new Crudify(Nonce)
};