import { Cluster } from './cluster' ; import { BucketExistsError , BucketNotFoundError , CouchbaseError , } from './errors' ; import { DurabilityLevel } from './generaltypes' ; import { HttpExecutor , HttpMethod , HttpServiceType } from './httpexecutor' ; import { RequestSpan } from './tracing' ; import { cbQsStringify , duraLevelToNsServerStr , NodeCallback , nsServerStrToDuraLevel , PromiseHelper , } from './utilities' ; category export enum ConflictResolutionType { /**
     * Indicates that timestamps should be used for conflict resolution.  The most
     * recently modified document (according to each server, ie: time synchronization
     * is important) is the one selected to win.
     */ Timestamp = 'lww' , /**
     * Indicates that the seqno of the document should be used for conflict resolution.
     */ SequenceNumber = 'seqno' , experimental Custom = 'custom' } category export enum BucketType { /**
     * Indicates the bucket should be a Couchbase bucket.
     */ Couchbase = 'membase' , /**
     * Indicates the bucket should be a Memcached bucket.
     */ Memcached = 'memcached' , /**
     * Indicates the bucket should be a Ephemeral bucket.
     */ Ephemeral = 'ephemeral' } category export enum StorageBackend { /**
     * Indicates the bucket should use the Couchstore storage engine.
     */ Couchstore = 'couchstore' , /**
     * Indicates the bucket should use the Magma storage engine.
     */ Magma = 'magma' } category export enum EvictionPolicy { /**
     * Indicates that both the document meta-data and value should be evicted.
     */ FullEviction = 'fullEviction' , /**
     * Indicates that only the value of a document should be evicted.
     */ ValueOnly = 'valueOnly' , /**
     * Indicates that the least recently used documents are evicted.
     */ NotRecentlyUsed = 'nruEviction' , /**
     * Indicates that nothing should be evicted.
     */ NoEviction = 'noEviction' } category export enum CompressionMode { /**
     * Indicates that compression should not be used on the server.
     */ Off = 'off' , /**
     * Indicates that compression should be used passively.  That is that if the
     * client sends data which is encrypted, it is stored on the server in its
     * compressed form, but the server does not actively compress documents.
     */ Passive = 'passive' , /**
     * Indicates that compression should be performed actively.  Even if the
     * client does not transmit the document in a compressed form.
     */ Active = 'active' } category export interface IBucketSettings { /**
     * The name of the bucket.
     */ name : string ; /**
     * Whether the flush operation (truncating all data in the bucket) should
     * be enabled.
     */ flushEnabled : boolean ; /**
     * The amount of RAM which should be allocated to this bucket, expressed in
     * megabytes.
     */ ramQuotaMB : number ; /**
     * The number of replicas that should exist for this bucket.
     */ numReplicas : number ; /**
     * Whether the indexes on this bucket should be replicated.
     */ replicaIndexes : boolean ; /**
     * Specifies the type of bucket that should be used.
     */ bucketType : BucketType | string ; /**
     * Specifies the storage backend to use for the bucket.
     */ storageBackend : StorageBackend | string ; /**
     * Specifies the ejection method that should be used.
     */ evictionPolicy : EvictionPolicy | string ; /**
     * Specifies the maximum expiry time that any document should be permitted
     * to have.  Any documents stored with an expiry configured higher than this
     * will be forced to this number.
     */ maxExpiry : number ; /**
     * Specifies the compression mode that should be used.
     */ compressionMode : CompressionMode | string ; /**
     * Specifies the minimum durability level that should be used for any write
     * operations which are performed against this bucket.
     */ minimumDurabilityLevel : DurabilityLevel | string ; /**
     * Same as  IBucketSettings . maxExpiry .
     *
     *  deprecated Use  IBucketSettings . maxExpiry instead.
      maxTTL : number ; /**
     * Same as  IBucketSettings . minimumDurabilityLevel , but represented as
     * the raw server-side configuration string.
     *
     *  deprecated Use  IBucketSettings . minimumDurabilityLevel instead.
      durabilityMinLevel : string ; /**
     * Same as  IBucketSettings . evictionPolicy , but represented as
     * the raw server-side configuration string.
     *
     *  deprecated Use  IBucketSettings . evictionPolicy instead.
      ejectionMethod ? : EvictionPolicy | string ; } category export class BucketSettings implements IBucketSettings { /**
     * The name of the bucket.
     */ name : string ; /**
     * Whether the flush operation (truncating all data in the bucket) should
     * be enabled.
     */ flushEnabled : boolean ; /**
     * The amount of RAM which should be allocated to this bucket, expressed in
     * megabytes.
     */ ramQuotaMB : number ; /**
     * The number of replicas that should exist for this bucket.
     */ numReplicas : number ; /**
     * Whether the indexes on this bucket should be replicated.
     */ replicaIndexes : boolean ; /**
     * Specifies the type of bucket that should be used.
     */ bucketType : BucketType ; /**
     * Specifies the storage backend to use for the bucket.
     */ storageBackend : StorageBackend | string ; /**
     * Specifies the ejection method that should be used.
     */ evictionPolicy : EvictionPolicy ; /**
     * Specifies the maximum expiry time that any document should be permitted
     * to have.  Any documents stored with an expiry configured higher than this
     * will be forced to this number.
     */ maxExpiry : number ; /**
     * Specifies the compression mode that should be used.
     */ compressionMode : CompressionMode ; /**
     * Specifies the minimum durability level that should be used for any write
     * operations which are performed against this bucket.
     */ minimumDurabilityLevel : DurabilityLevel ; internal constructor ( data ) { this . name = data . name ; this . flushEnabled = data . flushEnabled ; this . ramQuotaMB = data . ramQuotaMB ; this . numReplicas = data . numReplicas ; this . replicaIndexes = data . replicaIndexes ; this . bucketType = data . bucketType ; this . storageBackend = data . storageBackend ; this . evictionPolicy = data . evictionPolicy ; this . maxExpiry = data . maxExpiry ; this . compressionMode = data . compressionMode ; this . minimumDurabilityLevel = data . minimumDurabilityLevel ; } /**
     * Same as  IBucketSettings . maxExpiry .
     *
     *  deprecated Use  IBucketSettings . maxExpiry instead.
      get maxTTL ( ) : number { return this . maxExpiry ; } set maxTTL ( val ) { this . maxExpiry = val ; } /**
     * Same as  IBucketSettings . evictionPolicy .
     *
     *  deprecated Use  IBucketSettings . evictionPolicy instead.
      get ejectionMethod ( ) : EvictionPolicy | string { return this . evictionPolicy ; } set ejectionMethod ( val ) { this . evictionPolicy = val as EvictionPolicy ; } /**
     * Same as  IBucketSettings . minimumDurabilityLevel , but represented as
     * the raw server-side configuration string.
     *
     *  deprecated Use  IBucketSettings . minimumDurabilityLevel instead.
      get durabilityMinLevel ( ) : string { return duraLevelToNsServerStr ( this . minimumDurabilityLevel ) as string ; } internal static _toNsData ( data ) { return { name : data . name , flushEnabled : data . flushEnabled , ramQuotaMB : data . ramQuotaMB , replicaNumber : data . numReplicas , replicaIndexes : data . replicaIndexes , bucketType : data . bucketType , storageBackend : data . storageBackend , evictionPolicy : data . evictionPolicy , maxTTL : data . maxTTL || data . maxExpiry , compressionMode : data . compressionMode , durabilityMinLevel : data . durabilityMinLevel || duraLevelToNsServerStr ( data . minimumDurabilityLevel ) , } ; } internal static _fromNsData ( data ) { return new BucketSettings ( { name : data . name , flushEnabled : data . controllers && data . controllers . flush , ramQuotaMB : data . ramQuotaMB , numReplicas : data . replicaNumber , replicaIndexes : data . replicaIndexes , bucketType : data . bucketType , storageBackend : data . storageBackend , evictionPolicy : data . evictionPolicy , maxExpiry : data . maxTTL , compressionMode : data . compressionMode , minimumDurabilityLevel : nsServerStrToDuraLevel ( data . durabilityMinLevel ) , maxTTL : 0 , durabilityMinLevel : '' , ejectionMethod : '' , } ) ; } } category export interface ICreateBucketSettings extends IBucketSettings { /**
     * Specifies the conflict resolution mode to use for XDCR of this bucket.
     */ conflictResolutionType : ConflictResolutionType | string ; } internal class CreateBucketSettings extends BucketSettings implements ICreateBucketSettings { /**
     * Specifies the conflict resolution mode to use for XDCR of this bucket.
     */ conflictResolutionType : ConflictResolutionType ; internal constructor ( data ) { super ( data ) ; this . conflictResolutionType = data . conflictResolutionType ; } internal static _toNsData ( data ) { return { ... BucketSettings . _toNsData ( data ) , conflictResolutionType : data . conflictResolutionType , } ; } } category export interface CreateBucketOptions { /**
     * 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 UpdateBucketOptions { /**
     * 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 DropBucketOptions { /**
     * 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 GetBucketOptions { /**
     * 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 GetAllBucketsOptions { /**
     * 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 FlushBucketOptions { /**
     * 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 BucketManager { private _cluster : Cluster ; internal constructor ( cluster ) { this . _cluster = cluster ; } private get _http ( ) { return new HttpExecutor ( this . _cluster . _getClusterConn ( ) ) ; } param settings param options param callback async createBucket ( settings , 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 bucketData = CreateBucketSettings . _toNsData ( settings ) ; const res = await this . _http . request ( { type : HttpServiceType . Management , method : HttpMethod . Post , path : `/pools/default/buckets` , contentType : 'application/x-www-form-urlencoded' , body : cbQsStringify ( bucketData ) , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 202 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; const errText = res . body . toString ( ) . toLowerCase ( ) ; if ( errText . includes ( 'already exists' ) ) { throw new BucketExistsError ( undefined , errCtx ) ; } throw new CouchbaseError ( 'failed to create bucket' , undefined , errCtx ) ; } } , callback ) ; } param settings param options param callback async updateBucket ( settings , 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 bucketData = BucketSettings . _toNsData ( settings ) ; const res = await this . _http . request ( { type : HttpServiceType . Management , method : HttpMethod . Post , path : `/pools/default/buckets/${ settings . name }` , contentType : 'application/x-www-form-urlencoded' , body : cbQsStringify ( bucketData ) , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; const errText = res . body . toString ( ) . toLowerCase ( ) ; if ( errText . includes ( 'not found' ) ) { throw new BucketNotFoundError ( undefined , errCtx ) ; } throw new CouchbaseError ( 'failed to update bucket' , undefined , errCtx ) ; } } , callback ) ; } param bucketName param options param callback async dropBucket ( bucketName , 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 . Management , method : HttpMethod . Delete , path : `/pools/default/buckets/${ bucketName }` , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; const errText = res . body . toString ( ) . toLowerCase ( ) ; if ( errText . includes ( 'not found' ) ) { throw new BucketNotFoundError ( undefined , errCtx ) ; } throw new CouchbaseError ( 'failed to drop bucket' , undefined , errCtx ) ; } } , callback ) ; } param bucketName param options param callback async getBucket ( bucketName , 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 . Management , method : HttpMethod . Get , path : `/pools/default/buckets/${ bucketName }` , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; const errText = res . body . toString ( ) . toLowerCase ( ) ; if ( errText . includes ( 'not found' ) ) { throw new BucketNotFoundError ( undefined , errCtx ) ; } throw new CouchbaseError ( 'failed to get bucket' , undefined , errCtx ) ; } const bucketData = JSON . parse ( res . body . toString ( ) ) ; return BucketSettings . _fromNsData ( bucketData ) ; } , callback ) ; } param options param callback async getAllBuckets ( 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 res = await this . _http . request ( { type : HttpServiceType . Management , method : HttpMethod . Get , path : `/pools/default/buckets` , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; throw new CouchbaseError ( 'failed to get buckets' , undefined , errCtx ) ; } const bucketsData = JSON . parse ( res . body . toString ( ) ) ; const buckets = bucketsData . map ( ( bucketData ) => BucketSettings . _fromNsData ( bucketData ) ) ; return buckets ; } , callback ) ; } param bucketName param options param callback async flushBucket ( bucketName , 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 . Management , method : HttpMethod . Post , path : `/pools/default/buckets/${ bucketName }/controller/doFlush` , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; const errText = res . body . toString ( ) . toLowerCase ( ) ; if ( errText . includes ( 'not found' ) ) { throw new BucketNotFoundError ( undefined , errCtx ) ; } throw new CouchbaseError ( 'failed to get bucket' , undefined , errCtx ) ; } } , callback ) ; } }