-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevdb.js
executable file
·126 lines (118 loc) · 3.46 KB
/
devdb.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
#!/usr/bin/env node
// (c) Mathieu Bruyen - http://mais-h.eu/
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
// ## Database management script
var path = require('path');
var exec = require('child_process').exec;
var fs = require('fs');
var when = require('when');
var dbId = 1;
// ### Preparation
//
// Database is stored in `db` directory.
var dbdir = path.join(__dirname, 'db');
// Use local `couchdb.ini` configuration file to override globals, store running PID in local directory and redirect
// out and error streams to files in local directory.
var couchcommand = 'couchdb' +
' -a ' + path.join(__dirname, 'couchdb.ini') +
' -p ' + path.join(dbdir, 'couchdb.pid') +
' -o ' + path.join(dbdir, 'couchdb.stdout') +
' -e ' + path.join(dbdir, 'couchdb.stderr');
// Helper function to remove a directory with all files and directories (recursively) inside it.
function recursiveUnlinkSync(directory) {
fs.readdirSync(directory).forEach(function (filename) {
var file = path.join(directory, filename);
if (fs.statSync(file).isDirectory()) {
recursiveUnlinkSync(file);
} else {
fs.unlinkSync(file);
}
});
fs.rmdirSync(directory);
}
// Helper function to check whether a simple object is contained in an array.
var contains = function (arr, item) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === item) {
return true;
}
}
return false;
};
// ### Actual scripts
module.exports = {
// #### Start
//
// Starts the development database.
start: function () {
var deferred = when.defer();
var child = exec(couchcommand + ' -b', { cwd: __dirname });
child.on('exit', function (code) {
if (code === 0) {
setTimeout(deferred.resolve, 1000);
} else {
deferred.reject('Error');
}
});
return deferred.promise;
},
// #### Stop
//
// Stops the development database.
stop: function () {
var deferred = when.defer();
exec(couchcommand + ' -k', { cwd: __dirname }, deferred.resolve);
return deferred.promise;
},
// #### Devdb
//
// Cleans the local directory and spawns a new empty database.
devdb: function () {
return when(module.exports.stop()).then(function () {
if (fs.existsSync(dbdir)) {
recursiveUnlinkSync(dbdir);
}
fs.mkdirSync(dbdir);
return module.exports.start();
});
},
// #### Document pre loading
//
// Loads some documents in a freshly created database.
load: function (documents, nameds) {
var nano = require('nano')('http://localhost:5984');
var dbname = 'test' + (dbId++);
var deferred = when.defer();
nano.db.create(dbname, function () {
deferred.resolve(nano.use(dbname));
});
return deferred.promise.then(function (db) {
var promises = [];
documents.forEach(function (doc) {
var deferred = when.defer();
db.insert(doc, function (err, body) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve();
}
});
promises.push(deferred.promise);
});
nameds.forEach(function (named) {
var deferred = when.defer();
db.insert(named.doc, named.key, function (err, body) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve();
}
});
promises.push(deferred.promise);
});
return when.all(promises);
}).then(function () {
return dbname;
});
}
};