UNPKG

3.66 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ViewExecutor = void 0;
4/* eslint jsdoc/require-jsdoc: off */
5const bindingutilities_1 = require("./bindingutilities");
6const streamablepromises_1 = require("./streamablepromises");
7const viewtypes_1 = require("./viewtypes");
8/**
9 * @internal
10 */
11class ViewExecutor {
12 /**
13 * @internal
14 */
15 constructor(bucket) {
16 this._bucket = bucket;
17 }
18 /**
19 @internal
20 */
21 get _cluster() {
22 return this._bucket.cluster;
23 }
24 /**
25 * @internal
26 */
27 query(designDoc, viewName, options) {
28 var _a;
29 const emitter = new streamablepromises_1.StreamableRowPromise((rows, meta) => {
30 return new viewtypes_1.ViewResult({
31 rows: rows,
32 meta: meta,
33 });
34 });
35 const timeout = options.timeout || this._cluster.viewTimeout;
36 const raw = options.raw || {};
37 const ns = (_a = options.namespace) !== null && _a !== void 0 ? _a : viewtypes_1.DesignDocumentNamespace.Production;
38 let fullSet = options.full_set;
39 if (typeof options.fullSet !== 'undefined') {
40 fullSet = options.fullSet;
41 }
42 this._cluster.conn.documentView({
43 timeout: timeout,
44 bucket_name: this._bucket.name,
45 document_name: designDoc,
46 view_name: viewName,
47 ns: (0, bindingutilities_1.designDocumentNamespaceToCpp)(ns),
48 limit: options.limit,
49 skip: options.skip,
50 consistency: (0, bindingutilities_1.viewScanConsistencyToCpp)(options.scanConsistency),
51 keys: options.keys ? options.keys.map((k) => JSON.stringify(k)) : [],
52 key: JSON.stringify(options.key),
53 start_key: options.range && options.range.start
54 ? JSON.stringify(options.range.start)
55 : undefined,
56 end_key: options.range && options.range.end
57 ? JSON.stringify(options.range.end)
58 : undefined,
59 inclusive_end: options.range ? options.range.inclusiveEnd : undefined,
60 start_key_doc_id: options.idRange && options.idRange.start
61 ? options.idRange.start
62 : undefined,
63 end_key_doc_id: options.idRange && options.idRange.end
64 ? options.idRange.end
65 : undefined,
66 reduce: options.reduce,
67 group: options.group,
68 group_level: options.groupLevel,
69 order: (0, bindingutilities_1.viewOrderingToCpp)(options.order),
70 debug: false,
71 query_string: [],
72 raw: raw,
73 full_set: fullSet,
74 }, (cppErr, resp) => {
75 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
76 if (err) {
77 emitter.emit('error', err);
78 emitter.emit('end');
79 return;
80 }
81 resp.rows.forEach((row) => {
82 emitter.emit('row', new viewtypes_1.ViewRow({
83 value: JSON.parse(row.value),
84 id: row.id,
85 key: JSON.parse(row.key),
86 }));
87 });
88 {
89 const metaData = resp.meta;
90 const meta = new viewtypes_1.ViewMetaData({
91 totalRows: metaData.total_rows,
92 debug: metaData.debug_info,
93 });
94 emitter.emit('meta', meta);
95 }
96 emitter.emit('end');
97 return;
98 });
99 return emitter;
100 }
101}
102exports.ViewExecutor = ViewExecutor;