Sunday, 12 March 2017

PouchDB issues with node module

I'm having a weird issue, I believe I am just overlooking something or do not understand the Node module.exports mechanism correctly.

I am trying to create a database service that holds my PouchDB connections and other service can simply require the database service. I am also replicating the remote database to a local database for performance reasons.

The database service looks something like this.

database.service.js

var __remote = new PouchDB(config.connectionstring.datastore);
var __local = new PouchDB("pouch-local-database");

var __replicationHandle = __startReplication();

function __startReplication() {
  return __local.sync(__remote, {
    live: true,
    retry: true
  }).on('change', function (change) {
    logger.info('Database Sync New Data');
  }).on('paused', function (info) {
    logger.info('Database Sync Paused');
  }).on('active', function (info) {
    logger.info('Database Sync Active');
  }).on('error', function (err) {
      logger.error('Database Sync Error', err);
  });
}

function __resync(){
  __replicationHandle.cancel();
  __local.destroy()
  .then(function(data){
    __local = new PouchDB("pouch-local-database");
    __replicationHandle = __startReplication();
  })
  .catch(function(err){
    logger.error('Database Destroy Error', err);
  });
}   

module.exports = {
    data : __local,
    resync: __resync
};

The two issues I am getting is:

  1. The replication works like a charm when I host it locally and the app responsiveness is very good, but when I upload it to my Azure Web App (free tier), the replication does not work. And it seems that I cannot use the local database. I don't think is a permission problem since my logger is writing to a file in the wwwroot directory.
  2. When I execute the resync method, all other services depending on the database stop working giving a "database destroyed" error. This does not make sense since I am assigning a new Pouch after destroying it.

I have considered replacing the module.exports with something like:

module.exports = {
    data : new PouchDB("pouch-local-database"),
    resync: __resync
};

Thanks guys! I am overlooking something obvious!



via monkeyman

No comments:

Post a Comment