import { Injectable , NgZone } from '@angular/core' ; import { Store } from '@ngrx/store' ; import { Observable } from 'rxjs' ; import { DbService } from './../db.service' ; const firebaseApp = require ( 'nativescript-plugin-firebase/app' ) ; @ Injectable ( ) export class TNSDbService extends DbService { constructor ( private zone , protected _store ) { super ( ) ; } public createDoc ( collectionName , document ) { const collectionRef = firebaseApp . firestore ( ) . collection ( collectionName ) ; return collectionRef . add ( document ) . then ( ( documentRef ) => { document . id = documentRef . id ; return this . setDoc ( collectionName , documentRef . id , document ) ; } ) ; } public CreateDocWithoutDocID ( collectionName , document ) { const collectionRef = firebaseApp . firestore ( ) . collection ( collectionName ) ; return collectionRef . add ( document ) ; } public setDoc ( collectionName , docId , document , timeStamp = null ) { if ( timeStamp !== null && timeStamp ) { if ( timeStamp . createdOn ) { document = { ... document , createdOn : firebaseApp . firestore ( ) . FieldValue ( ) . serverTimestamp ( ) } ; } if ( timeStamp . updatedOn ) { document = { ... document , updatedOn : firebaseApp . firestore ( ) . FieldValue ( ) . serverTimestamp ( ) } ; } } const userCollection = firebaseApp . firestore ( ) . collection ( collectionName ) ; return userCollection . doc ( docId ) . set ( document , { merge : true } ) ; } public updateDoc ( collectionName , docId , document ) { const userCollection = firebaseApp . firestore ( ) . collection ( collectionName ) ; return userCollection . doc ( docId ) . set ( document , { merge : true } ) ; } public valueChanges ( collectionName , path ? , queryParams ? ) { let query = firebaseApp . firestore ( ) . collection ( collectionName ) ; if ( queryParams ) { for ( const param of queryParams . condition ) { query = query . where ( param . name , param . comparator , param . value ) ; } if ( queryParams . orderBy ) { for ( const param of queryParams . orderBy ) { query = query . orderBy ( param . name , param . value ) ; } } if ( queryParams . limit ) { query = query . limit ( queryParams . limit ) ; } } if ( path ) { query = query . doc ( path ) ; } return Observable . create ( observer => { const unsubscribe = query . onSnapshot ( ( snapshot ) => { let results = [ ] ; if ( snapshot && snapshot . forEach ) { snapshot . forEach ( doc => results . push ( { id : doc . id , ... doc . data ( ) } ) ) ; } else { results = snapshot . data ( ) ; } this . zone . run ( ( ) => { if ( results !== undefined ) { observer . next ( results ) ; } else { observer . next ( undefined ) ; } } ) ; } ) ; return ( ) => unsubscribe ( ) ; } ) ; } public createId ( ) { return firebaseApp . createId ; } public getFireStorageReference ( filePath ) { } public getFireStore ( ) { } public getStore ( ) { } public getDoc ( collectionName , docId ) { const collectionRef = firebaseApp . firestore ( ) . collection ( collectionName ) ; return collectionRef . doc ( docId ) ; } public upload ( filePath , imageBlob ) { } public deleteDoc ( collectionName , docId ) { const collectionRef = firebaseApp . firestore ( ) . collection ( collectionName ) ; return collectionRef . doc ( docId ) . delete ( ) ; } }