-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
151 lines (134 loc) · 3.67 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
'use strict';
var utils = require('./pouch-utils');
var TaskQueue = require('./taskqueue');
var PREFIX = "db_";
function prefixed(dbName) {
//A database name starting with an underscore is valid, but a document
//id starting with an underscore is not in most cases. Because of
//that, they're prefixed in the all dbs database. See issue #7 for
//more info.
return PREFIX + dbName;
}
function unprefixed(dbName) {
return dbName.slice(PREFIX.length);
}
module.exports = function (Pouch) {
var ALL_DBS_NAME = 'pouch__all_dbs__';
var pouch;
var cache;
var queue = new TaskQueue();
function log(err) {
if (err) {
console.error(err); // shouldn't happen
}
}
function init() {
queue.add(function (callback) {
if (pouch) {
return callback();
}
new Pouch(ALL_DBS_NAME).then(function (db) {
pouch = db;
callback();
}).catch(function (err) {
console.error(err);
callback(err);
});
});
}
function normalize(name) {
return name.replace(/^_pouch_/, ''); // TODO: remove when fixed in Pouch
}
function canIgnore(dbName) {
return (dbName === ALL_DBS_NAME) ||
// TODO: get rid of this when we have a real 'onDependentDbRegistered'
// event (pouchdb/pouchdb#2438)
(dbName.indexOf('-mrview-') !== -1) ||
// TODO: might be a better way to detect remote DBs
(/^https?:\/\//.test(dbName));
}
Pouch.on('created', function (dbName) {
dbName = normalize(dbName);
if (canIgnore(dbName)) {
return;
}
dbName = prefixed(dbName);
init();
queue.add(function (callback) {
pouch.get(dbName).then(function () {
// db exists, nothing to do
}).catch(function (err) {
if (err.name !== 'not_found') {
throw err;
}
return pouch.put({_id: dbName});
}).then(function () {
if (cache) {
cache[dbName] = true;
}
callback();
}, callback);
}, log);
});
Pouch.on('destroyed', function (dbName) {
dbName = normalize(dbName);
if (canIgnore(dbName)) {
return;
}
dbName = prefixed(dbName);
init();
queue.add(function (callback) {
pouch.get(dbName).then(function (doc) {
return pouch.remove(doc);
}).catch(function (err) {
// normally a not_found error; nothing to do
if (err.name !== 'not_found') {
throw err;
}
}).then(function () {
if (cache) {
delete cache[dbName];
}
callback();
}, callback);
}, log);
});
Pouch.allDbs = utils.toPromise(function (callback) {
init();
queue.add(function (callback) {
if (cache) {
return callback(null, Object.keys(cache).map(unprefixed));
}
// older versions of this module didn't have prefixes, so check here
var opts = {startkey: PREFIX, endkey: (PREFIX + '\uffff')};
pouch.allDocs(opts).then(function (res) {
cache = {};
var dbs = [];
res.rows.forEach(function (row) {
dbs.push(unprefixed(row.key));
cache[row.key] = true;
});
callback(null, dbs);
}).catch(function (err) {
console.error(err);
callback(err);
});
}, callback);
});
Pouch.resetAllDbs = utils.toPromise(function (callback) {
queue.add(function (callback) {
pouch.destroy().then(function () {
pouch = null;
cache = null;
callback();
}).catch(function (err) {
console.error(err);
callback(err);
});
}, callback);
});
};
/* istanbul ignore next */
if (typeof window !== 'undefined' && window.PouchDB) {
module.exports(window.PouchDB);
}