forked from couchbaselabs/nativescript-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
couchbase.android.ts
297 lines (258 loc) · 10.4 KB
/
couchbase.android.ts
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import * as utils from "utils/utils";
//import fs = require("file-system");
declare var com: any;
declare var java: any;
declare var android: any;
export class Couchbase {
private context: any;
private manager: any;
private database: any;
public constructor(databaseName: string) {
this.context = utils.ad.getApplicationContext();
try {
this.manager = new com.couchbase.lite.Manager(new com.couchbase.lite.android.AndroidContext(this.context), null);
this.database = this.manager.getDatabase(databaseName);
} catch (exception) {
console.error("MANAGER ERROR:", exception.message);
throw new Error("MANAGER ERROR: " + exception.message);
}
}
public createDocument(data: Object, documentId?: string) {
var document: any = documentId == null ? this.database.createDocument() : this.database.getDocument(documentId);
var documentId: string = document.getId();
try {
document.putProperties(this.objectToMap(data));
} catch (exception) {
console.error("DOCUMENT ERROR:", exception.message);
throw new Error("DOCUMENT ERROR: " + exception.message);
}
return documentId;
}
public getDocument(documentId: string) {
var document: any = this.database.getDocument(documentId);
return JSON.parse(this.mapToJson(document.getProperties()));
}
public updateDocument(documentId: string, data: any) {
let document: any = this.database.getDocument(documentId);
let temp: any = JSON.parse(this.mapToJson(document.getProperties()));
data._id = temp._id;
data._rev = temp._rev;
try {
document.putProperties(this.objectToMap(data));
} catch (exception) {
console.error("DOCUMENT ERROR", exception.message);
throw new Error("DOCUMENT ERROR: " + exception.message);
}
}
public deleteDocument(documentId: string) {
var document: any = this.database.getDocument(documentId);
try {
document.delete();
} catch (exception) {
console.error("DOCUMENT ERROR", exception.message);
}
return document.isDeleted();
}
public destroyDatabase() {
try {
this.database.delete();
} catch (exception) {
console.error("DESTROY", exception.message);
}
}
public createView(viewName: string, viewRevision: string, callback: any) {
var view = this.database.getView(viewName);
var self = this;
view.setMap(new com.couchbase.lite.Mapper({
map(document, emitter) {
let e = new Emitter(emitter);
callback(JSON.parse(self.mapToJson(document)), e);
}
}), viewRevision);
}
public executeQuery(viewName: string, options?: any) {
var query = this.database.getView(viewName).createQuery();
if(options != null) {
if(options.descending) {
query.setDescending(options.descending);
}
if(options.limit) {
query.setLimit(options.limit);
}
if(options.skip) {
query.setSkip(options.skip);
}
if(options.startKey) {
query.setStartKey(options.startKey);
}
if(options.endKey) {
query.setEndKey(options.endKey);
}
}
var result = query.run();
var parsedResult: Array<any> = [];
while(result.hasNext()) {
var row = result.next();
parsedResult.push(this.mapToObject(row.getValue()));
}
return parsedResult;
}
public createPullReplication(remoteUrl: string) {
var replication;
try {
replication = this.database.createPullReplication(new java.net.URL(remoteUrl));
} catch (exception) {
console.error("PULL ERROR", exception.message);
throw new Error("PULL ERROR: " + exception.message);
}
return new Replicator(replication);
}
public createPushReplication(remoteUrl: string) {
var replication;
try {
replication = this.database.createPushReplication(new java.net.URL(remoteUrl));
} catch (exception) {
console.error("PUSH ERROR", exception.message);
throw new Error("PUSH ERROR: " + exception.message);
}
return new Replicator(replication);
}
public addDatabaseChangeListener(callback: any) {
try {
this.database.addChangeListener(new com.couchbase.lite.Database.ChangeListener({
changed(event) {
let changes: Array<any> = event.getChanges().toArray();
callback(changes);
}
}));
} catch (exception) {
console.error("DATABASE LISTENER ERROR", exception.message);
}
}
private objectToMap(data: Object) {
var gson = (new com.google.gson.GsonBuilder()).create();
return gson.fromJson(JSON.stringify(data), (new java.util.HashMap).getClass());
}
private mapToJson(data: Object) {
var gson = (new com.google.gson.GsonBuilder()).create();
return gson.toJson(data);
}
private mapToObject(data: Object) {
var gson = (new com.google.gson.GsonBuilder()).create();
return JSON.parse(gson.toJson(data));
}
/*private getPath(uri) {
let cursor = applicationModule.android.currentContext.getContentResolver().query(uri, null, null, null, null);
if (cursor == null) return null;
let column_index = cursor.getColumnIndexOrThrow(android.provider.MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
let s = cursor.getString(column_index);
cursor.close();
return s;
}
getAttachment(documentId: string, attachmentId: string): Promise<any> {
return new Promise((resolve, reject) => {
let document = this.database.getDocument(documentId);
let rev = document.getCurrentRevision();
let att = rev.getAttachment(attachmentId);
if (att != null) {
resolve(att.getContent());
} else {
reject("Sorry can't process your request");
}
})
}
setAttachment(documentId: string, attachmentId: string, file: string): Promise<any> {
return new Promise((resolve, reject) => {
let document = this.database.getDocument(documentId);
let newRev = document.getCurrentRevision().createRevision();
if (file.toString().substr(0, 10).indexOf('content://') > -1) {
let stream = applicationModule.android.context.getContentResolver().openInputStream(file);
let fileExtension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(this.getPath(file));
let mimeType = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
try {
newRev.setAttachment(attachmentId, mimeType, stream);
newRev.save();
resolve();
} catch (exception) {
reject(exception.message);
}
} else if (file.toString().substr(0, 7).indexOf('file://') > -1) {
let stream = applicationModule.android.context.getContentResolver().openInputStream(android.net.Uri.fromFile(file));
let fileExtension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(file);
let mimeType = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
try {
newRev.setAttachment(attachmentId, mimeType, stream);
newRev.save();
resolve();
} catch (exception) {
reject(exception.message);
}
} else if (file.substr(0, 2).indexOf('~/') > -1) {
let path = fs.path.join(fs.knownFolders.currentApp().path, file.replace('~/', ''));
let stream = applicationModule.android.context.getContentResolver().openInputStream(android.net.Uri.fromFile(new java.io.File(path)));
let fileExtension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(path);
let mimeType = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
try {
newRev.setAttachment(attachmentId, mimeType, stream);
newRev.save();
resolve();
} catch (exception) {
reject(exception.message);
}
}
})
}
removeAttachment(documentId: string, attachmentId: string): Promise<any> {
return new Promise((resolve, reject) => {
let document = this.database.getDocument(documentId);
let newRev = document.getCurrentRevision().createRevision();
try {
newRev.removeAttachment(attachmentId);
newRev.save();
resolve();
} catch (exception) {
reject(exception.message);
}
})
}*/
}
export class Replicator {
replicator: any;
constructor(replicator: any) {
this.replicator = replicator;
}
start() {
this.replicator.start();
}
stop() {
this.replicator.stop();
}
isRunning() {
return this.replicator.isRunning;
}
setContinuous(isContinuous: boolean) {
this.replicator.setContinuous(isContinuous);
}
setCookie(name: String, value: String, path: String, expirationDate: Date, secure: boolean, httpOnly: boolean) {
let date = new java.util.Date(expirationDate.getTime());
this.replicator.setCookie(name, value, path, date, secure, httpOnly);
};
deleteCookie(name: String) {
this.replicator.deleteCookieNamed(name);
}
}
export class Emitter {
public emitter: any;
constructor(emitter: any) {
this.emitter = emitter;
}
emit(key: Object, value: Object) {
if(typeof value === "object") {
var gson = (new com.google.gson.GsonBuilder()).create();
this.emitter.emit(key, gson.fromJson(JSON.stringify(value), (new java.util.HashMap).getClass()));
} else {
this.emitter.emit(key, value);
}
}
}