blob: 9f0ddd2641686f0861d581a55898f9028199d3b7 [file] [log] [blame]
import PouchDB from './constructor';
import { guardedConsole, hasLocalStorage } from 'pouchdb-utils';
function parseAdapter(name, opts) {
var match = name.match(/([a-z\-]*):\/\/(.*)/);
if (match) {
// the http adapter expects the fully qualified name
name = /http(s?)/.test(match[1]) ? match[1] + '://' + match[2] : match[2];
return {name: name, adapter: match[1]};
}
if (process.env.POUCHDB_LITE) {
adapterName = 'idb';
} else {
// 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];
var adapterName;
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];
/* istanbul ignore if */
if (!process.env.POUCHDB_LITE && skipIdb && adapterName === 'idb') {
// log it, because this can be confusing during development
guardedConsole('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;
}
}
}
var 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
};
}
export default parseAdapter;