-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple_model.js
150 lines (119 loc) · 3.48 KB
/
simple_model.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
144
145
146
147
148
149
150
var _ = require('underscore');
var util = require('util');
/**
* This model is intending for testing server systems before the remote repo is added,
* or for unit testing.
* @param config
* @param data
* @constructor
*/
function MockModel(config, data) {
var self = this;
if (config) {
_.extend(self, config);
}
this.repo = [];
this.index = {};
if (data) {
data.forEach(function (item) {
self.put(item, function () {
});
});
}
}
MockModel.prototype = {
all:function (callback) {
callback(null, this.repo.slice(0));
},
count:function (filter, callback) {
if (!callback) {
filter = null;
callback = filter;
}
callback(null, filter ? _.filter(this.repo, filter).length : this.repo.length);
},
del:function (id, callback) {
var self = this;
if (self.index.hasOwnProperty(id)) {
var outed = self.index[id];
delete self.index[id];
self.repo = _.reject(self.repo, function (n) {
var nid = n[self.key];
// console.log('test: key %s == %s', nid, id);
return nid == id;
});
// console.log('repo afer del: %s', util.inspect(this.repo));
callback(null, outed);
} else {
callback(new Error('cannot delete ' + id + '; no record found'));
}
},
get:function (id, callback) {
if (this.index.hasOwnProperty(id)) {
callback(null, this.index[id]);
} else {
callback(new Error('cannot find key ' + id));
}
},
find:function (what, callback, sort) {
var self = this;
var out = [];
if (typeof what == 'function') {
f = what;
} else {
f = function (n) {
for (p in what) {
var pvalue = what[p];
if (!n.hasOwnProperty(p)) {
return false;
} else if (n[p] != pvalue) {
return false;
}
}
return true;
}
}
out = _.filter(self.repo, f);
if (sort) {
return _.sortBy(out, sort);
} else {
return out;
}
},
key:'id',
next_key:function () {
var self = this;
var id = 0;
this.repo.forEach(function (obj) {
var oid = obj[self.key];
// console.log('next_key == %s', oid);
if (oid > id) {
id = oid;
}
});
// console.log('next_key: returning %s', id);
return id + 1;
},
put:function (obj, callback) {
var self = this;
if (_.isObject(obj)) {
if (obj.hasOwnProperty(self.key)) {
obj[self.key] = parseInt(obj[self.key]);
self.repo = _.reject(self.repo, function (item) {
if (!item.hasOwnProperty(self.key)) {
return false;
}
return item[self.key] == obj[self.key]
})// done cleaning out old object
} else {
obj[self.key] = self.next_key();
}
self.repo.push(obj);
self.index[obj[self.key]] = obj;
if (callback) {
callback(null, obj);
}
}
}
}
module.exports = MockModel;