RxDB 与 PouchDB 紧密耦合,并在后台使用其同步实现。据我了解,将自定义标头添加到远程 PouchDB 实例的唯一方法(这是当您传递 url 作为remote
论证中sync
), 是为了拦截HTTP请求:
var db = new PouchDB('http://example.com/dbname', {
fetch: function (url, opts) {
opts.headers.set('X-Some-Special-Header', 'foo');
return PouchDB.fetch(url, opts);
}
});
PouchDB 复制文档(sync) 还指出:
RemoteDB 可以是字符串或 PouchDB 对象。如果您在远程数据库上进行了提取覆盖,您将需要使用 PouchDB 对象而不是字符串,以便使用这些选项。
幸运的是,RxDBRx.Collection.sync
不仅接受服务器 url 作为remote
参数,但也是另一个 RxCollection 或 PouchDB 实例。
RxDB 甚至重新导出内部使用的 PouchDB 模块,因此您不必将 PouchDB 安装为直接依赖项。
import { ..., PouchDB } from 'rxdb';
// ...
const remotePouch = new PouchDB('http://27.0.0.1:5984/tododb', {
fetch: function (url, opts) {
opts.headers.set('Authorization', `Bearer ${getYourJWTToken()}`)
return PouchDB.fetch(url, opts);
}
})
database.tasks.sync({
remote: remotePouch,
});