1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ViewIndexManager = exports.DesignDocument = exports.DesignDocumentView = void 0;
|
4 | const utilities_1 = require("./utilities");
|
5 | const viewtypes_1 = require("./viewtypes");
|
6 | const bindingutilities_1 = require("./bindingutilities");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | class DesignDocumentView {
|
13 | |
14 |
|
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 |
|
32 |
|
33 | static _toCppData(name, data) {
|
34 | return {
|
35 | name: name,
|
36 | map: data.map,
|
37 | reduce: data.reduce,
|
38 | };
|
39 | }
|
40 | |
41 |
|
42 |
|
43 | static _fromCppData(data) {
|
44 | return new DesignDocumentView({
|
45 | map: data.map,
|
46 | reduce: data.reduce,
|
47 | });
|
48 | }
|
49 | }
|
50 | exports.DesignDocumentView = DesignDocumentView;
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | class DesignDocument {
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 | static get View() {
|
63 | return DesignDocumentView;
|
64 | }
|
65 | |
66 |
|
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 |
|
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 |
|
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 |
|
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 | }
|
129 | exports.DesignDocument = DesignDocument;
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | class ViewIndexManager {
|
137 | |
138 |
|
139 |
|
140 | constructor(bucket) {
|
141 | this._bucket = bucket;
|
142 | }
|
143 | |
144 |
|
145 |
|
146 | get _cluster() {
|
147 | return this._bucket.cluster;
|
148 | }
|
149 | |
150 |
|
151 |
|
152 | async getAllDesignDocuments() {
|
153 | let namespace;
|
154 | let options;
|
155 | let callback;
|
156 |
|
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 |
|
164 | namespace = undefined;
|
165 | options = undefined;
|
166 | callback = arguments[0];
|
167 | }
|
168 | else {
|
169 |
|
170 | namespace = arguments[0];
|
171 |
|
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 |
|
206 |
|
207 | async getDesignDocument() {
|
208 | let designDocName = arguments[0];
|
209 | let namespace;
|
210 | let options;
|
211 | let callback;
|
212 |
|
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 |
|
220 | namespace = undefined;
|
221 | options = undefined;
|
222 | callback = arguments[1];
|
223 | }
|
224 | else {
|
225 |
|
226 | namespace = arguments[1];
|
227 |
|
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 |
|
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 |
|
265 |
|
266 | async upsertDesignDocument() {
|
267 | const designDoc = arguments[0];
|
268 | let namespace;
|
269 | let options;
|
270 | let callback;
|
271 |
|
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 |
|
279 | namespace = undefined;
|
280 | options = undefined;
|
281 | callback = arguments[1];
|
282 | }
|
283 | else {
|
284 |
|
285 | namespace = arguments[1];
|
286 |
|
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 |
|
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 |
|
322 |
|
323 | async dropDesignDocument() {
|
324 | let designDocName = arguments[0];
|
325 | let namespace;
|
326 | let options;
|
327 | let callback;
|
328 |
|
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 |
|
336 | namespace = undefined;
|
337 | options = undefined;
|
338 | callback = arguments[1];
|
339 | }
|
340 | else {
|
341 |
|
342 | namespace = arguments[1];
|
343 |
|
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 |
|
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 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
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 | }
|
408 | exports.ViewIndexManager = ViewIndexManager;
|