import { IObserverCallback , IItem , IGetItem , IInsertItem , IQueryInput , IInsertInput , IUnlisten , IInsert , IDebeUse } from './types' ; import { DebeDispatcher } from './dispatcher' ; import { ensureQuery } from './sanitize' ; export class Debe < TBase = IItem , TDispatcher = DebeDispatcher < TBase > > { dispatcher : TDispatcher & DebeDispatcher < TBase > ; initializing : Promise < void > ; isInitialized = false ; constructor ( dispatcher ) { this . dispatcher = dispatcher ; dispatcher . db = this as any ; } async close ( ) { await this . dispatcher . close ( ) ; } async initialize ( ) { if ( ! this . initializing ) { this . initializing = this . dispatcher . initialize ( ) ; } await this . initializing ; this . isInitialized = true ; return this ; } use < T = IItem > ( collection ) { const proxy = this ; return new Proxy < any > ( { } , { get : function ( t , methodName ) { return ( ... args ) => { return proxy [ methodName ] ( collection , ... args ) ; } ; } } ) ; } listen < T = IGetItem > ( collection , callback ) { return this . dispatcher . listen ( 'insert' , callback , { collection } ) ; } insert < T = IInsertItem > ( collection , value , options ? ) ; insert < T = IInsertItem > ( collection , value , options ? ) ; insert < T = IInsertItem > ( collection , value , optionsInput = { } ) { const options = { refetchResult : false , update : true , ... optionsInput } as any ; if ( Array . isArray ( value ) ) { return this . dispatcher . run < T & IGetItem > ( 'insert' , collection , value , options ) ; } return this . dispatcher . run < ( T & IGetItem ) [ ] > ( 'insert' , collection , [ value ] , options ) . then ( x => ( Array . isArray ( x ) ? x [ 0 ] : x ) ) ; } remove < T = any > ( collection , value ) { if ( Array . isArray ( value ) ) { return this . dispatcher . run ( 'remove' , collection , value ) ; } return this . dispatcher . run ( 'remove' , collection , [ value ] ) . then ( x => x && x [ 0 ] ) ; } all < T = TBase > ( collection ) ; all < T = TBase > ( collection , value ) ; all < T = TBase > ( collection , callback ) ; all < T = TBase > ( collection , value , callback ) ; all < T = TBase > ( collection , value ? , callback ? ) { if ( value && typeof value === 'function' ) { callback = value ; value = undefined ; } if ( value && typeof value === 'string' ) { value = { id : value } ; } else if ( value && Array . isArray ( value ) ) { value = { id : value } ; } else if ( value && typeof value === 'object' && Array . isArray ( value . id ) ) { value = { id : value . id } ; } const query = ensureQuery ( value ) ; if ( callback ) { return this . dispatcher . listen < T [ ] > ( 'all' , callback , { collection , query } ) ; } return this . dispatcher . run < T [ ] > ( 'all' , collection , query ) ; } count ( collection ) ; count ( collection , value ) ; count ( collection , callback ) ; count ( collection , value , callback ) ; count ( collection , value ? , callback ? ) { if ( value && typeof value === 'function' ) { callback = value ; value = undefined ; } if ( value && typeof value === 'string' ) { value = { id : value } ; } else if ( value && Array . isArray ( value ) ) { value = { id : value } ; } else if ( value && typeof value === 'object' && Array . isArray ( value . id ) ) { value = { id : value . id } ; } const query = ensureQuery ( value ) ; if ( callback ) { return this . dispatcher . listen < number > ( 'count' , callback , { collection , query } ) ; } return this . dispatcher . run < number > ( 'count' , collection , query ) ; } get < T = TBase > ( collection , args ? ) ; get < T = TBase > ( collection , value ? , callback ? ) ; get < T = TBase > ( collection , value ? , callback ? ) { if ( value && typeof value === 'function' ) { callback = value ; value = undefined ; } if ( value && typeof value === 'object' && value . id && ! Array . isArray ( value . id ) ) { value = value . id ; } if ( value && typeof value === 'string' ) { if ( callback ) { return this . dispatcher . listen < T > ( 'get' , callback , { collection , query : value } ) ; } return this . dispatcher . run < T & IGetItem > ( 'get' , collection , value ) ; } if ( value && Array . isArray ( value ) ) { value = { id : value } ; } else if ( value && typeof value === 'object' && Array . isArray ( value . id ) ) { value = { id : value . id } ; } const query = ensureQuery ( value ) ; if ( callback !== undefined ) { const cb = callback ; return this . dispatcher . listen < T [ ] > ( 'all' , ( error , value ) => cb ( error , error ? undefined : ( value [ 0 ] as any ) ) , { collection , query } ) ; } return this . dispatcher . run < ( T & IGetItem ) [ ] > ( 'all' , collection , query ) . then ( x => x && x [ 0 ] ) ; } }