如何使用pouchdb-adapter-leveldb设置Rxdb?

时间:2018-08-20 13:15:16

标签: reactjs react-redux electron pouchdb

我正在开发电子应用,正在使用react,react-redux。现在,我试图将Rxdb和pouchdb-adapter-leveldb设置为脱机模式,我以Rxdb(react)-示例为例,对pouchdb-adapter-leveldb进行了一些更改。这是我的database.js文件:

import * as RxDB from 'rxdb';

var PouchDB = require('pouchdb-core')
    .plugin(require('pouchdb-adapter-leveldb'));
RxDB.plugin(require('pouchdb-adapter-leveldb'));

const collections = [
    {
        name: 'heroes',
        schema: require('./Schema.js').default,
        methods: {
            hpPercent() {
                return this.hp / this.maxHP * 100;
            }
        },
        sync: true
    }
];

const syncURL = 'http://localhost:3000/teams';
console.log('host: ' + syncURL);
// const syncURL = host;

let dbPromise = null;

const _create = async function() {


    console.log('DatabaseService: creating database..');

    const db = await RxDB.create({name: 'heroesreactdb', adapter: 'leveldb', password: 'myLongAndStupidPassword'});
    console.log('DatabaseService: created database');
    window['db'] = db; // write to window for debugging

    // show leadership in title
    db.waitForLeadership().then(() => {
        console.log('isLeader now');
        document.title = '♛ ' + document.title;
    });

    // create collections
    console.log('DatabaseService: create collections');
    await Promise.all(collections.map(colData => db.collection(colData)));

    // hooks
    console.log('DatabaseService: add hooks');
    db.collections.heroes.preInsert(function(docObj) {
        const color = docObj.color;
        return db.collections.heroes.findOne({color}).exec().then(has => {
            if (has != null) {
                alert('another hero already has the color ' + color);
                throw new Error('color already there');
            }
            return db;
        });
    });

    // sync
    console.log('DatabaseService: sync');
    collections.filter(col => col.sync).map(col => col.name).map(colName => db[colName].sync({
        remote: syncURL + colName + '/'
    }));

    return db;
};

export function get() {
    if (!dbPromise)
        dbPromise = _create();
    return dbPromise;
}

![and this is my error notification: ] 1

当我看到此错误消息时,我删除了nod-modules,并安装了节点绑定(一个人建议此解决方案),但它对我不起作用。任何想法如何解决这个问题? :/谢谢!!!

0 个答案:

没有答案