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 Jun 1, 2014
1 parent 763c1f7 commit 551e64d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 56 deletions.
60 changes: 50 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ Here's a sample Express app, which we'll name `app.js`.

```javascript
var express = require('express')
, app = express();
, app = express()
, PouchDB = require('pouchdb');

app.configure(function () {
app.use(express.logger('tiny'));
app.use('/db', require('express-pouchdb'));
});
app.use(express.logger('tiny'));
app.use('/db', require('express-pouchdb')(PouchDB));

app.listen(3000);
```
Expand All @@ -54,11 +53,51 @@ you can use `express.urlencoded()` and `express.multipart()` alongside the **exp
and you should find the results to be the same as you would have expected with `express.bodyParser()`.

```javascript
app.configure(function () {
app.use(express.urlencoded());
app.use(express.multipart());
app.use(require('express-pouchdb'));
});
app.use(express.urlencoded());
app.use(express.multipart());
app.use(require('express-pouchdb'));
```

### Using your own PouchDB

Since you pass in the `PouchDB` that you would like to use with express-pouchb, you can drop
express-pouchdb into an existing Node-based PouchDB application and get all the benefits of the HTTP interface without having to change your code.

```js
var express = require('express')
, app = express()
, PouchDB = require('pouchdb');

app.use('/db', require('express-pouchdb')(PouchDB));

var myPouch = new PouchDB('foo');

// myPouch is now modifiable in your own code, and it's also
// available via HTTP at /db/foo
```

### PouchDB defaults

When you use your own PouchDB code in tandem with express-pouchdb, the `PouchDB.defaults()` API can be very convenient for specifying some default settings for how PouchDB databases are created.

For instance, if you want to use an in-memory [MemDOWN](https://github.com/rvagg/memdown)-backed pouch, you can simply do:

```js
var InMemPouchDB = PouchDB.defaults({db: require('memdown')});

app.use('/db', require('express-pouchdb')(InMemPouchDB));

var myPouch = new InMemPouchDB('foo');
```

Similarly, if you want to place all database files in a folder other than the `pwd`, you can do:

```js
var TempPouchDB = PouchDB.defaults({prefix: '/tmp/my-temp-pouch/'});

app.use('/db', require('express-pouchdb')(TempPouchDB));

var myPouch = new TempPouchDB('foo');
```

## Contributing
Expand All @@ -78,6 +117,7 @@ I haven't defined a formal styleguide, so please take care to maintain the exist
A huge thanks goes out to all of the following people for helping me get this to where it is now.

* Dale Harvey ([@daleharvey](https://github.com/daleharvey))
* Nolan Lawson ([@nolanlawson](https://github.com/nolanlawson))
* Ryan Ramage ([@ryanramage](https://github.com/ryanramage))
* Garren Smith ([@garrensmith](https://github.com/garrensmith))
* ([@copongcopong](https://github.com/copongcopong))
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();
}
41 changes: 23 additions & 18 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,22 @@ 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') {
allDbs.allDbs(function (err, dbs) {
if (err) {
return res.send(500, err);
} else if (dbs.indexOf(name) === -1) {
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 +523,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 551e64d

Please sign in to comment.