-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
189 lines (148 loc) · 3.47 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
/**
* Module dependencies
*/
var debug = require('debug')('modella:leveldb');
var indexing = require('leveldex');
var sublevel = require('level-sub');
var level = require('level-11');
var noop = function() {};
var sync = {};
/**
* Export `LevelDB`
*/
module.exports = function(path, options) {
options = options || {};
options.valueEncoding = options.valueEncoding || 'json';
// allow you to pass your own instance in
var db = path.get && path.put ? path : level(path, options);
return function(model) {
model.db = sublevel(db).sublevel(model.modelName);
indexing(model.db);
for (fn in sync) model[fn] = sync[fn];
};
};
/**
* Index
*/
sync.index = function(key, opts) {
opts = opts || {};
// index the DB usuing level-indexing
this.db.index(key);
// ensure the key is unique
opts.unique && this.db.unique(key);
return this;
};
/**
* All
*/
sync.all = function(options, fn) {
options = options || {}
fn = fn || noop;
if ('function' == typeof options) {
fn = options;
options = {};
}
var model = this;
// default options
options.valueEncoding = options.valueEncoding || 'json';
debug('getting all data with options %j', options);
var rs = this.db.createReadStream(options);
var buffer = [];
rs.on('error', function(err) {
return fn(err);
});
rs.on('data', function(data) {
buffer[buffer.length] = data.value;
});
rs.on('end', function() {
return fn(null, buffer.map(model));
});
};
/**
* Get
*/
sync.get =
sync.find = function(key, options, fn) {
options = options || {};
fn = fn || noop;
if('function' == typeof options) {
fn = options;
options = {};
}
var db = this.db;
var model = this;
// default options
options.encoding = options.encoding || 'json';
debug('getting %j with %j options...', key, options);
if ('object' == typeof key) {
var k = Object.keys(key)[0];
var v = key[k];
db.by(k, v, options, function(err, json) {
if (err) return err.notFound ? fn(null, null) : fn(err);
debug('got %j', json);
return fn(null, model(json));
});
} else {
db.get(key, options, function(err, json) {
if (err) return err.notFound ? fn(null, null) : fn(err);
debug('got %j', json);
return fn(null, model(json));
});
}
};
/**
* removeAll
*/
sync.removeAll = function(query, fn) {
throw new Error('model.removeAll not yet implemented');
};
/**
* batch
*/
sync.batch = function () {
throw new Error('model.batch not yet implemented');
};
/**
* save
*/
sync.save =
sync.update = function(options, fn) {
options = options || {};
fn = fn || noop;
if ('function' == typeof options) {
fn = options;
options = {};
}
// default options
options.encoding = options.encoding || 'json';
var json = this.toJSON();
debug('saving... %j', json);
var id = this.primary();
this.model.db.put(id, json, options, function(err) {
if(err) return fn(err);
debug('saved %j', json);
// do not pass body through,
// modella messes with the primary key
// when it's not an id
return fn();
});
};
/**
* remove
*/
sync.remove = function(options, fn) {
options = options || {};
fn = fn || noop;
if ('function' == typeof options) {
fn = options;
options = {};
}
var db = this.model.db;
var id = this.primary();
debug('removing %s with options %j', id, options);
db.del(id, options, function(err) {
if(err) return fn(err);
debug('removed %s', id);
return fn();
});
};