nativescript-couchdb
by Jeffry L | v0.6.23
Couchdb 插件用于 nativescript
npm i --save nativescript-couchdb

nativescript-couchdb

Nativescript 插件用于 Couchdb。使用 nativescript http api 实现与 PouchDB 兼容的子集 API 的简单包装。

安装

tns plugin install nativescript-couchdb

API

constructor(couchdb_url, extraHeaders) 设置要连接的数据库

  • url string 例如:https://localhost:5984
  • extraHeaders Map 指定作为 http header 传递的额外映射

put(doc)

  • doc string 包含 _id 键的有效 couchdb json 文档
  • 返回 promise

get(docId)

  • docId string 文档 id
  • 返回 promise

remove(doc)

  • doc json object couchdb json db 或包含 _id, _rev 键的 json
  • 返回 promise

allDocs(options)

  • options json object couchdb 参数,如 https://docs.couchdb.cn/en/2.0.0/api/database/bulk-api.html 中所述
  • 返回 promise

query(design_view)

  • design_view 字符串,例如 /_design/design_name/_view/view_name 将是 design_name/view_name
  • 返回 promise

使用方法


import * as dialog from "ui/dialogs";
import { CouchDB } from "nativescript-couchdb";

let db = new CouchDB("https://couchdb.server/dbname", {
"Authorization": "Basic base64\_encoded\_string"
});
let data = {
_id: "hello",
name: "world"
}


// create and update
db.put(data)
.then(res => dialog.alert("saved"))
.catch(err => dialog.alert("Failed"));

// get data
db.get("hello")
.then(res => dialog.alert(JSON.stringify(res)))
.catch(err => dialog.alert("Data not found));

// delete doc
db.remove(data)
.then(res => dialog.alert("
Data deleted"))
.catch(err => dialog.alert("
Delete failed"));

// alldocs
db.allDocs(options)
.then(res => dialog.alert(res))
.catch(err => dialog.alert(err));

// query views
db.query("
user/top_contributor", { group_level: 1, reduce: true })
.then(res => dialog.alert(res))
.catch(err => dialog.alert(err));