@nestjs-add-ons/firebase-admin
v0.4.0
Published
NestJS firebase admin module
Downloads
428
Maintainers
Readme
🧾 Description
This package implements the NestJS module for Firebase admin
👨🏻💻 Installation
$ npm install -E @nestjs-add-ons/firebase-admin firebase-admin
▶️ Usage
Enable shutdown hooks in bootstrap function to take care of resource release
async function bootstrap(): Promise<void> {
const app = await NestFactory.create(AppModule);
app.enableShutdownHooks();
}
bootstrap();
Just create module in import
/* app.module.ts */
import { Module } from '@nestjs/common';
import { FirebaseAdminCoreModule } from '@nestjs-add-ons/firebase-admin';
import { MyService } from './my-service';
@Module({
imports: [
FirebaseAdminCoreModule.forRootAsync({
useFactory: () => ({
// ...setup options
}),
}),
],
providers: [MyService]
})
export class AppModule {}
In order for providers to be available, you need to inject them via FirebaseAdminCoreModule.forFeature()
function or
set isGlobal: true
in dynamic module options
/* my-test.module.ts */
import { Module } from '@nestjs/common';
import { FirebaseAdminCoreModule } from '@nestjs-add-ons/firebase-admin';
import { MyService } from './my-service';
@Module({
imports: [
FirebaseAdminCoreModule.forFeature(),
// or FirebaseAdminCoreModule.forFeature(['app_1', 'app_2']) if firebase app is presented
],
providers: [MyService]
})
export class MyTestModule {}
In the service, you can inject firebase application via the InjectFirebaseAdminApp
decorator
/* my-service.ts */
import { Injectable } from '@nestjs/common';
import { App } from 'firebase-admin/app';
import { InjectFirebaseAdminApp } from '@nestjs-add-ons/firebase-admin';
@Injectable()
export class MyService {
constructor(
@InjectFirebaseAdminApp()
private readonly firebaseApp: App
) {
}
public async sendPushNotification(): Promise<void> {
await getMessaging(this.firebaseApp).send({
// notification data
})
}
}
You can also create multiply firebase applications
/* app.module.ts */
import { Module } from '@nestjs/common';
import { FirebaseAdminCoreModule } from '@nestjs-add-ons/firebase-admin';
import { MyServiceForApp1 } from './my-service-for-app1';
import { MyServiceForApp2 } from './my-service-for-app2';
@Module({
imports: [
FirebaseAdminCoreModule.forRootAsync({
appName: 'app1',
useFactory: () => ({
// ...setup options
}),
}),
FirebaseAdminCoreModule.forRootAsync({
appName: 'app2',
useFactory: () => ({
// ...setup options
}),
}),
],
providers: [MyServiceForApp1, MyServiceForApp2]
})
export class AppModule {}
/* my-service-for-app1.ts */
import { Injectable } from '@nestjs/common';
import { App } from 'firebase-admin/app';
import { InjectFirebaseAdminApp } from '@nestjs-add-ons/firebase-admin';
@Injectable()
export class MyServiceForApp1 {
constructor(
@InjectFirebaseAdminApp('app1')
private readonly firebaseApp: App
) {
}
public async sendPushNotification(): Promise<void> {
await getMessaging(this.firebaseApp).send({
// notification data
})
}
}
/* my-service-for-app2.ts */
import { Injectable } from '@nestjs/common';
import { App } from 'firebase-admin/app';
import { InjectFirebaseAdminApp } from '@nestjs-add-ons/firebase-admin';
@Injectable()
export class MyServiceForApp2 {
constructor(
@InjectFirebaseAdminApp('app2')
private readonly firebaseApp: App
) {
}
public async sendPushNotification(): Promise<void> {
await getMessaging(this.firebaseApp).send({
// notification data
})
}
}