-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdesign.js
163 lines (134 loc) · 4.39 KB
/
design.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
var util = require('util'),
Document = require('./document.js').Document;
/**
* design document object
*
* @constructor
* @param {string|null} id string if you have document id, null if not
* @param {cushion.Connection} connection cushion connection object
* @param {cushion.Database} database cushion database object
*/
var Design = function(id, revision, connection, database) {
Document.call(this, id, revision, connection, database);
};
util.inherits(Design, Document);
/**
* compacts the design document
*
* @param {function(error, started)} callback function that will be called,
* after compaction was started or if there was an error
*/
Design.prototype.compact = function(callback) {
this._database.compact(this._id.substr(8), callback);
};
/**
* get, create, update or delete a list function
*
* @param {string} name name of the list function
* @param {?string} content string representation of the list function; if you
* set this to null, the list function will be deleted
* @return {cushion.Design|string} if you save a list function, you will get
* this design document, otherwise the string representation of the specific
* list function
*/
Design.prototype.list = function(name, content) {
if (content) {
this.body('lists', name, ((content === null) ? undefined : content));
}
return ((content !== undefined) ?
this :
(this._body.lists) ? this._body.lists[name] : undefined
);
};
/**
* get or set rewrites
* if you want to get the
*
* @param {?array.<object>} rewrites list ob rewrite objects
* {string} from url source
* {string} to url source
* {?string} method http method
* {?object} query url query params
*/
Design.prototype.rewrites = function(rewrites) {
if (rewrites) {
this.body('rewrites', rewrites);
}
return ((rewrites) ? this : (this.body().rewrites || []));
};
/**
* get, create, update or delete a show function
*
* @param {string} name name of the show function
* @param {?string} content string representation of the show function; if you
* set this to null, the show function will be deleted
* @return {cushion.Design|string} if you save a show function, you will get
* this design document, otherwise the string representation of the specific
* show function
*/
Design.prototype.show = function(name, content) {
if (typeof(content) !== undefined) {
this.body('shows', name, ((content === null) ? undefined : content));
}
return ((content !== undefined) ?
this :
(this._body.shows) ? this._body.shows[name] : undefined
);
};
/**
* sets or get the validate doc update handler
* if you set the handler argument, you will set the handler, otherwise you will
* get the source
*
* @param {?string} handler validate doc update function as string
* @return {cushion.Design|string} if you set the handler, you will get the
* design document, otherwise the string representation of the validation
* handler
*/
Design.prototype.validateHandler = function(handler) {
if (arguments.length > 0) {
this.body('validate_doc_update', handler);
return this;
}
return this.body('validate_doc_update');
};
/**
* get, create, update or delete a view
*
* @param {string} name name of the view
* @param {string} map string representation of the map function; if you set
* this to null, the view will be deleted
* @param {?string} reduce string representation of the reduce function
* @return {cushion.Design|object} if you set a view, you will get this design
* document, otherwise you will get the view object with the string
* representations of the map and reduce functions
*/
Design.prototype.view = function(name, map, reduce) {
var view = {};
if (reduce) {
view.reduce = reduce;
}
if (map !== undefined) {
view.map = map;
this.body('views', name, ((map === null) ? undefined : view));
}
return ((map !== undefined) ?
this :
(this._body.views) ? this._body.views[name] : undefined
);
};
/**
* returns some infos about the design document and the views
*
* @param {function(error, info)} callback function that will be called, after
* getting the infos or if there was an error
*/
Design.prototype.viewInfo = function(callback) {
this._connection.request({
'method': 'GET',
'path': this._database.name() + '/' + this._id + '/_info',
'callback': callback
});
};
exports.Design = Design;