forked from pouchdb/plugin-seed
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
62 lines (54 loc) · 1.6 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
'use strict';
var utils = require('./pouch-utils');
var datascript = require('datascript-async');
var lowerCase = function (key) {
if (!key) {
return key;
}
return key.toLowerCase();
};
exports.dataquery = utils.toPromise(function (query, dataquery_callback) {
var self = this;
var searchPouchIndex = function (db, index) {
return function (startkey, endkey, index_callback) {
var view = index + "/" + index;
if (index === "eav") {
startkey = [startkey.e, startkey.a, startkey.v];
endkey = [endkey.e, endkey.a, endkey.v];
} else if (index === "ave") {
startkey = [startkey.a, startkey.v, startkey.e];
endkey = [endkey.a, endkey.v, endkey.e];
}
startkey = startkey.map(lowerCase);
endkey = endkey.map(lowerCase);
endkey = endkey.map(function (el) {
if (el === null) {
return {};
}
return el;
});
return db.query(view, {
startkey: startkey,
endkey: endkey
}).then(function (data) {
var result = data.rows.map(function (el) {
return el.value;
});
index_callback(result);
}).catch(function (error) {
console.error("An error occured.", error);
});
};
};
var db = datascript.set_indexes(searchPouchIndex(self, "eav"), searchPouchIndex(self, "ave"));
return datascript.q(function (err, data) {
if (err) {
return err;
}
return dataquery_callback(null, data);
}, query, db);
});
/* istanbul ignore next */
if (typeof window !== 'undefined' && window.PouchDB) {
window.PouchDB.plugin(exports);
}