@nodeboot/starter-firebase v1.1.5
@nodeboot/starter-firebase Documentation
Overview
@nodeboot/starter-firebase seamlessly integrates Firebase services into your Node.js application using the Node-Boot framework. Drawing inspiration from Spring Boot's auto-configuration, this package simplifies Firebase setup by:
- Auto-configuring Firebase Services: Automatically initializes Firebase services based on your configuration.
- Dependency Injection (DI) Support: Provides ready-to-use Firebase service instances as beans in the DI container.
- Centralized Configuration: Reads settings from an
app-config.yamlfile, promoting an opinionated and consistent configuration approach.
Installation
Install the package via npm:
npm install @nodeboot/starter-firebaseConfiguration
To enable Firebase integration, add your Firebase settings to the app-config.yaml file under the integrations.firebase path.
Example app-config.yaml:
integrations:
firebase:
serviceAccount: "./path/to/firebase.service-account.json"
realtimeDatabaseUrl: "https://<your-database-name>.firebaseio.com"
storageBucket: "<your-storage-bucket>.appspot.com"
projectId: "<your-project-id>"Configuration Properties:
serviceAccount(string): Path to your Firebase service account JSON file. Learn more.realtimeDatabaseUrl(string): URL of your Firebase Realtime Database. Learn more.storageBucket(string): Name of your Google Cloud Storage bucket (withoutgs://prefix). Learn more.projectId(string): ID of your Google Cloud project. Learn more.
Enabling Firebase Integration
In your main application class, apply the @EnableFirebase decorator to activate Firebase auto-configuration:
import {EnableFirebase} from "@nodeboot/starter-firebase";
import {NodeBootApplication, NodeBootApp} from "@nodeboot/core";
import {EnableDI, EnableComponentScan} from "@nodeboot/core";
import {ExpressServer} from "@nodeboot/express";
import {Container} from "typedi";
@EnableDI(Container)
@EnableFirebase()
@EnableComponentScan()
@NodeBootApplication()
export class MyApp implements NodeBootApp {
start(): Promise<void> {
return NodeBoot.run(ExpressServer);
}
}Accessing Firebase Services
With auto-configuration enabled, you can inject Firebase services into your components using the DI container.
Example Service:
import {Service} from "@nodeboot/core";
import {FIREBASE_MACHINE_LEARNING_BEAN} from "@nodeboot/starter-firebase";
import {Inject} from "typedi";
import {Logger} from "winston";
import {remoteConfig} from "firebase-admin";
@Service()
export class FirebaseService {
constructor(
private readonly logger: Logger,
@Inject(FIREBASE_MACHINE_LEARNING_BEAN)
private readonly firebaseRemoteConfig: remoteConfig.RemoteConfig,
) {}
public async fetchRemoteConfigVersions() {
this.logger.info("Fetching Firebase Remote Config versions...");
try {
const result = await this.firebaseRemoteConfig.listVersions();
this.logger.info(`Retrieved ${result.versions.length} versions.`);
} catch (error) {
this.logger.error("Error fetching Remote Config versions:", error);
}
}
}Available Firebase Beans:
firebase.auth: Firebase Authenticationfirebase.firestore: Cloud Firestorefirebase.storage: Cloud Storagefirebase.realtime-database: Realtime Databasefirebase.messaging: Cloud Messaging (FCM)firebase.remote-config: Remote Configfirebase.app-check: App Checkfirebase.machine-learning: Machine Learning
Logging
The package utilizes a logger to provide informative messages during the initialization and injection of Firebase services.
If the serviceAccount path is not provided or incorrect, the initialization will log an error:
No configuration provided for Firebase integration. Please configure "integrations.firebase.serviceAccount=./path/to/firebase.service-account.json"Ensure that your app-config.yaml is correctly set up and the service account file is accessible.
Conclusion
@nodeboot/starter-firebase streamlines the integration of Firebase services into your Node.js application by leveraging Node-Boot's auto-configuration and DI capabilities. With minimal setup, you can access and utilize Firebase services efficiently.
For more detailed information on each Firebase service, refer to the official Firebase Documentation.
6 months ago
6 months ago
7 months ago
8 months ago
8 months ago
8 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago