-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
258 lines (250 loc) · 8.9 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
(function () {
var vue = null;
var pouch = null;
var defaultDB = null;
var defaultUsername = null;
var defaultPassword = null;
var databases = {};
var vuePouch = {
destroyed: function() {
Object.values(this._liveFinds).map(function(lf) { lf.cancel(); });
},
created: function() {
if (!vue) {
console.warn('[vue-pouch] not installed!')
return
}
var defineReactive = vue.util.defineReactive;
var vm = this;
vm._liveFinds = {};
function fetchSession(database) {
if (['http','https'].indexOf(database.adapter) == -1) return;
database.getSession().then(function(res) {
return database.getUser(res.userCtx.name).then(function(res) {
vm.$pouch.session = res;
})
}).catch(function(error) {
vm.$pouch.authError = error
});
}
function login(database) {
if (['http','https'].indexOf(database.adapter) == -1) return;
database.login(defaultUsername, defaultPassword).then(function(res) {
vm.$pouch.session = res;
return database.getUser(defaultUsername).then(function(res) {
vm.$pouch.session = res;
})
}).catch(function(error) {
vm.$pouch.authError = error
})
}
function logout(database) {
database.logout()
}
var $pouch = {
useAuth: function(username, password) {
defaultUsername = username;
defaultPassword = password;
for (var dbString in databases) {
if (!databases.hasOwnProperty(dbString)) continue;
login(databases[dbString]);
}
vm.$pouch.gotAuth = true;
},
createUser: function(username, password) {
defaultDB.signup(username, password).then(function(res) {
vm.$pouch.useAuth(username, password);
}).catch(function(error) {
vm.$pouch.authError = error;
})
},
resetAuth: function() {
defaultUsername = null;
defaultPassword = null;
vm.$pouch.gotAuth = false;
vm.$pouch.session = {}
if (defaultDB) defaultDB.logout()
},
sync: function(localDB, remoteDB, _options) {
if (!databases[localDB]) databases[localDB] = new pouch(localDB);
if (!databases[remoteDB]) databases[remoteDB] = new pouch(remoteDB);
if (!defaultDB) defaultDB = databases[remoteDB];
var options = Object.assign({}, _options, {live: true, retry: true})
var numPaused = 0;
vm.$pouch.loading[localDB] = true
// defineReactive(vm, '$pouch.ready', vm.$pouch.ready)
return pouch.sync(databases[localDB], databases[remoteDB], options)
.on('paused', function (err) {
if (err) {
vm.$pouch.errors[localDB] = err
vm.$pouch.errors = Object.assign({}, vm.$pouch.errors)
return;
}
numPaused += 1;
if (numPaused >= 2) {
vm.$pouch.loading[localDB] = false
vm.$pouch.loading = Object.assign({}, vm.$pouch.loading)
}
})
.on('active', function () {
// console.log('active callback')
})
.on('denied', function (err) {
vm.$pouch.errors[localDB] = err
vm.$pouch.errors = Object.assign({}, vm.$pouch.errors)
// console.log('denied callback')
})
.on('complete', function (info) {
// console.log('complete callback')
})
.on('error', function (err) {
vm.$pouch.errors[localDB] = err
vm.$pouch.errors = Object.assign({}, vm.$pouch.errors)
})
fetchSession(databases[remoteDB]);
},
push: function(localDB, remoteDB, options) {
if (!databases[localDB]) databases[localDB] = new pouch(localDB);
if (!databases[remoteDB]) databases[remoteDB] = new pouch(remoteDB);
if (!defaultDB) defaultDB = databases[remoteDB];
fetchSession(databases[remoteDB]);
return databases[localDB].replicate.to(databases[remoteDB], options)
.on('paused', function (err) {
// console.log('paused callback')
})
.on('active', function () {
// console.log('active callback')
})
.on('denied', function (err) {
vm.$pouch.errors[localDB] = err
vm.$pouch.errors = Object.assign({}, vm.$pouch.errors)
// console.log('denied callback')
})
.on('complete', function (info) {
// console.log('complete callback')
})
.on('error', function (err) {
vm.$pouch.errors[localDB] = err
vm.$pouch.errors = Object.assign({}, vm.$pouch.errors)
// console.log('error callback')
})
},
put: function(db, object, options) {
return databases[db].put(object, options || {});
},
post: function(db, object, options) {
return databases[db].post(object, options || {});
},
remove: function(db, object, options) {
return databases[db].remove(object, options || {});
},
get: function(db, object, options) {
return databases[db].get(object, options || {});
},
session: {},
errors: {},
loading: {},
authError: null,
gotAuth: false
}
defineReactive(vm, '$pouch', $pouch);
vm.$databases = databases; // Add non-reactive property
var pouchOptions = this.$options.pouch;
if (!pouchOptions) return;
if (typeof pouchOptions == 'function') pouchOptions = pouchOptions();
Object.keys(pouchOptions).map(function(key) {
var pouchFn = pouchOptions[key];
if (typeof pouchFn !== 'function') {
pouchFn = function() {
return pouchOptions[key];
}
}
if (typeof vm.$data[key] == 'undefined') vm.$data[key] = null;
defineReactive(vm, key, null);
vm.$watch(pouchFn, function(config) {
if (!config) {
if (!vm[key]) vm[key] = []
return;
}
var selector, sort, skip, limit, first;
if (config.selector) {
selector = config.selector;
sort = config.sort;
skip = config.skip;
limit = config.limit;
first = config.first;
}
else {
selector = config
}
var databaseParam = config.database || key;
var db = null;
if (typeof databaseParam == 'object') {
db = databaseParam;
}
else if (typeof databaseParam == 'string') {
if (!databases[databaseParam]) {
databases[databaseParam] = new pouch(databaseParam);
if (vm.$pouch.gotAuth) login(databases[databaseParam]);
}
db = databases[databaseParam];
}
if (!db) return;
if (vm._liveFinds[key]) vm._liveFinds[key].cancel()
var aggregateCache = []
vm._liveFinds[key] = db.liveFind({
selector: selector,
sort: sort,
skip: skip,
limit: limit,
aggregate: true
}).on('update', function(update, aggregate) {
if (first && aggregate) aggregate = aggregate[0]
vm[key] = aggregateCache = aggregate;
}).on('ready', function() {
vm[key] = aggregateCache;
})
}, {
immediate: true
})
})
}
}
function installSelectorReplicationPlugin() {
// This plugin enables selector-based replication
pouch.plugin(function(pouch) {
var oldReplicate = pouch.replicate
pouch.replicate = function(source, target, repOptions) {
var sourceAjax = source._ajax
source._ajax = function(ajaxOps, callback) {
if (ajaxOps.url.includes('_selector')) {
ajaxOps.url = ajaxOps.url.replace('filter=_selector%2F_selector', 'filter=_selector')
ajaxOps.method = 'POST'
ajaxOps.body = {
selector: repOptions.selector
}
}
return sourceAjax(ajaxOps, callback)
}
return oldReplicate(source, target, repOptions)
}
})
}
var api = {
mixin: vuePouch,
install: function (Vue, options) {
vue = Vue;
pouch = (options && options.pouch) || PouchDB;
installSelectorReplicationPlugin()
defaultDB = (options && options.defaultDB);
Vue.options = Vue.util.mergeOptions(Vue.options, vuePouch);
}
}
if(typeof exports === 'object' && typeof module === 'object') {
module.exports = api
} else if(typeof define === 'function' && define.amd) {
define(function () { return api })
} else if (typeof window !== 'undefined') {
window.VuePouch = api
}
})()