Skip to content

Commit

Permalink
(pouchdb#628) - Added Pouch.enableAllDbs flag to make allDbs optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ymichael committed Mar 29, 2013
1 parent 73c4c18 commit b7d5f3c
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions src/pouch.js
Expand Up @@ -125,36 +125,49 @@ Pouch.destroy = function(name, callback) {
// call destroy method of the particular adaptor
Pouch.adapters[opts.adapter].destroy(opts.name, callback);
};

// remove Pouch from allDBs
Pouch.removeFromAllDbs(opts, cb);
};

Pouch.removeFromAllDbs = function(opts, callback) {
// Only execute function if flag is enabled
if (!Pouch.enableAllDbs) {
callback();
return;
}

// skip http and https adaptors for allDbs
var adapter = opts.adapter;
if (adapter === "http" || adapter === "https") {
cb();
callback();
return;
}

// remove db from Pouch.ALL_DBS
new Pouch(Pouch.allDBName(opts.adapter), function(err, db) {
if (err) {
callback(err);
// don't fail when allDbs fail
console.log(err);
callback();
return;
}
// check if db has been registered in Pouch.ALL_DBS
var dbname = Pouch.dbName(opts.adapter, opts.name);
db.get(dbname, function(err, doc) {
if (err) {
if (err.status === 404) {
cb();
} else {
cb(err);
}
callback();
} else {
db.remove(doc, function(err, response) {
cb(err);
if (err) {
console.log(err);
}
callback();
});
}
});
});

};

Pouch.adapter = function (id, obj) {
Expand All @@ -167,6 +180,9 @@ Pouch.plugin = function(id, obj) {
Pouch.plugins[id] = obj;
};

// flag to toggle allDbs (off by default)
Pouch.enableAllDbs = false;

// name of database used to keep track of databases
Pouch.ALL_DBS = "_allDbs";
Pouch.dbName = function(adapter, name) {
Expand All @@ -180,6 +196,12 @@ Pouch.allDBName = function(adapter) {
};

Pouch.open = function(opts, callback) {
// Only register pouch with allDbs if flag is enabled
if (!Pouch.enableAllDbs) {
callback();
return;
}

var adapter = opts.adapter;
// skip http and https adaptors for allDbs
if (adapter === "http" || adapter === "https") {
Expand All @@ -189,22 +211,26 @@ Pouch.open = function(opts, callback) {

new Pouch(Pouch.allDBName(adapter), function(err, db) {
if (err) {
callback(err);
// don't fail when allDb registration fails
console.log(err);
callback();
return;
}

// check if db has been registered in Pouch.ALL_DBS
var dbname = Pouch.dbName(adapter, opts.name);
db.get(dbname, function(err, response) {
if (err) {
if (err.status === 404) {
db.put({
_id: dbname,
dbname: opts.originalName
}, callback);
} else {
callback(err);
}
if (err && err.status === 404) {
db.put({
_id: dbname,
dbname: opts.originalName
}, function(err) {
if (err) {
console.log(err);
}

callback();
});
} else {
callback();
}
Expand Down

0 comments on commit b7d5f3c

Please sign in to comment.