-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f94a36a
commit c4ae98f
Showing
3 changed files
with
335 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.