forked from countable/json-crud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
209 lines (201 loc) · 6.56 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
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
198
199
200
201
202
203
204
205
206
207
208
209
// Generated by CoffeeScript 1.10.0
(function() {
var _, async, debounce, exec, fs, path;
fs = require('fs');
exec = require('child_process').exec;
_ = require('underscore');
async = require('async');
debounce = require('./lib/debounce');
path = require('path');
module.exports = function(db_name, options) {
var api, collections, commit, dirty_collections, flush, mark_dirty, url_path;
if (options == null) {
options = {};
}
collections = {};
dirty_collections = {};
db_name += '.db';
if (options.db_location) {
db_name = path.join(options.db_location, db_name);
}
if (options.url_path) {
url_path = options.url_path + 'data/';
} else {
url_path = '/data/';
}
exec('mkdir -p ' + db_name, function(err, out, serr) {
if (err) {
throw err;
}
});
mark_dirty = function(collection) {
dirty_collections[collection] = 1;
return commit();
};
flush = function(done) {
return async.map(Object.keys(dirty_collections), function(collection, callback) {
var filename;
filename = db_name + "/" + collection + ".json";
console.log('writing', filename);
return fs.writeFile(filename, JSON.stringify(collections[collection], null, 2), function(err) {
if (err) {
throw err;
}
return callback();
});
}, function() {
dirty_collections = {};
return typeof done === "function" ? done() : void 0;
});
};
commit = debounce(flush, 200);
return api = {
drop: function(collection, done) {
return this.collection(collection, (function(_this) {
return function() {
collections[collection] = null;
if (dirty_collections[collection]) {
delete dirty_collections[collection];
}
return fs.unlink(db_name + "/" + collection + ".json", function(err) {
if (err) {
throw err;
}
return done();
});
};
})(this));
},
collection: function(collection, done) {
var filename, load_collection;
filename = db_name + "/" + collection + ".json";
load_collection = function() {
return fs.readFile(filename, function(err, result) {
if (err) {
throw err;
}
if (!collections[collection]) {
collections[collection] = JSON.parse(result + '');
}
return done(err);
});
};
if (collections[collection] != null) {
return done(null);
} else {
return fs.exists(filename, function(exists) {
if (exists) {
return load_collection();
} else {
collections[collection] = [];
mark_dirty(collection);
return flush(function() {
return done(null);
});
}
});
}
},
all: function(collection, done) {
return this.collection(collection, (function(_this) {
return function() {
return done(null, collections[collection]);
};
})(this));
},
del: function(collection, record, done) {
if (typeof record !== 'object') {
record = {
_id: record
};
}
return this.collection(collection, (function(_this) {
return function() {
collections[collection] = _.filter(collections[collection], function(row) {
return row._id !== record._id;
});
mark_dirty(collection);
return typeof done === "function" ? done() : void 0;
};
})(this));
},
put: function(collection, record, done) {
return this.collection(collection, (function(_this) {
return function() {
if (record._id) {
collections[collection] = _.filter(collections[collection], function(row) {
return row._id !== record._id;
});
}
collections[collection].push(record);
mark_dirty(collection);
return typeof done === "function" ? done() : void 0;
};
})(this));
},
restify: function(app) {
var counter, post_json, store;
store = this;
app.get(url_path + ":collection/:id/del", function(req, res) {
return store.del(req.params.collection, {
_id: req.params.id
}, function(err) {
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
return res.send({
success: !err,
message: err || ''
});
});
});
app.get(url_path + ":collection", function(req, res) {
return store.all(req.params.collection, function(err, results) {
if (err) {
throw err;
}
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
return res.send(results);
});
});
counter = 0;
post_json = function(req, res) {
var base, obj;
if ((base = req.params).id == null) {
base.id = (new Date).valueOf() + '' + counter;
}
counter += 1;
obj = JSON.parse(req.body.data);
if (obj._id == null) {
obj._id = req.params.id;
}
return store.put(req.params.collection, obj, function(err) {
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
return res.send({
success: !err,
message: err || ''
});
});
};
app.post(url_path + ":collection/:id", post_json);
return app.post(url_path + ":collection", post_json);
},
collections: function(done) {
return exec('mkdir -p ' + db_name, function(err, out, serr) {
if (err) {
throw err;
}
return fs.readdir(db_name, function(err, files) {
if (err) {
throw err;
}
return async.map(files, function(file, callback) {
file = file.replace(".json", "");
return api.collection(file, callback);
}, function() {
return done(collections);
});
});
});
}
};
};
}).call(this);