-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
161 lines (130 loc) · 3.37 KB
/
index.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
var fs = require('fs');
var _ = require('underscore');
/**
* Creates a DB instance that is a simple wapper around the given file.
* @param file File path
* @param idAttribute Name of the attribut to use as the id. Used to identify
* an existing document by the put and delete methods. Default value is 'id'
*/
var open = function (file, idAttribute) {
var idAttr = idAttribute || 'id';
var file = file;
if (!file) {
throw new Error("DB: file name is required");
}
var convertValueToFilter = function (attrs) {
// if a value was provided instead of an object, use the value as an id
if (attrs !== undefined && !_.isObject(attrs)) {
var val = attrs;
attrs = {};
attrs[idAttr] = val;
}
return attrs;
}
/**
* Gets docs in the database optionally filtering them
* @attrs optional object used to call _.where()
* @cb cb Callback returning data
*/
var get = function (attrs, cb) {
//predicate is optional
if (_.isFunction(attrs)) {
cb = attrs;
attrs = undefined;
}
if (!fs.existsSync(file)) {
cb(null, []);
return;
}
fs.readFile(file, 'utf-8', function (err, data) {
if (err) {
cb(err);
return;
}
if (!data || data === '') {
cb(null, []);
return;
}
try {
var docs = JSON.parse(data);
}
catch (err) {
cb(err);
return;
}
attrs = convertValueToFilter(attrs);
if (attrs) {
docs = _.where(docs, attrs);
}
cb(null, docs);
});
};
/**
* Gets a single docs that matches 'attrs'. returns undefined if no doc matches.
* @attrs optional object used to call _.where(). if not an object, used as the key.
* @cb cb Callback returning data
*/
var getSingle = function (attrs, cb) {
get(attrs, function (err, data) {
if (err) {
cb(err, null);
return;
}
var selected = (data && data.length > 0) ? data[0] : undefined;
cb(null, selected);
});
};
/**
* Inserts or updates a doc in the database. If no doc is found
* with the same id attribute, a new other is created, otherwise
* the existing doc is replaced with the given doc.
* @param newDoc The doc
* @param cb Callback for result
*/
var put = function (newDoc, cb) {
get(function (err, docs) {
if (err) {
cb(err);
return;
}
var match = _.filter(docs, function (doc) {
return doc[idAttr] === newDoc[idAttr];
});
if (match.length >= 1) {
_.extend(match[0], newDoc);
}
else {
docs.push(newDoc);
}
fs.writeFile(file, JSON.stringify(docs, null, " "), 'utf-8', cb)
});
};
/**
* Deletes a document with the given id value
* @param attrs Object use to find docs to delete
* @param cb Callback for result
*/
var del = function (attrs, cb) {
if (!fs.existsSync(file)) {
cb(null);
return;
}
get(function (err, docs) {
if (err) {
cb(err);
return;
}
attrs = convertValueToFilter(attrs);
var toDelete = _.where(docs, attrs);
var toKeep = _.difference(docs, toDelete);
fs.writeFile(file, JSON.stringify(toKeep, null, " "), 'utf-8', cb)
})
};
return {
get: get,
getSingle: getSingle,
put: put,
"delete": del
}
}
module.exports = open;