migrate firebase db

This commit is contained in:
2026-03-09 21:50:49 +01:00
parent a569c070c5
commit b6c2fe1645
18 changed files with 143 additions and 62 deletions

View File

@@ -1,4 +1,4 @@
<div class="brand"> <div class="brand">
<app-logo></app-logo> <app-logo></app-logo>
<div class="copyright">© 2019 - 2024 - Benjamin Ifland</div> <div class="copyright">© 2019 - 2026 - Benjamin Ifland</div>
</div> </div>

View File

@@ -1,6 +1,5 @@
import firebase from 'firebase/compat/app';
import {Song} from '../songs/services/song'; import {Song} from '../songs/services/song';
import Timestamp = firebase.firestore.Timestamp; import {Timestamp} from '@angular/fire/firestore';
export interface GuestShow { export interface GuestShow {
id: string; id: string;

View File

@@ -1,5 +1,5 @@
<div class="fullscreen background"></div> <div class="fullscreen background"></div>
<div *ngIf="song && showType" [style.font-size.px]="zoom" class="fullscreen background"> <div *ngIf="showType" [style.font-size.px]="zoom" class="fullscreen background">
<div [class.visible]="presentationBackground==='blue'" class="bg-blue fullscreen bg-image"></div> <div [class.visible]="presentationBackground==='blue'" class="bg-blue fullscreen bg-image"></div>
<div [class.visible]="presentationBackground==='green'" class="bg-green fullscreen bg-image"></div> <div [class.visible]="presentationBackground==='green'" class="bg-green fullscreen bg-image"></div>
@@ -29,7 +29,7 @@
</div> </div>
<app-song-text <app-song-text
*ngIf="songId !== 'title' && songId !== 'empty'" *ngIf="song && songId !== 'title' && songId !== 'empty' && songId !== 'dynamicText'"
[@songSwitch]="songId" [@songSwitch]="songId"
[fullscreen]="true" [fullscreen]="true"
[header]="song.title" [header]="song.title"
@@ -40,7 +40,7 @@
chordMode="hide" chordMode="hide"
></app-song-text> ></app-song-text>
<app-legal <app-legal
*ngIf="songId !== 'title' && songId !== 'empty' && songId !== 'dynamicText'" *ngIf="song && songId !== 'title' && songId !== 'empty' && songId !== 'dynamicText'"
[@songSwitch]="songId" [@songSwitch]="songId"
[config]="config$ | async" [config]="config$ | async"
[song]="song" [song]="song"

View File

@@ -1,5 +1,5 @@
import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core'; import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
import {debounceTime, distinctUntilChanged, filter, map, shareReplay, switchMap, takeUntil, tap} from 'rxjs/operators'; import {distinctUntilChanged, filter, map, shareReplay, switchMap, takeUntil, tap} from 'rxjs/operators';
import {ShowService} from '../../shows/services/show.service'; import {ShowService} from '../../shows/services/show.service';
import {Song} from '../../songs/services/song'; import {Song} from '../../songs/services/song';
import {GlobalSettingsService} from '../../../services/global-settings.service'; import {GlobalSettingsService} from '../../../services/global-settings.service';
@@ -52,10 +52,9 @@ export class MonitorComponent implements OnInit, OnDestroy {
openFullscreen(); openFullscreen();
const currentShowId$ = this.globalSettingsService.get$ const currentShowId$ = this.globalSettingsService.get$
.pipe( .pipe(
debounceTime(100), filter((settings): settings is NonNullable<typeof settings> => !!settings),
filter(_ => !!_), map(settings => settings.currentShow),
map(_ => _), filter((showId): showId is string => !!showId),
map(_ => _.currentShow),
distinctUntilChanged(), distinctUntilChanged(),
tap(_ => (this.currentShowId = _)), tap(_ => (this.currentShowId = _)),
takeUntil(this.destroy$) takeUntil(this.destroy$)
@@ -92,6 +91,9 @@ export class MonitorComponent implements OnInit, OnDestroy {
map(show => ({showId: show.id, presentationSongId: show.presentationSongId})), map(show => ({showId: show.id, presentationSongId: show.presentationSongId})),
distinctUntilChanged((a, b) => a.showId === b.showId && a.presentationSongId === b.presentationSongId), distinctUntilChanged((a, b) => a.showId === b.showId && a.presentationSongId === b.presentationSongId),
tap(({presentationSongId}) => { tap(({presentationSongId}) => {
if (presentationSongId === 'title' || presentationSongId === 'dynamicText' || !presentationSongId) {
this.song = null;
}
if (this.songId !== presentationSongId) { if (this.songId !== presentationSongId) {
this.songId = 'empty'; this.songId = 'empty';
} }

View File

@@ -8,7 +8,6 @@
margin-bottom: 10px; margin-bottom: 10px;
box-sizing: border-box; box-sizing: border-box;
color: var(--text); color: var(--text);
border: 1px solid var(--surface-border);
@media screen and (max-width: 860px) { @media screen and (max-width: 860px) {
width: 100vw; width: 100vw;
@@ -46,7 +45,7 @@
overflow: hidden; overflow: hidden;
transition: var(--transition); transition: var(--transition);
cursor: pointer; cursor: pointer;
outline: 1px solid var(--divider); outline: 1px solid var(--surface-muted);
&:hover { &:hover {
outline: 1px solid var(--primary-hover); outline: 1px solid var(--primary-hover);

View File

@@ -7,7 +7,7 @@ import {FormControl, FormGroup, ReactiveFormsModule, Validators} from '@angular/
import {ActivatedRoute, Router} from '@angular/router'; import {ActivatedRoute, Router} from '@angular/router';
import {faSave} from '@fortawesome/free-solid-svg-icons'; import {faSave} from '@fortawesome/free-solid-svg-icons';
import {map, switchMap} from 'rxjs/operators'; import {map, switchMap} from 'rxjs/operators';
import firebase from 'firebase/compat/app'; import {Timestamp} from '@angular/fire/firestore';
import {CardComponent} from '../../../widget-modules/components/card/card.component'; import {CardComponent} from '../../../widget-modules/components/card/card.component';
import {MatFormField, MatLabel, MatSuffix} from '@angular/material/form-field'; import {MatFormField, MatLabel, MatSuffix} from '@angular/material/form-field';
import {MatSelect} from '@angular/material/select'; import {MatSelect} from '@angular/material/select';
@@ -18,7 +18,6 @@ import {MatDatepicker, MatDatepickerInput, MatDatepickerToggle} from '@angular/m
import {ButtonRowComponent} from '../../../widget-modules/components/button-row/button-row.component'; import {ButtonRowComponent} from '../../../widget-modules/components/button-row/button-row.component';
import {ButtonComponent} from '../../../widget-modules/components/button/button.component'; import {ButtonComponent} from '../../../widget-modules/components/button/button.component';
import {ShowTypePipe} from '../../../widget-modules/pipes/show-type-translater/show-type.pipe'; import {ShowTypePipe} from '../../../widget-modules/pipes/show-type-translater/show-type.pipe';
import Timestamp = firebase.firestore.Timestamp;
@Component({ @Component({
selector: 'app-edit', selector: 'app-edit',

View File

@@ -3,8 +3,7 @@ import {Observable} from 'rxjs';
import {DbService} from '../../../services/db.service'; import {DbService} from '../../../services/db.service';
import {Show} from './show'; import {Show} from './show';
import {map, shareReplay} from 'rxjs/operators'; import {map, shareReplay} from 'rxjs/operators';
import {QueryFn} from '@angular/fire/compat/firestore/interfaces'; import {orderBy, QueryConstraint, Timestamp, where} from '@angular/fire/firestore';
import firebase from 'firebase/compat/app';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@@ -28,11 +27,11 @@ export class ShowDataService {
const startDate = new Date(); const startDate = new Date();
startDate.setHours(0, 0, 0, 0); startDate.setHours(0, 0, 0, 0);
startDate.setDate(startDate.getDate() - lastMonths * 30); startDate.setDate(startDate.getDate() - lastMonths * 30);
const startTimestamp = firebase.firestore.Timestamp.fromDate(startDate); const startTimestamp = Timestamp.fromDate(startDate);
const queryFn: QueryFn = ref => ref.where('published', '==', true).where('date', '>=', startTimestamp).orderBy('date', 'desc'); const queryConstraints: QueryConstraint[] = [where('published', '==', true), where('date', '>=', startTimestamp), orderBy('date', 'desc')];
return this.dbService.col$<Show>(this.collection, queryFn).pipe( return this.dbService.col$<Show>(this.collection, queryConstraints).pipe(
map(shows => shows.filter(show => !show.archived)), map(shows => shows.filter(show => !show.archived)),
shareReplay({ shareReplay({
bufferSize: 1, bufferSize: 1,

View File

@@ -2,7 +2,7 @@ import {Injectable} from '@angular/core';
import {DbService} from '../../../services/db.service'; import {DbService} from '../../../services/db.service';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import {ShowSong} from './show-song'; import {ShowSong} from './show-song';
import {QueryFn} from '@angular/fire/compat/firestore/interfaces'; import {QueryConstraint} from '@angular/fire/firestore';
import {shareReplay} from 'rxjs/operators'; import {shareReplay} from 'rxjs/operators';
@Injectable({ @Injectable({
@@ -15,9 +15,9 @@ export class ShowSongDataService {
public constructor(private dbService: DbService) {} public constructor(private dbService: DbService) {}
public list$ = (showId: string, queryFn?: QueryFn): Observable<ShowSong[]> => { public list$ = (showId: string, queryConstraints?: QueryConstraint[]): Observable<ShowSong[]> => {
if (queryFn) { if (queryConstraints && queryConstraints.length > 0) {
return this.dbService.col$(`${this.collection}/${showId}/${this.subCollection}`, queryFn); return this.dbService.col$(`${this.collection}/${showId}/${this.subCollection}`, queryConstraints);
} }
const cached = this.listCache.get(showId); const cached = this.listCache.get(showId);

View File

@@ -1,5 +1,4 @@
import firebase from 'firebase/compat/app'; import {Timestamp} from '@angular/fire/firestore';
import Timestamp = firebase.firestore.Timestamp;
export type PresentationBackground = 'none' | 'blue' | 'green' | 'leder' | 'praise' | 'bible'; export type PresentationBackground = 'none' | 'blue' | 'green' | 'leder' | 'praise' | 'bible';

View File

@@ -1,24 +1,20 @@
import {TestBed} from '@angular/core/testing'; import {TestBed} from '@angular/core/testing';
import {SongDataService} from './song-data.service'; import {SongDataService} from './song-data.service';
import {AngularFirestore} from '@angular/fire/compat/firestore';
import {firstValueFrom, of} from 'rxjs'; import {firstValueFrom, of} from 'rxjs';
import {DbService} from '../../../services/db.service';
describe('SongDataService', () => { describe('SongDataService', () => {
const songs = [{title: 'title1'}]; const songs = [{title: 'title1'}];
const angularFirestoreCollection = { const mockDbService = {
valueChanges: () => of(songs), col$: () => of(songs),
};
const mockAngularFirestore = {
collection: () => angularFirestoreCollection,
}; };
beforeEach( beforeEach(
() => () =>
void TestBed.configureTestingModule({ void TestBed.configureTestingModule({
providers: [{provide: AngularFirestore, useValue: mockAngularFirestore}], providers: [{provide: DbService, useValue: mockDbService}],
}) })
); );

View File

@@ -3,8 +3,7 @@ import {firstValueFrom, Observable} from 'rxjs';
import {Song} from './song'; import {Song} from './song';
import {SongDataService} from './song-data.service'; import {SongDataService} from './song-data.service';
import {UserService} from '../../../services/user/user.service'; import {UserService} from '../../../services/user/user.service';
import firebase from 'firebase/compat/app'; import {Timestamp} from '@angular/fire/firestore';
import Timestamp = firebase.firestore.Timestamp;
// declare let importCCLI: any; // declare let importCCLI: any;

View File

@@ -1,6 +1,5 @@
import firebase from 'firebase/compat/app';
import {SongLegalOwner, SongLegalType, SongStatus, SongType} from './song.service'; import {SongLegalOwner, SongLegalType, SongStatus, SongType} from './song.service';
import Timestamp = firebase.firestore.Timestamp; import {Timestamp} from '@angular/fire/firestore';
export interface Song { export interface Song {
id: string; id: string;

View File

@@ -1,9 +1,15 @@
import {TestBed} from '@angular/core/testing'; import {TestBed} from '@angular/core/testing';
import {Firestore} from '@angular/fire/firestore';
import {DbService} from './db.service'; import {DbService} from './db.service';
describe('DbService', () => { describe('DbService', () => {
beforeEach(() => void TestBed.configureTestingModule({})); beforeEach(
() =>
void TestBed.configureTestingModule({
providers: [{provide: Firestore, useValue: {}}],
})
);
it('should be created', () => { it('should be created', () => {
const service: DbService = TestBed.inject(DbService); const service: DbService = TestBed.inject(DbService);

View File

@@ -1,24 +1,89 @@
import {Injectable} from '@angular/core'; 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 {Observable} from 'rxjs';
import {QueryFn} from '@angular/fire/compat/firestore/interfaces';
import {map} from 'rxjs/operators'; import {map} from 'rxjs/operators';
type CollectionPredicate<T> = string | AngularFirestoreCollection<T>; type CollectionPredicate<T> = string | DbCollection<T>;
type DocumentPredicate<T> = string | AngularFirestoreDocument<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({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class DbService { export class DbService {
public constructor(private afs: AngularFirestore) {} public constructor(private fs: Firestore) {}
public col<T>(ref: CollectionPredicate<T>, queryFn?: QueryFn): AngularFirestoreCollection<T> { public col<T>(ref: CollectionPredicate<T>): DbCollection<T> {
return typeof ref === 'string' ? this.afs.collection<T>(ref, queryFn) : ref; return typeof ref === 'string' ? new DbCollection<T>(this.fs, ref) : ref;
} }
public doc<T>(ref: DocumentPredicate<T>): AngularFirestoreDocument<T> { public doc<T>(ref: DocumentPredicate<T>): DbDocument<T> {
return typeof ref === 'string' ? this.afs.doc<T>(ref) : ref; return typeof ref === 'string' ? new DbDocument<T>(this.fs, ref) : ref;
} }
public doc$<T>(ref: DocumentPredicate<T>): Observable<(NonNullable<T> & {id?: string}) | null> { public doc$<T>(ref: DocumentPredicate<T>): Observable<(NonNullable<T> & {id?: string}) | null> {
@@ -27,7 +92,12 @@ export class DbService {
.pipe(map(_ => (_ ? _ : null))); .pipe(map(_ => (_ ? _ : null)));
} }
public col$<T>(ref: CollectionPredicate<T>, queryFn?: QueryFn): Observable<T[]> { public col$<T>(ref: CollectionPredicate<T>, queryConstraints: QueryConstraint[] = []): Observable<T[]> {
return this.col(ref, queryFn).valueChanges({idField: 'id'}); 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[]>;
} }
} }

View File

@@ -1,13 +1,29 @@
const elem = document.documentElement; const elem = document.documentElement;
export const openFullscreen = () => { export const openFullscreen = () => {
if (elem.requestFullscreen) { if (!elem.requestFullscreen || !document.fullscreenEnabled) {
void elem.requestFullscreen(); return;
}
try {
const promise = elem.requestFullscreen();
if (promise && typeof promise.catch === 'function') {
void promise.catch(() => {
// Browser may reject when no user gesture is present. Keep app usable.
});
}
} catch {
// Some browsers may throw synchronously if fullscreen is not allowed.
} }
}; };
export const closeFullscreen = () => { export const closeFullscreen = () => {
if (document.exitFullscreen) { if (document.exitFullscreen) {
void document.exitFullscreen(); const promise = document.exitFullscreen();
if (promise && typeof promise.catch === 'function') {
void promise.catch(() => {
// Ignore; leaving fullscreen is a best-effort action.
});
}
} }
}; };

View File

@@ -8,7 +8,7 @@ import {environment} from '../../../environments/environment';
import {Router} from '@angular/router'; import {Router} from '@angular/router';
import {ShowDataService} from '../../modules/shows/services/show-data.service'; import {ShowDataService} from '../../modules/shows/services/show-data.service';
import {ShowSongDataService} from '../../modules/shows/services/show-song-data.service'; import {ShowSongDataService} from '../../modules/shows/services/show-song-data.service';
import firebase from 'firebase/compat/app'; import {increment} from '@angular/fire/firestore';
export interface SongUsageMigrationResult { export interface SongUsageMigrationResult {
usersProcessed: number; usersProcessed: number;
@@ -159,7 +159,7 @@ export class UserService {
if (!user) return null; if (!user) return null;
await this.db.doc<User>('users/' + user.id).update({ await this.db.doc<User>('users/' + user.id).update({
[`songUsage.${songId}`]: firebase.firestore.FieldValue.increment(direction), [`songUsage.${songId}`]: increment(direction),
}); });
} }

View File

@@ -5,12 +5,12 @@
border-radius: 8px; border-radius: 8px;
background: var(--surface); background: var(--surface);
backdrop-filter: blur(15px); backdrop-filter: blur(15px);
border: 1px solid var(--surface-border); border: none;
overflow: hidden; overflow: hidden;
width: 800px; width: 800px;
position: relative; position: relative;
color: var(--text); color: var(--text);
padding-bottom: 5px; padding: 5px 0;
@media screen and (max-width: 860px) { @media screen and (max-width: 860px) {
width: 100vw; width: 100vw;

View File

@@ -7,12 +7,11 @@ import {provideAnimations} from '@angular/platform-browser/animations';
import {AppRoutingModule} from './app/app-routing.module'; import {AppRoutingModule} from './app/app-routing.module';
import {ServiceWorkerModule} from '@angular/service-worker'; import {ServiceWorkerModule} from '@angular/service-worker';
import {AngularFireModule} from '@angular/fire/compat'; import {AngularFireModule} from '@angular/fire/compat';
import {AngularFirestoreModule} from '@angular/fire/compat/firestore';
import {AngularFireStorageModule} from '@angular/fire/compat/storage'; import {AngularFireStorageModule} from '@angular/fire/compat/storage';
import {FontAwesomeModule} from '@fortawesome/angular-fontawesome'; import {FontAwesomeModule} from '@fortawesome/angular-fontawesome';
import {AppComponent} from './app/app.component'; import {AppComponent} from './app/app.component';
import {provideFirebaseApp, initializeApp} from '@angular/fire/app'; import {getApp, initializeApp, provideFirebaseApp} from '@angular/fire/app';
import {provideFirestore, getFirestore} from '@angular/fire/firestore'; import {initializeFirestore, persistentLocalCache, persistentMultipleTabManager, provideFirestore} from '@angular/fire/firestore';
import {getAuth, provideAuth} from '@angular/fire/auth'; import {getAuth, provideAuth} from '@angular/fire/auth';
import {UserService} from './app/services/user/user.service'; import {UserService} from './app/services/user/user.service';
@@ -37,13 +36,12 @@ bootstrapApplication(AppComponent, {
enabled: environment.production, enabled: environment.production,
}), }),
AngularFireModule.initializeApp(environment.firebase), AngularFireModule.initializeApp(environment.firebase),
AngularFirestoreModule.enablePersistence({synchronizeTabs: true}),
AngularFireStorageModule, AngularFireStorageModule,
FontAwesomeModule FontAwesomeModule
), ),
provideFirebaseApp(() => initializeApp(environment.firebase)), provideFirebaseApp(() => initializeApp(environment.firebase)),
provideAuth(() => getAuth()), provideAuth(() => getAuth()),
provideFirestore(() => getFirestore()), provideFirestore(() => initializeFirestore(getApp(), {localCache: persistentLocalCache({tabManager: persistentMultipleTabManager()})})),
{provide: MAT_DATE_LOCALE, useValue: 'de-DE'}, {provide: MAT_DATE_LOCALE, useValue: 'de-DE'},
provideAnimations(), provideAnimations(),
], ],