import { IGetItem , IQuery , ICollection , IInsert , ICollections , Debe , ICollectionInput } from 'debe' ; import { DebeAdapter , createMemoryFilter , sortArray , pluck , DebeBackend } from 'debe-adapter' ; export class MemoryDebe extends Debe { constructor ( collections , options ? , initialData = { } ) { super ( new DebeBackend ( new MemoryAdapter ( initialData ) , collections , options ) ) ; } } interface IStore { [ s ] : Map < string , IGetItem > ; } interface IInitialData { [ s ] : IGetItem [ ] ; } export class MemoryAdapter extends DebeAdapter { private store : IStore = { } ; filter = createMemoryFilter ( ) . filter ; initialData : IInitialData ; constructor ( initialData = { } ) { super ( ) ; this . initialData = initialData ; } initialize ( collections ) { for ( var key in collections ) { const collection = collections [ key ] ; this . store [ collection . name ] = new Map ( ) ; if ( this . initialData [ collection . name ] ) { this . initialData [ collection . name ] . forEach ( item => { this . store [ collection . name ] . set ( item . id , item ) ; } ) ; } } } get ( collection , id ) { const item = this . store [ collection . name ] . get ( id ) ; return item ? { ... item } : item ; } close ( ) { this . store = { } ; } all ( collection , query ) { let items = Array . from ( this . store [ collection . name ] . values ( ) ) ; if ( query . where ) { items = items . filter ( this . filter ( query . where ) ) ; } if ( query . orderBy ) { items = sortArray ( items , query . orderBy ) ; } if ( query . offset ) { items = items . slice ( query . offset ) ; } if ( query . limit ) { items = items . slice ( 0 , query . limit ) ; } if ( query . select ) { items = items . map ( x => pluck ( x , query . select ) ) ; } return [ ... items . map ( item => ( { ... item } ) ) ] ; } count ( collection , query ) { delete query . orderBy ; const { length } = this . all ( collection , query ) ; return length ; } insert ( collection , items , options ) { for ( var item of items ) { const update = options . existing . indexOf ( item . id ) !== - 1 && options . update ; this . handle ( collection . name , item , update ) ; } return items ; } remove ( collection , ids ) { for ( var id of ids ) { this . store [ collection . name ] . delete ( id ) ; } return ids ; } private handle ( type , item , update ) { if ( update && this . store [ type ] . has ( item . id ) ) { this . store [ type ] . set ( item . id , { ... this . store [ type ] . get ( item . id ) , ... item } ) ; } else { this . store [ type ] . set ( item . id , { ... item } ) ; } return item ; } }