From 835eb270f5826d1d26d9c6aafc66ab097d9678e4 Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Fri, 13 Nov 2020 18:29:11 +0530 Subject: [PATCH] :sparkles: Add Firebase module --- src/app.module.ts | 2 ++ src/config/configuration.interface.ts | 7 +++++ src/config/configuration.ts | 10 ++++++ src/providers/firebase/firebase.module.ts | 10 ++++++ src/providers/firebase/firebase.service.ts | 36 ++++++++++++++++++++++ 5 files changed, 65 insertions(+) create mode 100644 src/providers/firebase/firebase.module.ts create mode 100644 src/providers/firebase/firebase.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index 1c1614684..6392b605a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -32,6 +32,7 @@ import { AirtableModule } from './providers/airtable/airtable.module'; import { CloudinaryModule } from './providers/cloudinary/cloudinary.module'; import { DnsModule } from './providers/dns/dns.module'; import { ElasticSearchModule } from './providers/elasticsearch/elasticsearch.module'; +import { FirebaseModule } from './providers/firebase/firebase.module'; import { GeolocationModule } from './providers/geolocation/geolocation.module'; import { MailModule } from './providers/mail/mail.module'; import { PrismaModule } from './providers/prisma/prisma.module'; @@ -72,6 +73,7 @@ import { TasksModule } from './providers/tasks/tasks.module'; AirtableModule, S3Module, CloudinaryModule, + FirebaseModule, ], providers: [ { diff --git a/src/config/configuration.interface.ts b/src/config/configuration.interface.ts index c88083ea4..e332dcc3b 100644 --- a/src/config/configuration.interface.ts +++ b/src/config/configuration.interface.ts @@ -109,4 +109,11 @@ export interface Configuration { apiKey: string; apiSecret: string; }; + + firebase: { + serviceAccountKey: + | string + | { projectId?: string; clientEmail?: string; privateKey?: string }; + databaseUrl: string; + }; } diff --git a/src/config/configuration.ts b/src/config/configuration.ts index f23b5477a..7eb5d6d14 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -120,6 +120,16 @@ const configuration: Configuration = { apiKey: process.env.CLOUDINARY_API_KEY ?? '', apiSecret: process.env.CLOUDINARY_API_SECRET ?? '', }, + firebase: { + serviceAccountKey: process.env.FIREBASE_PROJECT_ID + ? { + projectId: process.env.FIREBASE_PROJECT_ID, + clientEmail: process.env.FIREBASE_CLIENT_EMAIL, + privateKey: process.env.FIREBASE_PRIVATE_KEY, + } + : process.env.FIREBASE_SERVICE_ACCOUNT_KEY, + databaseUrl: process.env.FIREBASE_DATABASE_URL, + }, }; const configFunction: ConfigFactory = () => configuration; diff --git a/src/providers/firebase/firebase.module.ts b/src/providers/firebase/firebase.module.ts new file mode 100644 index 000000000..9b4288502 --- /dev/null +++ b/src/providers/firebase/firebase.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; +import { FirebaseService } from './firebase.service'; + +@Module({ + imports: [ConfigModule], + providers: [FirebaseService], + exports: [FirebaseService], +}) +export class FirebaseModule {} diff --git a/src/providers/firebase/firebase.service.ts b/src/providers/firebase/firebase.service.ts new file mode 100644 index 000000000..4e1bbc78e --- /dev/null +++ b/src/providers/firebase/firebase.service.ts @@ -0,0 +1,36 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import admin from 'firebase-admin'; +import { Configuration } from '../../config/configuration.interface'; + +@Injectable() +export class FirebaseService { + private logger = new Logger(FirebaseService.name); + admin = admin; + + constructor(private configService: ConfigService) { + const config = this.configService.get( + 'firebase', + ); + if (config.serviceAccountKey) + admin.initializeApp({ + credential: admin.credential.cert( + typeof config.serviceAccountKey === 'string' + ? JSON.parse(config.serviceAccountKey) + : config.serviceAccountKey, + ), + databaseURL: config.databaseUrl, + }); + else this.logger.warn('Firebase API key not found'); + } + + async addCollectionItem(collectionName: string, data: any) { + const reference = admin.firestore().collection(collectionName); + return reference.add(data); + } + + async updateCollectionItem(collectionName: string, doc: string, data: any) { + const reference = admin.firestore().collection(collectionName).doc(doc); + return reference.update(data); + } +}