B

bsync-client

df1fe549 fix: node downloader · by Stefan Huber

bsync-client

Allows downloading of files referenced by a http/https URI inside a couchdb/pouchdb record. The plugin works for cordova and electron projects.

Usage

Use it with pouchdb

import {ServiceLocator} from 'bsync-client/dist/browser-build';

ServiceLocator.getConfig().setConfig('itemKey', 'type');
ServiceLocator.getConfig().setConfig('itemValue', 'asset');

let localDb = new PouchDB('local-pouch-db');
let fileReplicator = ServiceLocator.getFileReplicator();

localDb.put({
    _id : "_design/index_type",
    views : {
        type : {
            map : function(doc) {
                if (doc[fileReplicator.itemKey]) { emit(doc[fileReplicator.itemKey]); }
            }.toString()
        }
    }
});  

fileReplicator.once('complete', () => {
    // All file are downloaded
});

db.query('index_type/type',{
    include_docs : true,
    key : fileReplicator.itemValue
}).then((res) => { 
    let docs = { docs : [] };                      
    for (let r of res.rows) {
        docs.docs.push(r.doc);
    }

    fileReplicator.pushChanges(docs);
    fileReplicator.start();
});

In electron projects

Within the main process bsync needs to be integrated an initiated.

import {Bsync} from 'bsync';
Bsync.init(ipcMain, filePath);

Testing (Browser/Node)

npm test

Testing (Cordova)

npm run test:cordova npm run test:cordova -- run ios npm run test:cordova -- emulate ios