UNPKG

couchbase

Version:

The official Couchbase Node.js Client Library.

438 lines (437 loc) 15.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ViewIndexManager = exports.DesignDocument = exports.DesignDocumentView = void 0; const utilities_1 = require("./utilities"); const viewtypes_1 = require("./viewtypes"); const bindingutilities_1 = require("./bindingutilities"); const observability_1 = require("./observability"); const observabilityhandler_1 = require("./observabilityhandler"); const observabilitytypes_1 = require("./observabilitytypes"); /** * Contains information about a view in a design document. * * @category Management */ class DesignDocumentView { /** * @internal */ constructor(...args) { let data; if (typeof args[0] === 'string' || typeof args[0] === 'function') { data = { map: args[0], reduce: args[1], }; } else { data = args[0]; } this.map = data.map; this.reduce = data.reduce; } /** * @internal */ static _toCppData(name, data) { return { name: name, map: data.map, reduce: data.reduce, }; } /** * @internal */ static _fromCppData(data) { return new DesignDocumentView({ map: data.map, reduce: data.reduce, }); } } exports.DesignDocumentView = DesignDocumentView; /** * Contains information about a design document. * * @category Management */ class DesignDocument { /** * Same as {@link DesignDocumentView}. * * @deprecated Use {@link DesignDocumentView} directly. */ static get View() { return DesignDocumentView; } /** * @internal */ constructor(...args) { let data; if (typeof args[0] === 'string') { data = { name: args[0], views: args[1], }; } else { data = args[0]; } this.name = data.name; this.views = data.views || {}; this.namespace = data.namespace || viewtypes_1.DesignDocumentNamespace.Production; this.rev = data.rev; } /** * @internal */ static _fromNsData(ddocName, ddocData) { const views = {}; for (const viewName in ddocData.views) { const viewData = ddocData.views[viewName]; views[viewName] = new DesignDocumentView({ map: viewData.map, reduce: viewData.reduce, }); } return new DesignDocument({ name: ddocName, views: views }); } /** * @internal */ static _toCppData(data, namespace) { const cppView = {}; for (const [k, v] of Object.entries(data.views)) { cppView[k] = DesignDocumentView._toCppData(k, v); } return { rev: data.rev, name: data.name, ns: (0, bindingutilities_1.designDocumentNamespaceToCpp)(namespace), views: cppView, }; } /** * @internal */ static _fromCppData(ddoc) { const views = {}; for (const [viewName, viewData] of Object.entries(ddoc.views)) { views[viewName] = DesignDocumentView._fromCppData(viewData); } return new DesignDocument({ name: ddoc.name, views: views, namespace: (0, bindingutilities_1.designDocumentNamespaceFromCpp)(ddoc.ns), rev: ddoc.rev, }); } } exports.DesignDocument = DesignDocument; /** * ViewIndexManager is an interface which enables the management * of view indexes on the cluster. * * @deprecated Since version 4.7.0. Views are deprecated in Couchbase Server 7.0+, and will be removed from a future server version. * Views are not compatible with the Magma storage engine. Instead of views, use indexes and queries using the * Index Service (GSI) and the Query Service (SQL++). * * @category Management */ class ViewIndexManager { /** * @internal */ constructor(bucket) { this._bucket = bucket; } /** * @internal */ get _cluster() { return this._bucket.cluster; } /** * @internal */ get observabilityInstruments() { return this._bucket.cluster.observabilityInstruments; } /** * @internal */ async getAllDesignDocuments() { let namespace; let options; let callback; if (typeof arguments[0] === 'object') { namespace = undefined; options = arguments[0]; callback = arguments[1]; } else if (arguments[0] instanceof Function) { namespace = undefined; options = undefined; callback = arguments[0]; } else { namespace = arguments[0]; if (arguments[1] instanceof Function) { callback = arguments[1]; options = undefined; } else { options = arguments[1]; callback = arguments[2]; } } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.ViewIndexMgmtOp.ViewIndexGetAll, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes(); try { const timeout = options.timeout || this._cluster.managementTimeout; const ns = namespace !== null && namespace !== void 0 ? namespace : viewtypes_1.DesignDocumentNamespace.Production; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, resp] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementViewIndexGetAll.bind(this._cluster.conn), { bucket_name: this._bucket.name, ns: (0, bindingutilities_1.designDocumentNamespaceToCpp)(ns), timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); return resp.design_documents.map((ddoc) => DesignDocument._fromCppData(ddoc)); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * @internal */ async getDesignDocument() { let designDocName = arguments[0]; let namespace; let options; let callback; if (typeof arguments[1] === 'object') { namespace = undefined; options = arguments[1]; callback = arguments[2]; } else if (arguments[1] instanceof Function) { namespace = undefined; options = undefined; callback = arguments[1]; } else { namespace = arguments[1]; if (arguments[2] instanceof Function) { callback = arguments[2]; options = undefined; } else { options = arguments[2]; callback = arguments[3]; } } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.ViewIndexMgmtOp.ViewIndexGet, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes(); try { const timeout = options.timeout || this._cluster.managementTimeout; if (designDocName.startsWith('dev_')) { namespace = viewtypes_1.DesignDocumentNamespace.Development; designDocName = designDocName.substring(4); } const ns = namespace !== null && namespace !== void 0 ? namespace : viewtypes_1.DesignDocumentNamespace.Production; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, resp] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementViewIndexGet.bind(this._cluster.conn), { bucket_name: this._bucket.name, document_name: designDocName, ns: (0, bindingutilities_1.designDocumentNamespaceToCpp)(ns), timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); return DesignDocument._fromCppData(resp.document); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * @internal */ async upsertDesignDocument() { const designDoc = arguments[0]; let namespace; let options; let callback; if (typeof arguments[1] === 'object') { namespace = undefined; options = arguments[1]; callback = arguments[2]; } else if (arguments[1] instanceof Function) { namespace = undefined; options = undefined; callback = arguments[1]; } else { namespace = arguments[1]; if (arguments[2] instanceof Function) { callback = arguments[2]; options = undefined; } else { options = arguments[2]; callback = arguments[3]; } } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.ViewIndexMgmtOp.ViewIndexUpsert, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes(); try { const timeout = options.timeout || this._cluster.managementTimeout; if (designDoc.name.startsWith('dev_')) { namespace = viewtypes_1.DesignDocumentNamespace.Development; designDoc.name = designDoc.name.substring(4); } const ns = namespace !== null && namespace !== void 0 ? namespace : viewtypes_1.DesignDocumentNamespace.Production; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementViewIndexUpsert.bind(this._cluster.conn), { bucket_name: this._bucket.name, document: DesignDocument._toCppData(designDoc, ns), timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * @internal */ async dropDesignDocument() { let designDocName = arguments[0]; let namespace; let options; let callback; if (typeof arguments[1] === 'object') { namespace = undefined; options = arguments[1]; callback = arguments[2]; } else if (arguments[1] instanceof Function) { namespace = undefined; options = undefined; callback = arguments[1]; } else { namespace = arguments[1]; if (arguments[2] instanceof Function) { callback = arguments[2]; options = undefined; } else { options = arguments[2]; callback = arguments[3]; } } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.ViewIndexMgmtOp.ViewIndexDrop, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes(); try { const timeout = options.timeout || this._cluster.managementTimeout; if (designDocName.startsWith('dev_')) { namespace = viewtypes_1.DesignDocumentNamespace.Development; designDocName = designDocName.substring(4); } const ns = namespace !== null && namespace !== void 0 ? namespace : viewtypes_1.DesignDocumentNamespace.Production; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementViewIndexDrop.bind(this._cluster.conn), { bucket_name: this._bucket.name, document_name: designDocName, ns: (0, bindingutilities_1.designDocumentNamespaceToCpp)(ns), timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * Publishes a development design document to be a production design document. * It does this by fetching the design document by the passed name with `dev_` * appended, and then performs an upsert of the production name (no `dev_`) * with the data which was just fetched. * * @param designDocName The name of the design document to publish. * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async publishDesignDocument(designDocName, options, callback) { if (options instanceof Function) { callback = arguments[1]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.ViewIndexMgmtOp.ViewIndexPublish, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes(); try { const timeout = options.timeout || this._cluster.managementTimeout; const timer = new utilities_1.CompoundTimeout(timeout); return utilities_1.PromiseHelper.wrapAsync(async () => { const designDoc = await this.getDesignDocument(designDocName, viewtypes_1.DesignDocumentNamespace.Development, { timeout: timer.left(), parentSpan: obsReqHandler.wrappedSpan, }); await this.upsertDesignDocument(designDoc, viewtypes_1.DesignDocumentNamespace.Production, { timeout: timer.left(), parentSpan: obsReqHandler.wrappedSpan, }); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } } exports.ViewIndexManager = ViewIndexManager;