import { Injectable } from '@angular/core' ; import { AngularFireAuth } from '@angular/fire/auth' ; import { AngularFirestore } from '@angular/fire/firestore' ; import { AngularFireStorage , AngularFireStorageReference } from '@angular/fire/storage' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { isArray } from 'util' ; import { DbService } from './../db.service' ; import * as firebase from 'firebase/app' ; import 'firebase/firestore' ; @ Injectable ( ) export class WebDbService extends DbService { constructor ( protected _afAuth , protected _afStore , private _afstorage ) { super ( ) ; } public createDoc ( collectionName , document ) { const collectionRef = this . _afStore . 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 = this . _afStore . collection ( collectionName ) ; return collectionRef . add ( document ) ; } public setDoc ( collectionName , docId , document , timeStamp = null ) { if ( timeStamp ) { if ( timeStamp . createdOn ) { document = { ... document , createdOn : firebase . firestore . FieldValue . serverTimestamp ( ) } ; } if ( timeStamp . updatedOn ) { document = { ... document , updatedOn : firebase . firestore . FieldValue . serverTimestamp ( ) } ; } } return this . _afStore . doc ( `/${ collectionName }/${ docId }` ) . set ( document , { merge : true } ) ; } public updateDoc ( collectionName , docId , document ) { return this . _afStore . doc ( `/${ collectionName }/${ docId }` ) . set ( document , { merge : true } ) ; } public valueChanges ( collectionName , path ? , queryParams ? ) { let query = this . _afStore . collection ( collectionName , ref => { let query = ref ; 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 ) ; } } return query ; } ) ; if ( path ) { return query . doc ( path ) . snapshotChanges ( ) . pipe ( map ( actions => { return actions . payload . data ( ) ; } ) ) ; } else { return query . snapshotChanges ( ) . pipe ( map ( actions => { if ( isArray ( actions ) ) { return actions . map ( a => { const data = a . payload . doc . data ( ) ; const id = a . payload . doc . id ; return { id , ... data } ; } ) ; } } ) ) ; } } public createId ( ) { return this . _afStore . createId ( ) ; } public getFireStorageReference ( filePath ) { return this . _afstorage . ref ( filePath ) ; } public getFireStore ( ) { return this . _afStore ; } public getDoc ( collectionName , docId ) { return this . _afStore . firestore . collection ( collectionName ) . doc ( docId ) ; } public upload ( filePath , imageBlob ) { return this . _afstorage . upload ( filePath , imageBlob ) ; } public deleteDoc ( collectionName , docId ) { return this . _afStore . firestore . collection ( collectionName ) . doc ( docId ) . delete ( ) ; } }