import { Bucket } from './bucket' ; import { CouchbaseError , DesignDocumentNotFoundError } from './errors' ; import { HttpExecutor , HttpMethod , HttpServiceType } from './httpexecutor' ; import { RequestSpan } from './tracing' ; import { CompoundTimeout , NodeCallback , PromiseHelper } from './utilities' ; category export class DesignDocumentView { /**
     * The mapping function to use for this view.
     */ map : string ; /**
     * The reduction function to use for this view.
     */ reduce : string | undefined ; constructor ( data ) ; deprecated constructor ( map , reduce ? ) ; 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 ; } } category export class DesignDocument { /**
     * Same as  DesignDocumentView .
     *
     *  deprecated Use  DesignDocumentView directly.
      static get View ( ) : any { return DesignDocumentView ; } /**
     * The name of the design document.
     */ name : string ; /**
     * A map of the views that exist in this design document.
     */ views : { [ viewName ] : DesignDocumentView ; } ; constructor ( data ) ; deprecated constructor ( name , views ) ; 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 || { } ; } 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 } ) ; } } category export interface GetAllDesignDocumentOptions { /**
     * The parent tracing span that this operation will be part of.
     */ parentSpan ? : RequestSpan ; /**
     * The timeout for this operation, represented in milliseconds.
     */ timeout ? : number ; } category export interface GetDesignDocumentOptions { /**
     * The parent tracing span that this operation will be part of.
     */ parentSpan ? : RequestSpan ; /**
     * The timeout for this operation, represented in milliseconds.
     */ timeout ? : number ; } category export interface UpsertDesignDocumentOptions { /**
     * The parent tracing span that this operation will be part of.
     */ parentSpan ? : RequestSpan ; /**
     * The timeout for this operation, represented in milliseconds.
     */ timeout ? : number ; } category export interface DropDesignDocumentOptions { /**
     * The parent tracing span that this operation will be part of.
     */ parentSpan ? : RequestSpan ; /**
     * The timeout for this operation, represented in milliseconds.
     */ timeout ? : number ; } category export interface PublishDesignDocumentOptions { /**
     * The parent tracing span that this operation will be part of.
     */ parentSpan ? : RequestSpan ; /**
     * The timeout for this operation, represented in milliseconds.
     */ timeout ? : number ; } category export class ViewIndexManager { private _bucket : Bucket ; internal constructor ( bucket ) { this . _bucket = bucket ; } private get _http ( ) { return new HttpExecutor ( this . _bucket . conn ) ; } param options param callback async getAllDesignDocuments ( options ? , callback ? ) { if ( options instanceof Function ) { callback = arguments [ 0 ] ; options = undefined ; } if ( ! options ) { options = { } ; } const parentSpan = options . parentSpan ; const timeout = options . timeout ; return PromiseHelper . wrapAsync ( async ( ) => { const bucketName = this . _bucket . name ; const res = await this . _http . request ( { type : HttpServiceType . Management , method : HttpMethod . Get , path : `/pools/default/buckets/${ bucketName }/ddocs` , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; throw new CouchbaseError ( 'failed to get design documents' , undefined , errCtx ) ; } const ddocsData = JSON . parse ( res . body . toString ( ) ) ; const ddocs = ddocsData . rows . map ( ( ddocData ) => { const ddocName = ddocData . doc . meta . id . substr ( 8 ) ; return DesignDocument . _fromNsData ( ddocName , ddocData . doc . json ) ; } ) ; return ddocs ; } , callback ) ; } param designDocName param options param callback async getDesignDocument ( designDocName , options ? , callback ? ) { if ( options instanceof Function ) { callback = arguments [ 1 ] ; options = undefined ; } if ( ! options ) { options = { } ; } const parentSpan = options . parentSpan ; const timeout = options . timeout ; return PromiseHelper . wrapAsync ( async ( ) => { const res = await this . _http . request ( { type : HttpServiceType . Views , method : HttpMethod . Get , path : `/_design/${ designDocName }` , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; if ( res . statusCode === 404 ) { throw new DesignDocumentNotFoundError ( undefined , errCtx ) ; } throw new CouchbaseError ( 'failed to get the design document' , undefined , errCtx ) ; } const ddocData = JSON . parse ( res . body . toString ( ) ) ; return DesignDocument . _fromNsData ( designDocName , ddocData ) ; } , callback ) ; } param designDoc param options param callback async upsertDesignDocument ( designDoc , options ? , callback ? ) { if ( options instanceof Function ) { callback = arguments [ 1 ] ; options = undefined ; } if ( ! options ) { options = { } ; } const parentSpan = options . parentSpan ; const timeout = options . timeout ; return PromiseHelper . wrapAsync ( async ( ) => { const designDocData = { views : designDoc . views , } ; const encodedData = JSON . stringify ( designDocData ) ; const res = await this . _http . request ( { type : HttpServiceType . Views , method : HttpMethod . Put , path : `/_design/${ designDoc . name }` , contentType : 'application/json' , body : encodedData , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 201 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; throw new CouchbaseError ( 'failed to upsert the design document' , undefined , errCtx ) ; } } , callback ) ; } param designDocName param options param callback async dropDesignDocument ( designDocName , options ? , callback ? ) { if ( options instanceof Function ) { callback = arguments [ 1 ] ; options = undefined ; } if ( ! options ) { options = { } ; } const parentSpan = options . parentSpan ; const timeout = options . timeout ; return PromiseHelper . wrapAsync ( async ( ) => { const res = await this . _http . request ( { type : HttpServiceType . Views , method : HttpMethod . Delete , path : `/_design/${ designDocName }` , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; if ( res . statusCode === 404 ) { throw new DesignDocumentNotFoundError ( undefined , errCtx ) ; } throw new CouchbaseError ( 'failed to drop the design document' , undefined , errCtx ) ; } } , callback ) ; } param designDocName param options param callback async publishDesignDocument ( designDocName , options ? , callback ? ) { if ( options instanceof Function ) { callback = arguments [ 1 ] ; options = undefined ; } if ( ! options ) { options = { } ; } const parentSpan = options . parentSpan ; const timeout = options . timeout ; const timer = new CompoundTimeout ( timeout ) ; return PromiseHelper . wrapAsync ( async ( ) => { const designDoc = await this . getDesignDocument ( `dev_${ designDocName }` , { parentSpan : parentSpan , timeout : timer . left ( ) , } ) ; designDoc . name = designDocName ; await this . upsertDesignDocument ( designDoc , { timeout : timer . left ( ) , } ) ; } , callback ) ; } }