diff --git a/README.md b/README.md index 7692a98..9c41457 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,25 @@ app.configure(function () { }); ``` +You can also tell express-pouchdb to write its database files to a directory other than the `pwd`. Use e.g.: + +```js +var expressPouchDB = require('express-pouchdb'); +expressPouchDB.setPath('/path/to/my/db/files'); +app.use(expressPouchDB); +``` + +express-pouchdb will create this directory if it doesn't already exist. + +You can also pass in an alternative LevelDOWN adapter (e.g. memdown) into +express-pouchdb: + +```js +var expressPouchDB = require('express-pouchdb'); +expressPouchDB.setBackend(require('memdown')); +app.use(expressPouchDB); +``` + ## Contributing Want to help me make this thing awesome? Great! Here's how you should get started. diff --git a/all-dbs.js b/all-dbs.js index 7c15d05..dc24717 100644 --- a/all-dbs.js +++ b/all-dbs.js @@ -1,38 +1,58 @@ -var Pouch = require('pouchdb'); +var path = require('path'); -var pouch = new Pouch('pouch__all_dbs__'); +var Pouch; +var pouch; -Pouch.on('created', function (dbName) { - if (dbName === 'pouch__all_dbs__') { - return; +function normalize(name) { + return path.basename(name); +} + +function setup() { + if (!pouch) { + pouch = new Pouch('pouch__all_dbs__'); } - pouch.get('db_' + dbName).then(function (doc) { - // db exists, nothing to do - }).catch(function (err) { - if (err.name !== 'not_found') { - console.error(err); +} + +function init() { + Pouch.on('created', function (dbName) { + setup(); + dbName = normalize(dbName); + + if (dbName === 'pouch__all_dbs__') { return; } - pouch.put({_id: 'db_' + dbName}).catch(function (err) { - console.error(err); + pouch.get('db_' + dbName).then(function (doc) { + // db exists, nothing to do + }).catch(function (err) { + if (err.name !== 'not_found') { + console.error(err); + return; + } + pouch.put({_id: 'db_' + dbName}).catch(function (err) { + console.error(err); + }); }); }); -}); -Pouch.on('destroyed', function (dbName) { - pouch.get('db_' + dbName).then(function (doc) { - pouch.remove(doc).catch(function (err) { - console.error(err); + Pouch.on('destroyed', function (dbName) { + setup(); + dbName = normalize(dbName); + + pouch.get('db_' + dbName).then(function (doc) { + pouch.remove(doc).catch(function (err) { + console.error(err); + }); + }).catch(function (err) { + // normally a not_found error; nothing to do + if (err.name !== 'not_found') { + console.error(err); + } }); - }).catch(function (err) { - // normally a not_found error; nothing to do - if (err.name !== 'not_found') { - console.error(err); - } }); -}); +} -module.exports = function(callback) { +exports.allDbs = function(callback) { + setup(); pouch.allDocs().then(function (res) { var dbs = res.rows.map(function (row) { return row.key.replace(/^db_/, ''); @@ -43,4 +63,9 @@ module.exports = function(callback) { }).catch(function (err) { callback(err); }) -}; \ No newline at end of file +}; + +exports.setPouch = function (PouchToUse) { + Pouch = PouchToUse; + init(); +} \ No newline at end of file diff --git a/index.js b/index.js index 6e6a632..47194b3 100644 --- a/index.js +++ b/index.js @@ -2,20 +2,26 @@ var express = require('express') , rawBody = require('raw-body') , fs = require('fs') + , path = require('path') , extend = require('extend') , pkg = require('./package.json') , dbs = {} , uuids = require('./uuids') , allDbs = require('./all-dbs') , histories = {} - , app = module.exports = express() - , Pouch = module.exports.Pouch = require('pouchdb'); + , app = express(); + +var Pouch; +module.exports = function(PouchToUse) { + Pouch = PouchToUse; + allDbs.setPouch(PouchToUse); + return app; +}; function isPouchError(obj) { return obj.error && obj.error === true; } - app.use('/js', express.static(__dirname + '/fauxton/js')); app.use('/css', express.static(__dirname + '/fauxton/css')); app.use('/img', express.static(__dirname + '/fauxton/img')); @@ -95,7 +101,7 @@ app.get('/_uuids', function (req, res, next) { // List all databases. app.get('/_all_dbs', function (req, res, next) { - allDbs(function (err, response) { + allDbs.allDbs(function (err, response) { if (err) res.send(500, Pouch.UNKNOWN_ERROR); res.send(200, response); }); @@ -160,6 +166,7 @@ app.get('/_active_tasks', function (req, res, next) { // Create a database. app.put('/:db', function (req, res, next) { var name = encodeURIComponent(req.params.db); + if (name in dbs) { return res.send(412, { 'error': 'file_exists', @@ -167,7 +174,7 @@ app.put('/:db', function (req, res, next) { }); } - Pouch(name, function (err, db) { + new Pouch(name, function (err, db) { if (err) return res.send(412, err); dbs[name] = db; var loc = req.protocol @@ -201,24 +208,11 @@ app.delete('/:db', function (req, res, next) { return next(); } - // Check for the data stores, and rebuild a Pouch instance if able - fs.stat(name, function (err, stats) { - if (err && err.code == 'ENOENT') { - return res.send(404, { - status: 404, - error: 'not_found', - reason: 'no_db_file' - }); - } - - if (stats.isDirectory()) { - Pouch(name, function (err, db) { - if (err) return res.send(412, err); - dbs[name] = db; - req.db = db; - return next(); - }); - } + new Pouch(name, function (err, db) { + if (err) return res.send(412, err); + dbs[name] = db; + req.db = db; + return next(); }); }); }); @@ -518,4 +512,4 @@ app.copy('/:db/:id', function (req, res, next) { res.send(200, doc); }); }); -}); +}); \ No newline at end of file diff --git a/package.json b/package.json index 6031d6a..79de1c4 100644 --- a/package.json +++ b/package.json @@ -13,12 +13,12 @@ }, "main": "./index.js", "dependencies": { - "pouchdb": "pouchdb/pouchdb", - "raw-body": "^1.1.5", - "extend": "^1.2.1" + "extend": "^1.2.1", + "raw-body": "^1.1.5" }, "devDependencies": { "express": "4.x", + "pouchdb": "pouchdb/pouchdb", "morgan": "^1.0.1" }, "peerDependencies": {