import { Bucket } from './bucket' ; import { CollectionExistsError , CollectionNotFoundError , CouchbaseError , FeatureNotAvailableError , ScopeExistsError , ScopeNotFoundError , } from './errors' ; import { HttpExecutor , HttpMethod , HttpServiceType } from './httpexecutor' ; import { RequestSpan } from './tracing' ; import { cbQsStringify , NodeCallback , PromiseHelper } from './utilities' ; category export interface ICollectionSpec { /**
     * The name of the scope.
     */ name : string ; /**
     * The name of the scope containing this collection.
     */ scopeName : string ; see IBucketSettings . maxExpiry maxExpiry ? : number ; } category export class CollectionSpec { /**
     * The name of the collection.
     */ name : string ; /**
     * The name of the scope this collection exists in.
     */ scopeName : string ; see IBucketSettings . maxExpiry maxExpiry : number ; internal constructor ( data ) { this . name = data . name ; this . scopeName = data . scopeName ; this . maxExpiry = data . maxExpiry ; } internal static _fromNsData ( scopeName , data ) { return new CollectionSpec ( { name : data . name , scopeName : scopeName , maxExpiry : data . maxTTL , } ) ; } internal static _toNsData ( data ) { return { name : data . name , maxTTL : data . maxExpiry , } ; } } category export class ScopeSpec { /**
     * The name of the scope.
     */ name : string ; /**
     * The collections which exist in this scope.
     */ collections : CollectionSpec [ ] ; internal constructor ( data ) { this . name = data . name ; this . collections = data . collections ; } internal static _fromNsData ( data ) { let collections ; if ( data . collections ) { const scopeName = data . name ; collections = data . collections . map ( ( collectionData ) => CollectionSpec . _fromNsData ( scopeName , collectionData ) ) ; } else { collections = [ ] ; } return new ScopeSpec ( { name : data . name , collections : collections , } ) ; } } category export interface CreateCollectionOptions { /**
     * 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 GetAllScopesOptions { /**
     * 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 DropCollectionOptions { /**
     * 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 CreateScopeOptions { /**
     * 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 DropScopeOptions { /**
     * 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 CollectionManager { private _bucket : Bucket ; internal constructor ( bucket ) { this . _bucket = bucket ; } private get _http ( ) { return new HttpExecutor ( this . _bucket . conn ) ; } param options param callback async getAllScopes ( options ? , callback ? ) { if ( options instanceof Function ) { callback = arguments [ 0 ] ; options = undefined ; } if ( ! options ) { options = { } ; } const bucketName = this . _bucket . name ; 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 }/scopes` , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; const errText = res . body . toString ( ) . toLowerCase ( ) ; if ( errText . includes ( 'not allowed on this version of cluster' ) || res . statusCode === 404 ) { throw new FeatureNotAvailableError ( undefined , errCtx ) ; } throw new CouchbaseError ( 'failed to get scopes' , undefined , errCtx ) ; } const scopesData = JSON . parse ( res . body . toString ( ) ) ; const scopes = scopesData . scopes . map ( ( scopeData ) => ScopeSpec . _fromNsData ( scopeData ) ) ; return scopes ; } , callback ) ; } deprecated async createCollection ( collectionName , scopeName , options ? , callback ? ) ; param collectionSpec param options param callback async createCollection ( collectionSpec , options ? , callback ? ) ; internal async createCollection ( ) { let collectionSpec = arguments [ 0 ] ; let options = arguments [ 1 ] ; let callback = arguments [ 2 ] ; if ( typeof collectionSpec === 'string' ) { collectionSpec = { name : arguments [ 0 ] , scopeName : arguments [ 1 ] , } ; options = arguments [ 2 ] ; callback = arguments [ 3 ] ; } if ( options instanceof Function ) { callback = arguments [ 1 ] ; options = undefined ; } if ( ! options ) { options = { } ; } const bucketName = this . _bucket . name ; const parentSpan = options . parentSpan ; const timeout = options . timeout ; return PromiseHelper . wrapAsync ( async ( ) => { const collectionData = CollectionSpec . _toNsData ( collectionSpec ) ; const res = await this . _http . request ( { type : HttpServiceType . Management , method : HttpMethod . Post , path : `/pools/default/buckets/${ bucketName }/scopes/${ collectionSpec . scopeName }/collections` , contentType : 'application/x-www-form-urlencoded' , body : cbQsStringify ( collectionData ) , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; const errText = res . body . toString ( ) . toLowerCase ( ) ; if ( errText . includes ( 'already exists' ) && errText . includes ( 'collection' ) ) { throw new CollectionExistsError ( undefined , errCtx ) ; } if ( errText . includes ( 'not found' ) && errText . includes ( 'scope' ) ) { throw new ScopeNotFoundError ( undefined , errCtx ) ; } if ( errText . includes ( 'not allowed on this version of cluster' ) || res . statusCode === 404 ) { throw new FeatureNotAvailableError ( undefined , errCtx ) ; } throw new CouchbaseError ( 'failed to create collection' , undefined , errCtx ) ; } } , callback ) ; } param collectionName param scopeName param options param callback async dropCollection ( collectionName , scopeName , options ? , callback ? ) { if ( options instanceof Function ) { callback = arguments [ 2 ] ; options = undefined ; } if ( ! options ) { options = { } ; } const bucketName = this . _bucket . name ; 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 }/scopes/${ scopeName }/collections/${ collectionName }` , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; const errText = res . body . toString ( ) . toLowerCase ( ) ; if ( errText . includes ( 'not found' ) && errText . includes ( 'collection' ) ) { throw new CollectionNotFoundError ( undefined , errCtx ) ; } if ( errText . includes ( 'not found' ) && errText . includes ( 'scope' ) ) { throw new ScopeNotFoundError ( undefined , errCtx ) ; } if ( errText . includes ( 'not allowed on this version of cluster' ) || res . statusCode === 404 ) { throw new FeatureNotAvailableError ( undefined , errCtx ) ; } throw new CouchbaseError ( 'failed to drop collection' , undefined , errCtx ) ; } } , callback ) ; } param scopeName param options param callback async createScope ( scopeName , options ? , callback ? ) { if ( options instanceof Function ) { callback = arguments [ 1 ] ; options = undefined ; } if ( ! options ) { options = { } ; } const bucketName = this . _bucket . name ; 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 }/scopes` , contentType : 'application/x-www-form-urlencoded' , body : cbQsStringify ( { name : scopeName , } ) , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; const errText = res . body . toString ( ) . toLowerCase ( ) ; if ( errText . includes ( 'already exists' ) && errText . includes ( 'scope' ) ) { throw new ScopeExistsError ( undefined , errCtx ) ; } if ( errText . includes ( 'not allowed on this version of cluster' ) || res . statusCode === 404 ) { throw new FeatureNotAvailableError ( undefined , errCtx ) ; } throw new CouchbaseError ( 'failed to create scope' , undefined , errCtx ) ; } } , callback ) ; } param scopeName param options param callback async dropScope ( scopeName , options ? , callback ? ) { if ( options instanceof Function ) { callback = arguments [ 1 ] ; options = undefined ; } if ( ! options ) { options = { } ; } const bucketName = this . _bucket . name ; 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 }/scopes/${ scopeName }` , parentSpan : parentSpan , timeout : timeout , } ) ; if ( res . statusCode !== 200 ) { const errCtx = HttpExecutor . errorContextFromResponse ( res ) ; const errText = res . body . toString ( ) . toLowerCase ( ) ; if ( errText . includes ( 'not found' ) && errText . includes ( 'scope' ) ) { throw new ScopeNotFoundError ( undefined , errCtx ) ; } if ( errText . includes ( 'not allowed on this version of cluster' ) || res . statusCode === 404 ) { throw new FeatureNotAvailableError ( undefined , errCtx ) ; } throw new CouchbaseError ( 'failed to drop scope' , undefined , errCtx ) ; } } , callback ) ; } }