Skip to content

Commit

Permalink
(#1108) - split out index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Dec 6, 2013
1 parent 95527af commit f60eaa9
Show file tree
Hide file tree
Showing 3 changed files with 335 additions and 330 deletions.
78 changes: 78 additions & 0 deletions lib/constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"use strict";

var Adapter = require('./adapter')(PouchDB);
function PouchDB(name, opts, callback) {

if (!(this instanceof PouchDB)) {
return new PouchDB(name, opts, callback);
}

if (typeof opts === 'function' || typeof opts === 'undefined') {
callback = opts;
opts = {};
}

if (typeof name === 'object') {
opts = name;
name = undefined;
}

if (typeof callback === 'undefined') {
callback = function () {};
}

var backend = PouchDB.parseAdapter(opts.name || name);
opts.originalName = name;
opts.name = opts.name || backend.name;
opts.adapter = opts.adapter || backend.adapter;

if (!PouchDB.adapters[opts.adapter]) {
throw 'Adapter is missing';
}

if (!PouchDB.adapters[opts.adapter].valid()) {
throw 'Invalid Adapter';
}

var adapter = new Adapter(opts, function (err, db) {
if (err) {
if (callback) {
callback(err);
}
return;
}

for (var plugin in PouchDB.plugins) {
// In future these will likely need to be async to allow the plugin
// to initialise
var pluginObj = PouchDB.plugins[plugin](db);
for (var api in pluginObj) {
// We let things like the http adapter use its own implementation
// as it shares a lot of code
if (!(api in db)) {
db[api] = pluginObj[api];
}
}
}
db.taskqueue.ready(true);
db.taskqueue.execute(db);
callback(null, db);
});
for (var j in adapter) {
this[j] = adapter[j];
}
for (var plugin in PouchDB.plugins) {
// In future these will likely need to be async to allow the plugin
// to initialise
var pluginObj = PouchDB.plugins[plugin](this);
for (var api in pluginObj) {
// We let things like the http adapter use its own implementation
// as it shares a lot of code
if (!(api in this)) {
this[api] = pluginObj[api];
}
}
}
}

module.exports = PouchDB;

0 comments on commit f60eaa9

Please sign in to comment.