nativeScript-couchbaselite
一个 NativeScript 插件,提供了所有 Couchbase Mobile 的功能(附件、TTL、map、reduce、group、加密、冲突、livequery、复制……)。它支持 iOS 和 Android。
npm i --save nativescript-couchbaselite

nativeScript-couchbaselite

一个提供所有 couchbase 功能的 nativescript 插件

  • CRUD(带有 ttl...)
  • 冲突管理
  • CRUD 附件(文件、图片...)
  • 视图(map、mapreduce...)
  • 查询(完整查询功能、live 查询、所有文档、group 级别)
  • 复制(拉取和推送、过滤器、身份验证、通道、监听器...)
  • 数据库加密
  • Typescript 映射对象(返回具有 getters/setters 的对象...)

作者

如何使用它

1- 导入依赖项

tns plugin add nativescript-couchbaselite

2- 示例

导入库

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 });

映射 TypeScript 对象

let mapping = new Map<string, any>();
mapping.set("USER", User);
dbTest.setMapping(mapping);

CRUD 文档

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);

CRUD 附件

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);
}
}
})

查询视图

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"] });

使用复合键

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);
}
}
})

使用 MapReduce

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 });

使用 LiveQuery

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
}
}