import { Injectable } from '@angular/core' ; import { Platform } from 'ionic-angular' ; import { Observable } from 'rxjs/rx' ; import { Birthday } from '../models/birthday' ; import * as PouchDB from 'pouchdb' ; @ Injectable ( ) export class BirthdayService { private db ; constructor ( private platform ) { } initDB ( ) { return this . platform . ready ( ) . then ( ( ) => { this . db = new PouchDB ( 'birthday' , { adapter : 'websql' } ) ; } ) ; } add ( birthday ) { return this . db . post ( birthday ) ; } update ( birthday ) { return this . db . put ( birthday ) ; } delete ( birthday ) { return this . db . remove ( birthday ) ; } getAll ( ) { return Observable . fromPromise ( this . initDB ( ) . then ( ( ) => { return this . db . allDocs ( { include_docs : true } ) ; } ) . then ( docs => { return docs . rows . map ( row => { row . doc . Date = new Date ( row . doc . Date ) ; return row . doc ; } ) ; } ) ) ; } getChanges ( ) { return Observable . create ( observer => { this . db . changes ( { live : true , since : 'now' , include_docs : true } ) . on ( 'change' , change => { change . doc . Date = new Date ( change . doc . Date ) ; observer . next ( change . doc ) ; } ) ; } ) ; } }