-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.js
197 lines (162 loc) · 4.38 KB
/
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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const _ = require("lodash");
const uuid = require("uuid");
function UserError(str) {
let err = new Error(str);
err.isUserError = true;
return err;
}
function parseIntIfNeeded(n) {
if (!isNaN(parseFloat(n)) && isFinite(n)) {
return parseFloat(n);
} else {
return n;
}
}
function toPaths(obj) {
let paths = {};
let node,
nodes = [{ path: "", obj: obj }];
while ((node = nodes.pop()) != undefined) {
let obj = node.obj,
path = node.path;
if (!_.isPlainObject(obj)) {
paths[path] = obj;
continue;
} else {
paths[path] = {};
} // Empty object means path contains a sub object
Object.keys(obj).forEach((key) => {
let next = {
path: !path ? key : path + "." + key,
obj: obj[key],
};
nodes.push(next);
});
}
return paths;
}
function generateDiffs(author, prev, cur) {
let diffs = {};
let prevPaths = toPaths(prev);
let curPaths = toPaths(cur);
// Find deleted paths
Object.keys(prevPaths).forEach((path) => {
if (path in curPaths) {
return;
}
diffs[path] = { deleted: true, prev: prevPaths[path], author: author };
});
// Find changed and created paths
Object.keys(curPaths).forEach((path) => {
let prev = prevPaths[path];
let cur = curPaths[path];
if (_.isEqual(prev, cur)) {
return;
}
if (path in prevPaths) {
diffs[path] = { prev: prev, cur: cur };
} else {
diffs[path] = { created: true, cur: cur };
}
diffs[path].author = author;
});
return diffs;
}
function removeEmptyValues(obj) {
obj = _.cloneDeep(obj);
let node,
nodes = [obj];
while ((node = nodes.pop()) != undefined) {
Object.keys(node).forEach((key) => {
if (node[key] === "" || _.isNil(node[key])) {
delete node[key];
}
});
let children = _.filter(node, _.isPlainObject);
nodes.push(...children);
}
return obj;
}
module.exports = function (storage) {
return {
all(cb) {
storage.all(cb);
},
has(key, cb) {
storage.has(key, cb);
},
by(key, val, cb) {
val = parseIntIfNeeded(val);
storage.by(key, val, cb);
},
one(key, val, cb) {
val = parseIntIfNeeded(val);
this.by(key, val, (err, results) => {
if (err) {
return cb(err);
}
if (results.length === 0) {
return cb(null, null);
}
if (results.length !== 1) {
return cb(new Error("More than one value found."));
}
return cb(null, results[0]);
});
},
history(key, val, path, cb) {
this.one(key, val, (err, obj) => {
if (err) {
return cb(err);
}
if (obj == null) {
return cb(null, null);
}
storage.history(obj._whoid, path, (err, diffs) => {
if (err) {
return cb(err);
}
let hist = {};
diffs.forEach((diff) => {
let d = _.omit(diff, "path");
hist[diff.path] = hist[diff.path] || [];
d.date = new Date(d.date);
hist[diff.path].push(d);
});
Object.keys(hist).forEach((key) => {
hist[key].sort((a, b) => b.date - a.date);
});
cb(null, hist);
});
});
},
put(author, key, val, body, cb) {
val = parseIntIfNeeded(val);
this.one(key, val, (err, obj) => {
if (err) {
return cb(err);
}
// Before creating a new object, try to find pre-existing object using email prop.
// Should be effective since all new objects must have an email prop.
if (obj == null && key !== "email") {
body = _.set(body, key, val); // Here so key/val pair isn't lost
return this.put(author, "email", body["email"], body, cb);
}
let prev = obj || { _whoid: uuid() };
let cur = _.defaultsDeep({}, body, prev);
cur = removeEmptyValues(cur); // Don't save empty values
if (key === "email") {
cur.email = val;
}
if (!cur.email) {
return cb(UserError("Can't save. No email address found."));
}
if (Object.keys(cur).some((k) => k.indexOf(".") !== -1)) {
return cb(UserError("Key with dots ('.') aren't supported"));
}
let diffs = generateDiffs(author, prev, cur);
storage.put(cur, diffs, cb);
});
},
};
};