-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (44 loc) · 1.4 KB
/
index.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
var config = require('./config');
function install(callback){
var async = require('async');
var _ = require('lodash');
var db_module = require('./models/db');
var designs = require('./models/db/designs');
var nano = db_module.nano();
var db = db_module.db();
var db_name = config.get('couchdb').database;
async.series([
// Create the database if it doesn't exist:
function(async_callback){
nano.db.get(db_name, function(get_error, result){
if (!get_error) { return async_callback(null, result) }
if (get_error.status_code === 404){
return nano.db.create(db_name, function(
creation_error,
creation_result
){
if (creation_error){
if (creation_error.error == 'file_exists'){
return async_callback(null, null);
}
return async_callback(creation_error, null);
}
return async_callback(null, creation_result)
});
}
return async_callback(get_error, null);
});
},
// Insert design documents
function(async_callback){
var design_docs = [];
for (design_name in designs) design_docs.push(designs[design_name]);
db.bulk({ docs: design_docs }, async_callback);
}
], callback);
}
module.exports = {
models: require('./models'),
config: config,
install: install
}