import { Collection } from './collection' ; import { CouchbaseError , PathExistsError , PathInvalidError } from './errors' ; import { StoreSemantics } from './generaltypes' ; import { LookupInSpec , MutateInSpec } from './sdspecs' ; import { NodeCallback , PromiseHelper } from './utilities' ; see Collection . list category export class CouchbaseList { private _coll : Collection ; private _key : string ; internal constructor ( collection , key ) { this . _coll = collection ; this . _key = key ; } private async _get ( ) { const doc = await this . _coll . get ( this . _key ) ; if ( ! ( doc . content instanceof Array ) ) { throw new CouchbaseError ( 'expected document of array type' ) ; } return doc . content ; } param callback async getAll ( callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { return await this . _get ( ) ; } , callback ) ; } param rowCallback param callback async forEach ( rowCallback , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const values = await this . _get ( ) ; for ( let i = 0 ; i < values . length ; ++ i ) { rowCallback ( values [ i ] , i , this ) ; } } , callback ) ; } /**
     * Provides the ability to async-for loop this object.
     */ [ Symbol . asyncIterator ] ( ) { const getNext = async ( ) => this . _get ( ) ; return { data : null as null | any [ ] , index : - 1 , async next ( ) { if ( this . index < 0 ) { this . data = await getNext ( ) ; this . index = 0 ; } const data = this . data as any [ ] ; if ( this . index < data . length ) { return { done : false , value : data [ this . index ++ ] } ; } return { done : true } ; } , } as any ; } param index param callback async getAt ( index , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const res = await this . _coll . lookupIn ( this . _key , [ LookupInSpec . get ( '[' + index + ']' ) , ] ) ; const itemRes = res . content [ 0 ] ; if ( itemRes . error ) { throw itemRes . error ; } return itemRes . value ; } , callback ) ; } param index param callback async removeAt ( index , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { await this . _coll . mutateIn ( this . _key , [ MutateInSpec . remove ( '[' + index + ']' ) , ] ) ; } , callback ) ; } param value param callback async indexOf ( value , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const items = await this . _get ( ) ; for ( let i = 0 ; i < items . length ; ++ i ) { if ( items [ i ] === value ) { return i ; } } return - 1 ; } , callback ) ; } param callback async size ( callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const res = await this . _coll . lookupIn ( this . _key , [ LookupInSpec . count ( '' ) ] ) ; return res . content [ 0 ] . value ; } , callback ) ; } param value param callback async push ( value , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { await this . _coll . mutateIn ( this . _key , [ MutateInSpec . arrayAppend ( '' , value ) ] , { storeSemantics : StoreSemantics . Upsert , } ) ; } , callback ) ; } param value param callback async unshift ( value , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { await this . _coll . mutateIn ( this . _key , [ MutateInSpec . arrayPrepend ( '' , value ) ] , { storeSemantics : StoreSemantics . Upsert , } ) ; } , callback ) ; } } see Collection . map category export class CouchbaseMap { private _coll : Collection ; private _key : string ; internal constructor ( collection , key ) { this . _coll = collection ; this . _key = key ; } private async _get ( ) { const doc = await this . _coll . get ( this . _key ) ; if ( ! ( doc . content instanceof Object ) ) { throw new CouchbaseError ( 'expected document of object type' ) ; } return doc . content ; } param callback async getAll ( callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { return await this . _get ( ) ; } , callback ) ; } param rowCallback param callback async forEach ( rowCallback , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const values = await this . _get ( ) ; for ( const i in values ) { rowCallback ( values [ i ] , i , this ) ; } } , callback ) ; } /**
     * Provides the ability to async-for loop this object.
     */ [ Symbol . asyncIterator ] ( ) { const getNext = async ( ) => this . _get ( ) ; return { data : null as { [ key ] : any ; } | null , keys : null as string [ ] | null , index : - 1 , async next ( ) { if ( this . index < 0 ) { this . data = await getNext ( ) ; this . keys = Object . keys ( this . data ) ; this . index = 0 ; } const keys = this . keys as string [ ] ; const data = this . data as { [ key ] : any ; } ; if ( this . index < keys . length ) { const key = keys [ this . index ++ ] ; return { done : false , value : [ data [ key ] , key ] } ; } return { done : true , value : undefined } ; } , } as any ; } param item param value param callback async set ( item , value , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { await this . _coll . mutateIn ( this . _key , [ MutateInSpec . upsert ( item , value ) ] , { storeSemantics : StoreSemantics . Upsert , } ) ; } , callback ) ; } param item param callback async get ( item , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const res = await this . _coll . lookupIn ( this . _key , [ LookupInSpec . get ( item ) ] ) ; const itemRes = res . content [ 0 ] ; if ( itemRes . error ) { throw itemRes . error ; } return itemRes . value ; } , callback ) ; } param item param callback async remove ( item , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { await this . _coll . mutateIn ( this . _key , [ MutateInSpec . remove ( item ) ] ) ; } , callback ) ; } param item param callback async exists ( item , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const res = await this . _coll . lookupIn ( this . _key , [ LookupInSpec . exists ( item ) , ] ) ; const itemRes = res . content [ 0 ] ; return itemRes . value ; } , callback ) ; } param callback async keys ( callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const values = await this . _get ( ) ; return Object . keys ( values ) ; } , callback ) ; } param callback async values ( callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const values = await this . _get ( ) ; return Object . values ( values ) ; } , callback ) ; } param callback async size ( callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const res = await this . _coll . lookupIn ( this . _key , [ LookupInSpec . count ( '' ) ] ) ; return res . content [ 0 ] . value ; } , callback ) ; } } see Collection . queue category export class CouchbaseQueue { private _coll : Collection ; private _key : string ; internal constructor ( collection , key ) { this . _coll = collection ; this . _key = key ; } private async _get ( ) { const doc = await this . _coll . get ( this . _key ) ; if ( ! ( doc . content instanceof Array ) ) { throw new CouchbaseError ( 'expected document of array type' ) ; } return doc . content ; } param callback async size ( callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const res = await this . _coll . lookupIn ( this . _key , [ LookupInSpec . count ( '' ) ] ) ; return res . content [ 0 ] . value ; } , callback ) ; } param value param callback async push ( value , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { await this . _coll . mutateIn ( this . _key , [ MutateInSpec . arrayPrepend ( '' , value ) ] , { storeSemantics : StoreSemantics . Upsert , } ) ; } , callback ) ; } param callback async pop ( callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { for ( let i = 0 ; i < 16 ; ++ i ) { try { const res = await this . _coll . lookupIn ( this . _key , [ LookupInSpec . get ( '[-1]' ) , ] ) ; const value = res . content [ 0 ] . value ; await this . _coll . mutateIn ( this . _key , [ MutateInSpec . remove ( '[-1]' ) ] , { cas : res . cas , } ) ; return value ; } catch ( e ) { if ( e instanceof PathInvalidError ) { throw new CouchbaseError ( 'no items available in list' ) ; } } } throw new CouchbaseError ( 'no items available to pop' ) ; } , callback ) ; } } see Collection . set category export class CouchbaseSet { private _coll : Collection ; private _key : string ; internal constructor ( collection , key ) { this . _coll = collection ; this . _key = key ; } private async _get ( ) { const doc = await this . _coll . get ( this . _key ) ; if ( ! ( doc . content instanceof Array ) ) { throw new CouchbaseError ( 'expected document of array type' ) ; } return doc . content ; } param item param callback async add ( item , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { try { await this . _coll . mutateIn ( this . _key , [ MutateInSpec . arrayAddUnique ( '' , item ) ] , { storeSemantics : StoreSemantics . Upsert , } ) ; } catch ( e ) { if ( e instanceof PathExistsError ) { return false ; } throw e ; } return true ; } , callback ) ; } param item param callback async contains ( item , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const values = await this . _get ( ) ; for ( let i = 0 ; i < values . length ; ++ i ) { if ( values [ i ] === item ) { return true ; } } return false ; } , callback ) ; } param item param callback async remove ( item , callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { for ( let i = 0 ; i < 16 ; ++ i ) { try { const res = await this . _coll . get ( this . _key ) ; if ( ! ( res . content instanceof Array ) ) { throw new CouchbaseError ( 'expected document of array type' ) ; } const itemIdx = res . content . indexOf ( item ) ; if ( itemIdx === - 1 ) { throw new Error ( 'item was not found in set' ) ; } await this . _coll . mutateIn ( this . _key , [ MutateInSpec . remove ( '[' + itemIdx + ']' ) ] , { cas : res . cas , } ) ; return ; } catch ( e ) { } } throw new CouchbaseError ( 'no items available to pop' ) ; } , callback ) ; } param callback async values ( callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { return await this . _get ( ) ; } , callback ) ; } param callback async size ( callback ? ) { return PromiseHelper . wrapAsync ( async ( ) => { const res = await this . _coll . lookupIn ( this . _key , [ LookupInSpec . count ( '' ) ] ) ; return res . content [ 0 ] . value ; } , callback ) ; } }