/* tslint:disable: max-line-length */ import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { AngularFirestore} from '@angular/fire/firestore'; import { map, take } from 'rxjs/operators'; import { WhereFilterOp, Query, DocumentReference } from '@firebase/firestore-types'; @Injectable({ providedIn: 'root' }) export class FirestoreService { constructor( private angularFirestore: AngularFirestore ) { } getRecord(collection: string, uid: string): Promise { return this.angularFirestore.collection(collection).doc(uid).valueChanges().pipe(take(1)).toPromise(); } getRecordByProperty(collection: string, property: string, filterOp: WhereFilterOp = '==', value: any) { return this.angularFirestore.collection(collection, (collectionRef) => { return collectionRef.where(property, filterOp, value).limit(1); }).valueChanges().pipe(take(1), map(value => value[0])).toPromise(); } getRecordByProperties(collection: string, properties: string[], filterOp: WhereFilterOp | WhereFilterOp[] = '==', values: any[]): Promise { return this.angularFirestore.collection(collection, (collectionRef) => { let query: Query; properties.forEach((property, i) => { const op = Array.isArray(filterOp) ? filterOp[i] : filterOp; if (!query) query = collectionRef.where(property, op, values[i]); else query = query.where(property, op, values[i]); }); return query.limit(1); }).valueChanges().pipe(take(1), map(value => value[0])).toPromise(); } getRecordsByProperty(collection: string, property: string, filterOp: WhereFilterOp = '==', value: any): Promise { return this.angularFirestore.collection(collection, (collectionRef) => { return collectionRef.where(property, filterOp, value); }).valueChanges().pipe(take(1)).toPromise(); } getRecordsByProperties(collection: string, properties: string[], filterOp: WhereFilterOp | WhereFilterOp[] = '==', values: any[]): Promise { return this.angularFirestore.collection(collection, (collectionRef) => { let query: Query; properties.forEach((property, i) => { const op = Array.isArray(filterOp) ? filterOp[i] : filterOp; if (!query) query = collectionRef.where(property, op, values[i]); else query = query.where(property, op, values[i]); }); return query; }).valueChanges().pipe(take(1)).toPromise(); } addOrUpdateRecord(collection: string, record: any) { return new Promise((resolve, reject) => { if (record.uid) { return this.updateRecord(collection, record) .then((rec: any) => resolve(rec)) .catch(reject); } else { return this.addRecord(collection, record) .then((rec: any) => resolve(rec)) .catch(reject); } }); } removeRecord(collection: string, uid: string) { return this.angularFirestore.collection(collection).doc(uid).delete(); } observeRecord(collection: string, uid: string): Observable { return this.angularFirestore.collection(collection).doc(uid).valueChanges(); } observeRecordByProperty(collection: string, property: string, filterOp: WhereFilterOp = '==', value: any): Observable { return this.angularFirestore.collection(collection, (collectionRef) => { return collectionRef.where(property, filterOp, value).limit(1); }).valueChanges().pipe(map(value => value[0])); } observeRecordByProperties(collection: string, properties: string[], filterOp: WhereFilterOp | WhereFilterOp[] = '==', values: any[]): Observable { return this.angularFirestore.collection(collection, (collectionRef) => { let query: Query; properties.forEach((property, i) => { const op = Array.isArray(filterOp) ? filterOp[i] : filterOp; if (!query) query = collectionRef.where(property, op, values[i]); else query = query.where(property, op, values[i]); }); return query.limit(1); }).valueChanges().pipe(map(value => value[0])); } observeRecordsByProperty(collection: string, property: string, filterOp: WhereFilterOp = '==', value: any): Observable { return this.angularFirestore.collection(collection, (collectionRef) => { return collectionRef.where(property, filterOp, value); }).valueChanges(); } observeRecordsByProperties(collection: string, properties: string[], filterOp: WhereFilterOp | WhereFilterOp[] = '==', values: any[]): Observable { return this.angularFirestore.collection(collection, (collectionRef) => { let query: Query; properties.forEach((property, i) => { const op = Array.isArray(filterOp) ? filterOp[i] : filterOp; if (!query) query = collectionRef.where(property, op, values[i]); else query = query.where(property, op, values[i]); }); return query; }).valueChanges(); } observeCollection(collection: string): Observable { return this.angularFirestore.collection(collection).valueChanges(); } getCollection(collection: string): Promise { return this.angularFirestore.collection(collection).valueChanges().pipe(take(1)).toPromise(); } updateCollection(collection: string, newValue): Promise { return this.getCollection(collection).then(col => { col.forEach((record, it) => { this.addOrUpdateRecord(collection, newValue[it]); }); }); } private addRecord(collection, record): Promise { return new Promise((resolve, reject) => { this.angularFirestore.collection(collection).add(record) .then((r: DocumentReference) => { record.uid = r.id; this.updateRecord(collection, record).then(resolve).catch(reject); }); }); } private updateRecord(collection, record) { return new Promise((resolve, reject) => { this.angularFirestore.collection(collection).doc(record.uid).set({...record}, {merge: true}) .then(() => resolve(record)) .catch(reject); }); } }