This class manages all access to the firebase Database. The class has no knowledge of the structure of the database.

Constructors

  • The constructor takes an optional parameter of type FirebaseConfig. If the parameter is not present the FirebaseConfig is imported from firebase-config.ts.

    Parameters

    • OptionalfirebaseConfigParam: any

    Returns FirebaseService

Properties

app: FirebaseApp
database: Database

Methods

  • Fetches data from the specified path in the Firebase Realtime Database.

    Type Parameters

    • T

      The expected class of the data to be fetched.

    Parameters

    • path: string

      The path in the database from which to fetch data.

    Returns Promise<null | T>

    A promise that resolves with the data of type T if available, or null if no data exists at the specified path or if an error occurs.

    Will log an error message to the console if an error occurs while fetching data from Firebase.

    const user = await firebaseService.get<User>('users/123');
    if (user) {
    console.log('User data:', user);
    } else {
    console.log('No user data found.');
    }
  • Parameters

    • path: string
    • propertyName: string

    Returns Promise<string[]>

  • Listens for changes at the specified path in the Firebase Realtime Database.

    Parameters

    • path: string

      The path in the database to listen for changes.

    • callback: ((key: string, data: any) => void)

      The callback function to be called with the results when a change occurs.

        • (key, data): void
        • Parameters

          • key: string
          • data: any

          Returns void

    Returns void

    firebaseService.listenForChanges('users/123', (data) => {
    console.log('Data changed:', data);
    });
  • Parameters

    • path: string
    • callback: ((key: string, data: any) => void)
        • (key, data): void
        • Parameters

          • key: string
          • data: any

          Returns void

    Returns void

  • Parameters

    • path: string
    • callback: ((key: string) => void)
        • (key): void
        • Parameters

          • key: string

          Returns void

    Returns void

  • Removes data at the specified path in the Firebase Realtime Database.

    Parameters

    • path: string

      The path in the database from which to remove data.

    Returns Promise<void>

    A promise that resolves when the data has been successfully removed.

    await firebaseService.remove('users/123');
    
  • Sets data at the specified path in the Firebase Realtime Database.

    Parameters

    • obj: any

      The data to be set at the specified path.

    • path: string

      The path in the database where the data should be set.

    Returns Promise<void>

    A promise that resolves when the data has been successfully set.

    await firebaseService.set({ name: 'John Doe' }, 'users/123');
    
  • Parameters

    • exerciseId: string
    • playerName: string

    Returns void