-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
73 lines (67 loc) · 1.73 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
var levelup = require('levelup');
var leveldown = require('leveldown');
module.exports = LevelStore;
function LevelStore (options, cb) {
if (typeof options === 'string') {
options = {
name: options
};
}
if (typeof options === 'function') {
cb = options;
options = {};
}
options = options || {};
this.options = options;
this.name = options.name || 'LevelStore';
if (options.defaultCallback) {
this.defaultCallback = options.defaultCallback;
}
cb = cb || this.defaultCallback;
var levelOpts ={};
Object.keys(options).forEach(function(key) {
levelOpts[key] = options[key];
});
levelOpts.db = levelOpts.db || leveldown;
levelOpts.valueEncoding = 'json';
this.db = levelup(this.name, levelOpts, cb);
this.close = this.db.close.bind(this.db);
}
LevelStore.prototype.add = function (geojson, callback) {
callback = callback || this.defaultCallback;
if (geojson.type === "FeatureCollection") {
this.db.batch(geojson.features.map(function (feature) {
return {
type: 'put',
key: feature.id,
value: feature
};
}), callback);
} else {
this.db.put(geojson.id, geojson, callback);
}
};
LevelStore.prototype.get = function (id, callback) {
this.db.get(id, callback);
};
LevelStore.prototype.update = function (geojson, callback) {
this.add(geojson, callback);
};
LevelStore.prototype.remove = function (id, callback) {
callback = callback || this.defaultCallback;
if (Array.isArray(id)){
this.db.batch(id.map(function (id) {
return {
type: 'del',
key: id
};
}), callback);
} else {
this.db.del(id, callback);
}
};
LevelStore.prototype.defaultCallback = function (err) {
if (err) {
throw err;
}
};