Skip to content

Commit

Permalink
(pouchdb/pouchdb-server#38) - in-memory/filepath option
Browse files Browse the repository at this point in the history
Also related to pouchdb/pouchdb-server#36.
  • Loading branch information
nolanlawson committed May 30, 2014
1 parent 763c1f7 commit 7eec35f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 52 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
75 changes: 50 additions & 25 deletions all-dbs.js
Original file line number Diff line number Diff line change
@@ -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_/, '');
Expand All @@ -43,4 +63,9 @@ module.exports = function(callback) {
}).catch(function (err) {
callback(err);
})
};
};

exports.setPouch = function (PouchToUse) {
Pouch = PouchToUse;
init();
}
42 changes: 18 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -160,14 +166,15 @@ 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',
'reason': 'The database could not be created.'
});
}

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
Expand Down Expand Up @@ -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();
});
});
});
Expand Down Expand Up @@ -518,4 +512,4 @@ app.copy('/:db/:id', function (req, res, next) {
res.send(200, doc);
});
});
});
});
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 7eec35f

Please sign in to comment.