/
db.js
142 lines (125 loc) · 4.24 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"use strict";
var startTime = new Date().getTime(),
utils = require('../utils'),
wrappers = require('pouchdb-wrappers'),
mkdirp = require('mkdirp'),
pathResolve = require('path').resolve,
cleanFilename = require('../clean-filename'),
deleteDB = require('../create-or-delete-dbs').deleteDB;
module.exports = function (app) {
// Create a database.
app.put('/:db', utils.jsonParser, function (req, res, next) {
var name = cleanFilename(req.params.db);
req.PouchDB.allDbs(function (err, dbs) {
if (err) {
return utils.sendError(res, err);
}
if (dbs.indexOf(name) !== -1) {
return utils.sendJSON(res, 412, {
'error': 'file_exists',
'reason': 'The database could not be created.'
});
}
// PouchDB.new() instead of new PouchDB() because that adds
// authorisation logic
var db = req.PouchDB.new(name, utils.makeOpts(req));
db.info(function (err) {
if (err) {
// temoperary workaround for leveldb adapter, see
// https://github.com/pouchdb/pouchdb/issues/5668
// when removing this code, also remove mkdir
// from dependencies in package.json
if (err.name === 'OpenError') {
var path = db.__opts.prefix ? db.__opts.prefix + name : name;
mkdirp(pathResolve(path), function (err) {
if (err) {
return utils.sendError(res, err, 412);
}
db = req.PouchDB.new(name, utils.makeOpts(req));
db.info(function (err, db) {
if (err) {
return utils.sendError(res, err, 412);
}
utils.setLocation(res, name);
utils.sendJSON(res, 201, {ok: true});
});
});
return;
}
return utils.sendError(res, err, 412);
}
utils.setLocation(res, name);
utils.sendJSON(res, 201, {ok: true});
});
});
});
// Delete a database
app.delete('/:db', function (req, res) {
if (req.query.rev) {
return utils.sendJSON(res, 400, {
error: 'bad_request',
reason: (
"You tried to DELETE a database with a ?rev= parameter. " +
"Did you mean to DELETE a document instead?"
)
});
}
var dbName = cleanFilename(req.params.db);
deleteDB(req.PouchDB, dbName, utils.makeOpts(req)).then(function () {
utils.sendJSON(res, 200, {ok: true});
}, function (err) {
utils.sendError(res, err);
});
});
// At this point, some route middleware can take care of identifying the
// correct PouchDB instance.
['/:db/*', '/:db'].forEach(function (route) {
app.all(route, function (req, res, next) {
utils.setDBOnReq(req.params.db, app.dbWrapper, req, res, next);
});
});
// Get database information
app.get('/:db', function (req, res, next) {
req.db.info(utils.makeOpts(req), function (err, info) {
if (err) {
return utils.sendError(res, err);
}
info.instance_start_time = startTime.toString();
// TODO: data_size?
utils.sendJSON(res, 200, info);
});
});
// Ensure all commits are written to disk
app.post('/:db/_ensure_full_commit', function (req, res, next) {
// TODO: implement. Also check security then: who is allowed to
// access this? (db & server admins?)
utils.sendJSON(res, 201, {
ok: true,
instance_start_time: startTime.toString()
});
});
app.dbWrapper.registerWrapper(function (name, db, next) {
// db.info() should just return the non-uri encoded db name
wrappers.installWrapperMethods(db, {
info: function (orig, args) {
return orig().then(function (info) {
info.db_name = decodeURIComponent(info.db_name);
return info;
});
}
});
return next();
});
app.daemonManager.registerDaemon({
start: function (PouchDB) {
// PouchDB.allDbs() should return the non-uri encoded db name
wrappers.installStaticWrapperMethods(PouchDB, {
allDbs: function (orig, args) {
return orig().then(function (dbs) {
return dbs.map(decodeURIComponent);
});
}
});
}
});
};