-
Notifications
You must be signed in to change notification settings - Fork 1
/
DB.js
204 lines (163 loc) · 5.65 KB
/
DB.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
/**
* @example
* var db = new DB(window.PouchDB, Q);
*
* @param {PouchDB} DB https://github.com/daleharvey/pouchdb
* @param {Q} Deferred https://github.com/kriskowal/q
* @param {Object} options
* @return {DB}
*/
var DB = function (DB, Deferred, _, remoteDbUrl) {
var remoteCouch = false,
_db,
_rdb,
_name,
getDbName = function () {
return _name;
},
_dbCallback = function (description, deferred) {
return function (error, response) {
if (error) {
console.error(error);
if (deferred) {
deferred.reject(new Error(error.error + ':' + error.reason));
}
return;
}
console.info(description);
if (deferred) {
deferred.resolve(response);
}
}
},
/**
* Creates your db for you. How nice.
*
* @param {String} name The name you wish your db to use.
* @return {Promise}
*/
createDB = function (name) {
var deferred = Deferred.defer(),
rdeferred,
localMessage = 'Created db: ' + name;
_name = name;
_db = new DB(
name,
function () {
var remoteDbMessage;
_dbCallback(localMessage, deferred)();
if (!!remoteDbUrl) {
rdeferred = Deferred.defer();
remoteDbMessage = 'Created remote db: ' + remoteDbUrl;
_rdb = new DB(
remoteDbUrl,
function () {
_dbCallback(remoteDbMessage, rdeferred)();
syncDbs();
}
);
}
}
);
return deferred.promise;
},
put = function (data) {
var deferred = Deferred.defer();
_db.put(data, _dbCallback('Put data', deferred));
return deferred.promise;
},
remove = function (data) {
var deferred = Deferred.defer();
_db.remove(data, _dbCallback('Removed data', deferred));
return deferred.promise;
},
get = function (id) {
var deferred = Deferred.defer();
_db.get(id, _dbCallback('Got data: ' + id , deferred));
return deferred.promise;
},
_consoleDocs = function (err, doc) {
console.log(err || doc);
},
onChange = function (callback) {
var options = {
continuous: true,
// callback(change)
onChange : callback
};
_db.info(function (err, info) {
options.since = info.update_seq;
_db.changes(options);
});
},
/**
* options.filter : Reference a filter function from a design document to selectively
* get updates.
*
* options.complete : Function called when all changes have been processed.
* options.onChange : Function called on each change processed.
*
* options.continuous: If true starts subscribing to future changes in the source database
* and continue replicating them.
*
* @param {String} direction Can be 'to' or 'from'.
* @param {Object} options
* @return {Promise}
*/
updateRemote = function (direction, options) {
var deferred,
origOnComplete,
validDirections = ['to', 'from'];
if (!direction || !_(validDirections).indexOf(direction) === -1) {
throw new Error('Direction must be specified');
}
options = options || {};
deferred = Deferred.defer();
console.log(!remoteDbUrl);
if (!remoteDbUrl) {
deferred.reject(new Error('No remote db specified'));
} else {
if (options.complete) {
origOnComplete = options.complete;
}
options.complete = function () {
if (origOnComplete) {
origOnComplete();
}
deferred.resolve();
};
_db.replicate[direction](remoteDbUrl, options);
}
return deferred.promise;
},
updateRemoteDb = function (options) {
return updateRemote('to', options);
},
updateLocalDb = function (options) {
return updateRemote('from', options);
},
syncDbs = function (options) {
updateRemoteDb(options);
updateLocalDb(options);
},
getAll = function () {
var deferred = Deferred.defer(),
options = {
include_docs: true,
descending : true
};
_db.allDocs(options, _dbCallback('Got all', deferred));
return deferred.promise;
};
return {
'onChange' : onChange,
'createDB' : createDB,
'updateLocalDb' : updateLocalDb,
'updateRemoteDb': updateRemoteDb,
'getAll' : getAll,
'getDbName' : getDbName,
'get' : get,
'put' : put,
'remove' : remove
};
};