blob: 08aa4254eabc0920f3169c6b11845c4594b08bac [file] [log] [blame]
import jsExtend from 'js-extend'; var extend = jsExtend.extend;
import PouchDB from "./constructor";
import inherits from 'inherits';
import { Map } from 'pouchdb-collections';
import { EventEmitter as EE } from 'events';
import { hasLocalStorage } from 'pouchdb-utils';
PouchDB.adapters = {};
PouchDB.preferredAdapters = [];
PouchDB.prefix = '_pouch_';
var eventEmitter = new EE();
function setUpEventEmitter(Pouch) {
Object.keys(EE.prototype).forEach(function (key) {
if (typeof EE.prototype[key] === 'function') {
Pouch[key] = eventEmitter[key].bind(eventEmitter);
}
});
// these are created in constructor.js, and allow us to notify each DB with
// the same name that it was destroyed, via the constructor object
var destructListeners = Pouch._destructionListeners = new Map();
Pouch.on('destroyed', function onConstructorDestroyed(name) {
if (!destructListeners.has(name)) {
return;
}
destructListeners.get(name).forEach(function (callback) {
callback();
});
destructListeners.delete(name);
});
}
setUpEventEmitter(PouchDB);
PouchDB.parseAdapter = function (name, opts) {
var match = name.match(/([a-z\-]*):\/\/(.*)/);
var adapter, adapterName;
if (match) {
// the http adapter expects the fully qualified name
name = /http(s?)/.test(match[1]) ? match[1] + '://' + match[2] : match[2];
adapter = match[1];
/* istanbul ignore if */
if (!PouchDB.adapters[adapter].valid()) {
throw 'Invalid adapter';
}
return {name: name, adapter: match[1]};
}
// check for browsers that have been upgraded from websql-only to websql+idb
var skipIdb = 'idb' in PouchDB.adapters && 'websql' in PouchDB.adapters &&
hasLocalStorage() &&
localStorage['_pouch__websqldb_' + PouchDB.prefix + name];
if (opts.adapter) {
adapterName = opts.adapter;
} else if (typeof opts !== 'undefined' && opts.db) {
adapterName = 'leveldb';
} else { // automatically determine adapter
for (var i = 0; i < PouchDB.preferredAdapters.length; ++i) {
adapterName = PouchDB.preferredAdapters[i];
if (adapterName in PouchDB.adapters) {
/* istanbul ignore if */
if (skipIdb && adapterName === 'idb') {
// log it, because this can be confusing during development
console.log('PouchDB is downgrading "' + name + '" to WebSQL to' +
' avoid data loss, because it was already opened with WebSQL.');
continue; // keep using websql to avoid user data loss
}
break;
}
}
}
adapter = PouchDB.adapters[adapterName];
// if adapter is invalid, then an error will be thrown later
var usePrefix = (adapter && 'use_prefix' in adapter) ?
adapter.use_prefix : true;
return {
name: usePrefix ? (PouchDB.prefix + name) : name,
adapter: adapterName
};
};
PouchDB.adapter = function (id, obj, addToPreferredAdapters) {
if (obj.valid()) {
PouchDB.adapters[id] = obj;
if (addToPreferredAdapters) {
PouchDB.preferredAdapters.push(id);
}
}
};
PouchDB.plugin = function (obj) {
if (typeof obj === 'function') { // function style for plugins
obj(PouchDB);
} else {
Object.keys(obj).forEach(function (id) { // object style for plugins
PouchDB.prototype[id] = obj[id];
});
}
return PouchDB;
};
PouchDB.defaults = function (defaultOpts) {
function PouchAlt(name, opts, callback) {
if (!(this instanceof PouchAlt)) {
return new PouchAlt(name, opts, callback);
}
if (typeof opts === 'function' || typeof opts === 'undefined') {
callback = opts;
opts = {};
}
if (name && typeof name === 'object') {
opts = name;
name = undefined;
}
opts = extend({}, defaultOpts, opts);
PouchDB.call(this, name, opts, callback);
}
inherits(PouchAlt, PouchDB);
setUpEventEmitter(PouchAlt);
PouchAlt.preferredAdapters = PouchDB.preferredAdapters.slice();
Object.keys(PouchDB).forEach(function (key) {
if (!(key in PouchAlt)) {
PouchAlt[key] = PouchDB[key];
}
});
return PouchAlt;
};
export default PouchDB;