A nativescript plugin that brings you all couchbase features:
tns plugin add nativescript-couchbaselite
import {
QueryResult, LiveQuery, QueryListener, Revision,
DatabaseManager, Document, Database, AttachmentFactory, Emitter, AttachmentImage
} from 'nativescript-couchbaselite';
class User implements Document {
docId: string;
docRev: string;
docType: string = "USER";
name: string;
registerAt: number;
secure: boolean = false;
set registerAtDate(d: Date) {
this.registerAt = d.getTime();
}
get registerAtDate() {
return this.registerAt ? new Date(this.registerAt) : null;
}
@Type(() => Group) group: Group = new Group;
getName() {
return this.name;
}
}
let dbTest = DatabaseManager.getOrCreate({ name: "test", encryptionKey: "SECURE", create: true });
let mapping = new Map<string, any>();
mapping.set("USER", User);
dbTest.setMapping(mapping);
let user = new User;
user.name = "user1";
user.group.name = "group1";
user.registerAtDate = now;
user.secure = true;
dbTest.createDocument(user, "ID", { ttl: new Date() });
let fetched: User = <User>dbTest.getDocument(user.docId);
console.log(fetched.getName());
fetched.group.name = "group2";
dbTest.updateDocument(fetched.docId, fetched);
let success = dbTest.deleteDocument(user.docId);
let source = fromResource("icon");
let attach = AttachmentFactory.fromSource(source, "yeah", AttachmentImage.PNG);
dbTest.setAttachment("ID", attach);
let attachments = dbTest.getAttachmentNames("ID");
let attach = dbTest.getAttachment("ID", "yeah");
removeAttachment("ID","yeah")
dbTest.createView({
name: "users",
revision: "1",
map: (doc: User, emitter) => {
if (doc.docType == "USER") {
emitter.emit(doc.name.toLowerCase(), null);
}
}
})
Querying a View returns a QueryResult object which has "documents" property that contains all the documents that matched our query. This document property will be null unless you call the getDocuments() method of the QueryResult. getDocuments() will return all the documents and will also fill up the QueryResult's "documents" property;
let founded = dbTest.queryView("users", { mapOnly: true });
founded = dbTest.queryView("users", { mapOnly: true, startKey: "user4" });
founded = dbTest.queryView("users", { mapOnly: true, endKey: "user0" });
founded = dbTest.queryView("users", { mapOnly: true, startKeyDocID: "ID4" });
founded = dbTest.queryView("users", { mapOnly: true, endKeyDocID: "ID0" });
founded = dbTest.queryView("users", { mapOnly: true, descending: true });
founded = dbTest.queryView("users", { mapOnly: true, limit: 2 });
founded = dbTest.queryView("users", { mapOnly: true, skip: 3 });
founded = dbTest.queryView("users", { mapOnly: true, keys: ["user1", "user2"] });
let docs = founded.getDocuments();
dbTest.createView({
name: "users_compound",
revision: "1",
map: (doc: User, emitter) => {
if (doc.docType == "USER") {
emitter.emit([doc.getName().toLowerCase(), doc.group.name.toLowerCase(), doc.registerAt, doc.registerAtDate, doc.secure], null);
}
}
})
dbTest.createView({
name: "users_bygroup",
revision: "1",
map: (doc: User, emitter) => {
if (doc.docType == "USER") {
emitter.emit([doc.group.name, doc.getName().toLowerCase()], doc.name);
}
},
reduce: (keys: any[], values: any[], rereduce: boolean) => {
return values.length;
}
});
let founded = dbTest.queryView("users_bygroup", { mapOnly: false, groupLevel: 1 });
let groups = founded.getValues();
let founded = dbTest.queryView("users_bygroup", { mapOnly: false, groupLevel: 1 });
let groups = founded.getValues();
let value = founded.firstValue();
let docs = <User[]>founded.getDocuments();
let doc = founded.firstDocument();
let docIds = founded.getDocumentIds();
let founded = dbTest.queryAllDocuments({ mapOnly: true });
let listener = {
onRows(rows: QueryResult) {
}
};
let live = dbTest.liveQuery("users_live", { mapOnly: true }, l);
live.start();
live.waitForRows();
live.stop();
let url = "http://localhost:4984/test/";
let push = dbTest.createPushReplication(url);
push.setBasicAuthenticator("user", "password");
dbTest.createFilter({
name: "filter",
filter: (doc: Revision<Document>, params: Map<string, any>) => {
return doc.id == "ID1";
}
});
push.setFilter("filter");
let listener = {
count: 0,
onChange: (p) => {
l.count = p.changesCount;
}
};
push.addChangeListener(listener)
push.start();
let url = "http://localhost:4984/test/";
let pull = dbTest.createPullReplication(url);
pull.setBasicAuthenticator("user", "password");
let pullCallback = {
countEvent: 0,
onChange: (p) => {
pullCallback.countEvent++;
}
}
pull.channels(["channel4"]);
pull.setDocIds(["ID1"]);
pull.addChangeListener(pullCallback);
pull.start();
let conflicts = dbTest.getConflicts("ID");
let merged = mergeConflict(conflicts);
dbTest.resolveConflict("ID", merged);
dbTest.addChangeListener({
onChange:(params:DatabaseListenerParam[])=>{
}
});
class CustomAttachment implements Attachment{
getName() => {
return name;//String
},
getStream () => {
return bs;//InputStream or NSData
},
getType () => {
return "any/any";//Content Type
}
}