-
Notifications
You must be signed in to change notification settings - Fork 1
/
beer_designs.js
121 lines (104 loc) · 4.28 KB
/
beer_designs.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
var couchbase = require('couchbase');
// Setup design documents.
// The following functions acts synchronously so that we can know when to safely
// exit. This is a workaround for a current bug in the client that stops node
// from gracefully exiting while a Couchbase connection is active.
exports.setup = function( config ) {
var beer_by_name = {
map : [ 'function(doc, meta) {',
'if (doc.type && doc.type == "beer") { ',
'emit(doc.name, null); }',
'}'
].join('\n')
}
var breweries_by_name = {
map : [ 'function(doc, meta) {',
'if (doc.type && doc.type == "brewery") { ',
'emit(doc.name, null); }',
'}'
].join('\n')
}
var bscluster = new couchbase.Cluster(config.connstr);
var bsbucket = bscluster.openBucket(config.bucket, function( err ) {
if(err) {
console.log("Unable to connect to server");
console.log(config);
process.exit(1);
}
var bmanager = bsbucket.manager();
// Update the beer view, to index beers `by_name`.
bmanager.getDesignDocument( "beer", function( err, ddoc, meta ) {
if(! ('by_name' in ddoc['views']) ) {
ddoc.views.by_name = beer_by_name;
bmanager.upsertDesignDocument( "beer", ddoc, function( err, res ) {
if(err) {
console.log( 'ERROR' + err );
} else if (res.ok) {
console.log( 'Updated ' + res.id );
}
// Update or create the brewery view, to index brewery or `by_name`.
bmanager.getDesignDocument( "brewery", function( err, ddoc, meta ) {
if (err) {
console.log( "Creating the brewery view" );
breweries_design = { views : {by_name : breweries_by_name} };
bmanager.upsertDesignDocument( "brewery", breweries_design, function(err) {
if(err) {
console.log( 'ERROR' + err );
} else if (res.ok) {
console.log( 'Updated ' + res.id );
}
process.exit(0);
});
} else {
if(! ('by_name' in ddoc['views']) ) {
console.log("Updating the brewery view");
ddoc['views']['by_name'] = breweries_by_name;
bmanager.upsertDesignDocument( "brewery", ddoc, function( err, res ) {
if(err) {
console.log( 'ERROR' + err );
} else if (res.ok) {
console.log( 'Updated ' + res.id );
}
process.exit(0);
});
}
}
});
});
} else {
console.log('already setup');
process.exit(0);
}
})
})
}
exports.reset = function( config ) {
var bscluster = new couchbase.Cluster(config.connstr);
var bsbucket = bscluster.openBucket(config.bucket, function( err ) {
if(err) {
console.error("Failed to connect to cluster: " + err);
process.exit(1);
}
var bmanager = bsbucket.manager();
// Update the beer view, to index beers `by_name`.
bmanager.getDesignDocument( "beer", function( err, ddoc, meta ) {
console.log(err);
console.log('get done');
delete ddoc['views']['by_name'];
bmanager.upsertDesignDocument( "beer", ddoc, function( err, res ) {
console.log('set done');
if(err) {
console.log( 'ERROR' + err );
} else if (res.ok) {
console.log( 'Updated ' + res.id );
}
// Update or create the brewery view, to index brewery or `by_name`.
bmanager.deleteDesignDocument( "brewery", function(err, res) {
console.log('delete done');
console.log(err);
process.exit(0);
});
});
})
})
}