migrate firebase db
This commit is contained in:
@@ -1,24 +1,89 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument} from '@angular/fire/compat/firestore';
|
||||
import {
|
||||
addDoc,
|
||||
collection,
|
||||
collectionData,
|
||||
CollectionReference,
|
||||
deleteDoc,
|
||||
doc,
|
||||
docData,
|
||||
DocumentData,
|
||||
DocumentReference,
|
||||
Firestore,
|
||||
query,
|
||||
QueryConstraint,
|
||||
setDoc,
|
||||
updateDoc,
|
||||
WithFieldValue,
|
||||
} from '@angular/fire/firestore';
|
||||
import {Observable} from 'rxjs';
|
||||
import {QueryFn} from '@angular/fire/compat/firestore/interfaces';
|
||||
import {map} from 'rxjs/operators';
|
||||
|
||||
type CollectionPredicate<T> = string | AngularFirestoreCollection<T>;
|
||||
type DocumentPredicate<T> = string | AngularFirestoreDocument<T>;
|
||||
type CollectionPredicate<T> = string | DbCollection<T>;
|
||||
type DocumentPredicate<T> = string | DbDocument<T>;
|
||||
|
||||
export class DbCollection<T> {
|
||||
public constructor(
|
||||
private readonly fs: Firestore,
|
||||
private readonly path: string
|
||||
) {}
|
||||
|
||||
public add(data: Partial<T>): Promise<DocumentReference<T>> {
|
||||
return addDoc(this.ref as CollectionReference<T>, data as WithFieldValue<T>);
|
||||
}
|
||||
|
||||
public valueChanges(options?: {idField?: string}): Observable<T[]> {
|
||||
return collectionData(this.ref, options as {idField?: never}) as Observable<T[]>;
|
||||
}
|
||||
|
||||
private get ref(): CollectionReference<DocumentData> {
|
||||
return collection(this.fs, this.path);
|
||||
}
|
||||
}
|
||||
|
||||
export class DbDocument<T> {
|
||||
public constructor(
|
||||
private readonly fs: Firestore,
|
||||
private readonly path: string
|
||||
) {}
|
||||
|
||||
public set(data: Partial<T>): Promise<void> {
|
||||
return setDoc(this.ref as DocumentReference<T>, data as WithFieldValue<T>);
|
||||
}
|
||||
|
||||
public update(data: Partial<T>): Promise<void> {
|
||||
return updateDoc(this.ref, data as Partial<DocumentData>);
|
||||
}
|
||||
|
||||
public delete(): Promise<void> {
|
||||
return deleteDoc(this.ref);
|
||||
}
|
||||
|
||||
public collection<U>(subPath: string): DbCollection<U> {
|
||||
return new DbCollection<U>(this.fs, `${this.path}/${subPath}`);
|
||||
}
|
||||
|
||||
public valueChanges(options?: {idField?: string}): Observable<(NonNullable<T> & {id?: string}) | undefined> {
|
||||
return docData(this.ref as DocumentReference<T>, options as {idField?: never}) as Observable<(NonNullable<T> & {id?: string}) | undefined>;
|
||||
}
|
||||
|
||||
private get ref(): DocumentReference<DocumentData> {
|
||||
return doc(this.fs, this.path);
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class DbService {
|
||||
public constructor(private afs: AngularFirestore) {}
|
||||
public constructor(private fs: Firestore) {}
|
||||
|
||||
public col<T>(ref: CollectionPredicate<T>, queryFn?: QueryFn): AngularFirestoreCollection<T> {
|
||||
return typeof ref === 'string' ? this.afs.collection<T>(ref, queryFn) : ref;
|
||||
public col<T>(ref: CollectionPredicate<T>): DbCollection<T> {
|
||||
return typeof ref === 'string' ? new DbCollection<T>(this.fs, ref) : ref;
|
||||
}
|
||||
|
||||
public doc<T>(ref: DocumentPredicate<T>): AngularFirestoreDocument<T> {
|
||||
return typeof ref === 'string' ? this.afs.doc<T>(ref) : ref;
|
||||
public doc<T>(ref: DocumentPredicate<T>): DbDocument<T> {
|
||||
return typeof ref === 'string' ? new DbDocument<T>(this.fs, ref) : ref;
|
||||
}
|
||||
|
||||
public doc$<T>(ref: DocumentPredicate<T>): Observable<(NonNullable<T> & {id?: string}) | null> {
|
||||
@@ -27,7 +92,12 @@ export class DbService {
|
||||
.pipe(map(_ => (_ ? _ : null)));
|
||||
}
|
||||
|
||||
public col$<T>(ref: CollectionPredicate<T>, queryFn?: QueryFn): Observable<T[]> {
|
||||
return this.col(ref, queryFn).valueChanges({idField: 'id'});
|
||||
public col$<T>(ref: CollectionPredicate<T>, queryConstraints: QueryConstraint[] = []): Observable<T[]> {
|
||||
if (typeof ref !== 'string' || queryConstraints.length === 0) {
|
||||
return this.col(ref).valueChanges({idField: 'id'});
|
||||
}
|
||||
|
||||
const q = query(collection(this.fs, ref), ...queryConstraints);
|
||||
return collectionData(q, {idField: 'id'}) as Observable<T[]>;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user