UNPKG

14.1 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ViewIndexManager = exports.DesignDocument = exports.DesignDocumentView = void 0;
4const utilities_1 = require("./utilities");
5const viewtypes_1 = require("./viewtypes");
6const bindingutilities_1 = require("./bindingutilities");
7/**
8 * Contains information about a view in a design document.
9 *
10 * @category Management
11 */
12class DesignDocumentView {
13 /**
14 * @internal
15 */
16 constructor(...args) {
17 let data;
18 if (typeof args[0] === 'string' || typeof args[0] === 'function') {
19 data = {
20 map: args[0],
21 reduce: args[1],
22 };
23 }
24 else {
25 data = args[0];
26 }
27 this.map = data.map;
28 this.reduce = data.reduce;
29 }
30 /**
31 * @internal
32 */
33 static _toCppData(name, data) {
34 return {
35 name: name,
36 map: data.map,
37 reduce: data.reduce,
38 };
39 }
40 /**
41 * @internal
42 */
43 static _fromCppData(data) {
44 return new DesignDocumentView({
45 map: data.map,
46 reduce: data.reduce,
47 });
48 }
49}
50exports.DesignDocumentView = DesignDocumentView;
51/**
52 * Contains information about a design document.
53 *
54 * @category Management
55 */
56class DesignDocument {
57 /**
58 * Same as {@link DesignDocumentView}.
59 *
60 * @deprecated Use {@link DesignDocumentView} directly.
61 */
62 static get View() {
63 return DesignDocumentView;
64 }
65 /**
66 * @internal
67 */
68 constructor(...args) {
69 let data;
70 if (typeof args[0] === 'string') {
71 data = {
72 name: args[0],
73 views: args[1],
74 };
75 }
76 else {
77 data = args[0];
78 }
79 this.name = data.name;
80 this.views = data.views || {};
81 this.namespace = data.namespace || viewtypes_1.DesignDocumentNamespace.Production;
82 this.rev = data.rev;
83 }
84 /**
85 * @internal
86 */
87 static _fromNsData(ddocName, ddocData) {
88 const views = {};
89 for (const viewName in ddocData.views) {
90 const viewData = ddocData.views[viewName];
91 views[viewName] = new DesignDocumentView({
92 map: viewData.map,
93 reduce: viewData.reduce,
94 });
95 }
96 return new DesignDocument({ name: ddocName, views: views });
97 }
98 /**
99 * @internal
100 */
101 static _toCppData(data, namespace) {
102 const cppView = {};
103 for (const [k, v] of Object.entries(data.views)) {
104 cppView[k] = DesignDocumentView._toCppData(k, v);
105 }
106 return {
107 rev: data.rev,
108 name: data.name,
109 ns: (0, bindingutilities_1.designDocumentNamespaceToCpp)(namespace),
110 views: cppView,
111 };
112 }
113 /**
114 * @internal
115 */
116 static _fromCppData(ddoc) {
117 const views = {};
118 for (const [viewName, viewData] of Object.entries(ddoc.views)) {
119 views[viewName] = DesignDocumentView._fromCppData(viewData);
120 }
121 return new DesignDocument({
122 name: ddoc.name,
123 views: views,
124 namespace: (0, bindingutilities_1.designDocumentNamespaceFromCpp)(ddoc.ns),
125 rev: ddoc.rev,
126 });
127 }
128}
129exports.DesignDocument = DesignDocument;
130/**
131 * ViewIndexManager is an interface which enables the management
132 * of view indexes on the cluster.
133 *
134 * @category Management
135 */
136class ViewIndexManager {
137 /**
138 * @internal
139 */
140 constructor(bucket) {
141 this._bucket = bucket;
142 }
143 /**
144 * @internal
145 */
146 get _cluster() {
147 return this._bucket.cluster;
148 }
149 /**
150 * @internal
151 */
152 async getAllDesignDocuments() {
153 let namespace;
154 let options;
155 let callback;
156 // deprecated path: options and maybe callback passed in
157 if (typeof arguments[0] === 'object') {
158 namespace = undefined;
159 options = arguments[0];
160 callback = arguments[1];
161 }
162 else if (arguments[0] instanceof Function) {
163 // deprecated path: no options, only callback passed in
164 namespace = undefined;
165 options = undefined;
166 callback = arguments[0];
167 }
168 else {
169 // either no args passed in or desired path (namespace is 1st arg)
170 namespace = arguments[0];
171 // still need to handle possible no options, but callback passed in
172 if (arguments[1] instanceof Function) {
173 callback = arguments[1];
174 options = undefined;
175 }
176 else {
177 options = arguments[1];
178 callback = arguments[2];
179 }
180 }
181 if (!options) {
182 options = {};
183 }
184 const timeout = options.timeout || this._cluster.managementTimeout;
185 const ns = namespace !== null && namespace !== void 0 ? namespace : viewtypes_1.DesignDocumentNamespace.Production;
186 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
187 this._cluster.conn.managementViewIndexGetAll({
188 bucket_name: this._bucket.name,
189 ns: (0, bindingutilities_1.designDocumentNamespaceToCpp)(ns),
190 timeout: timeout,
191 }, (cppErr, resp) => {
192 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
193 if (err) {
194 return wrapCallback(err, null);
195 }
196 const ddocs = [];
197 for (const ddoc of resp.design_documents) {
198 ddocs.push(DesignDocument._fromCppData(ddoc));
199 }
200 wrapCallback(null, ddocs);
201 });
202 }, callback);
203 }
204 /**
205 * @internal
206 */
207 async getDesignDocument() {
208 let designDocName = arguments[0];
209 let namespace;
210 let options;
211 let callback;
212 // deprecated path: options and maybe callback passed in
213 if (typeof arguments[1] === 'object') {
214 namespace = undefined;
215 options = arguments[1];
216 callback = arguments[2];
217 }
218 else if (arguments[1] instanceof Function) {
219 // deprecated path: no options, only callback passed in
220 namespace = undefined;
221 options = undefined;
222 callback = arguments[1];
223 }
224 else {
225 // either no other args passed in or desired path (namespace is 2nd arg)
226 namespace = arguments[1];
227 // still need to handle possible no options, but callback passed in
228 if (arguments[2] instanceof Function) {
229 callback = arguments[2];
230 options = undefined;
231 }
232 else {
233 options = arguments[2];
234 callback = arguments[3];
235 }
236 }
237 if (!options) {
238 options = {};
239 }
240 const timeout = options.timeout || this._cluster.managementTimeout;
241 // for compatibility with older SDK versions (i.e. deprecated getDesignDocument())
242 if (designDocName.startsWith('dev_')) {
243 namespace = viewtypes_1.DesignDocumentNamespace.Development;
244 designDocName = designDocName.substring(4);
245 }
246 const ns = namespace !== null && namespace !== void 0 ? namespace : viewtypes_1.DesignDocumentNamespace.Production;
247 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
248 this._cluster.conn.managementViewIndexGet({
249 bucket_name: this._bucket.name,
250 document_name: designDocName,
251 ns: (0, bindingutilities_1.designDocumentNamespaceToCpp)(ns),
252 timeout: timeout,
253 }, (cppErr, resp) => {
254 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
255 if (err) {
256 return wrapCallback(err, null);
257 }
258 const ddoc = DesignDocument._fromCppData(resp.document);
259 wrapCallback(null, ddoc);
260 });
261 }, callback);
262 }
263 /**
264 * @internal
265 */
266 async upsertDesignDocument() {
267 const designDoc = arguments[0];
268 let namespace;
269 let options;
270 let callback;
271 // deprecated path: options and maybe callback passed in
272 if (typeof arguments[1] === 'object') {
273 namespace = undefined;
274 options = arguments[1];
275 callback = arguments[2];
276 }
277 else if (arguments[1] instanceof Function) {
278 // deprecated path: no options, only callback passed in
279 namespace = undefined;
280 options = undefined;
281 callback = arguments[1];
282 }
283 else {
284 // either no other args passed in or desired path (namespace is 2nd arg)
285 namespace = arguments[1];
286 // still need to handle possible no options, but callback passed in
287 if (arguments[2] instanceof Function) {
288 callback = arguments[2];
289 options = undefined;
290 }
291 else {
292 options = arguments[2];
293 callback = arguments[3];
294 }
295 }
296 if (!options) {
297 options = {};
298 }
299 const timeout = options.timeout || this._cluster.managementTimeout;
300 // for compatibility with older SDK versions (i.e. deprecated upsertDesignDocument())
301 if (designDoc.name.startsWith('dev_')) {
302 namespace = viewtypes_1.DesignDocumentNamespace.Development;
303 designDoc.name = designDoc.name.substring(4);
304 }
305 const ns = namespace !== null && namespace !== void 0 ? namespace : viewtypes_1.DesignDocumentNamespace.Production;
306 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
307 this._cluster.conn.managementViewIndexUpsert({
308 bucket_name: this._bucket.name,
309 document: DesignDocument._toCppData(designDoc, ns),
310 timeout: timeout,
311 }, (cppErr) => {
312 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
313 if (err) {
314 return wrapCallback(err, null);
315 }
316 wrapCallback(err);
317 });
318 }, callback);
319 }
320 /**
321 * @internal
322 */
323 async dropDesignDocument() {
324 let designDocName = arguments[0];
325 let namespace;
326 let options;
327 let callback;
328 // deprecated path: options and maybe callback passed in
329 if (typeof arguments[1] === 'object') {
330 namespace = undefined;
331 options = arguments[1];
332 callback = arguments[2];
333 }
334 else if (arguments[1] instanceof Function) {
335 // deprecated path: no options, only callback passed in
336 namespace = undefined;
337 options = undefined;
338 callback = arguments[1];
339 }
340 else {
341 // either no other args passed in or desired path (namespace is 2nd arg)
342 namespace = arguments[1];
343 // still need to handle possible no options, but callback passed in
344 if (arguments[2] instanceof Function) {
345 callback = arguments[2];
346 options = undefined;
347 }
348 else {
349 options = arguments[2];
350 callback = arguments[3];
351 }
352 }
353 if (!options) {
354 options = {};
355 }
356 const timeout = options.timeout || this._cluster.managementTimeout;
357 // for compatibility with older SDK versions (i.e. deprecated dropDesignDocument())
358 if (designDocName.startsWith('dev_')) {
359 namespace = viewtypes_1.DesignDocumentNamespace.Development;
360 designDocName = designDocName.substring(4);
361 }
362 const ns = namespace !== null && namespace !== void 0 ? namespace : viewtypes_1.DesignDocumentNamespace.Production;
363 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
364 this._cluster.conn.managementViewIndexDrop({
365 bucket_name: this._bucket.name,
366 document_name: designDocName,
367 ns: (0, bindingutilities_1.designDocumentNamespaceToCpp)(ns),
368 timeout: timeout,
369 }, (cppErr) => {
370 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
371 if (err) {
372 return wrapCallback(err, null);
373 }
374 wrapCallback(err);
375 });
376 }, callback);
377 }
378 /**
379 * Publishes a development design document to be a production design document.
380 * It does this by fetching the design document by the passed name with `dev_`
381 * appended, and then performs an upsert of the production name (no `dev_`)
382 * with the data which was just fetched.
383 *
384 * @param designDocName The name of the design document to publish.
385 * @param options Optional parameters for this operation.
386 * @param callback A node-style callback to be invoked after execution.
387 */
388 async publishDesignDocument(designDocName, options, callback) {
389 if (options instanceof Function) {
390 callback = arguments[1];
391 options = undefined;
392 }
393 if (!options) {
394 options = {};
395 }
396 const timeout = options.timeout || this._cluster.managementTimeout;
397 const timer = new utilities_1.CompoundTimeout(timeout);
398 return utilities_1.PromiseHelper.wrapAsync(async () => {
399 const designDoc = await this.getDesignDocument(designDocName, viewtypes_1.DesignDocumentNamespace.Development, {
400 timeout: timer.left(),
401 });
402 await this.upsertDesignDocument(designDoc, viewtypes_1.DesignDocumentNamespace.Production, {
403 timeout: timer.left(),
404 });
405 }, callback);
406 }
407}
408exports.ViewIndexManager = ViewIndexManager;