import PouchDBAdapterMemory from 'pouchdb-adapter-memory' ; import PouchDB from 'pouchdb-browser' ; import PouchDBFind from 'pouchdb-find' ; PouchDB . plugin ( PouchDBAdapterMemory ) ; PouchDB . plugin ( PouchDBFind ) ; let POUCHDB_ADAPTER = 'idb' ; if ( typeof window === 'undefined' || window . indexedDB === undefined ) { POUCHDB_ADAPTER = 'memory' ; } export default class BaseModel < Type > { private _db : PouchDB . Database < Type > ; private _createIndexResponseList : Array < Promise < PouchDB . Find . CreateIndexResponse < Type > > > ; public constructor ( name , createIndexOptionsList ) { this . _db = new PouchDB < Type > ( name , { adapter : POUCHDB_ADAPTER } ) ; this . _createIndexResponseList = [ ] ; createIndexOptionsList . forEach ( options => { this . _createIndexResponseList . push ( this . createIndex ( options ) ) ; } ) ; } public get ( id ) { return this . _db . get ( id ) ; } public put ( doc ) { return this . _db . put ( doc ) ; } public post ( doc ) { return this . _db . post ( doc ) ; } public batchPost ( docs ) { return this . _db . bulkDocs ( docs ) ; } public batchGet ( options ) { return this . _db . bulkGet ( options ) ; } public all ( options ? ) { return this . _db . allDocs ( options ) ; } public createIndex ( options ) { return this . _db . createIndex ( options ) ; } public async beforeFind ( options ) { return Promise . all ( this . _createIndexResponseList ) ; } public async find ( options ) { await this . beforeFind ( options ) ; const result = this . _db . find ( options ) ; return result ; } }