Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add Firebase module
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 13, 2020
1 parent 4b7a4ad commit 835eb27
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -72,6 +73,7 @@ import { TasksModule } from './providers/tasks/tasks.module';
AirtableModule,
S3Module,
CloudinaryModule,
FirebaseModule,
],
providers: [
{
Expand Down
7 changes: 7 additions & 0 deletions src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,11 @@ export interface Configuration {
apiKey: string;
apiSecret: string;
};

firebase: {
serviceAccountKey:
| string
| { projectId?: string; clientEmail?: string; privateKey?: string };
databaseUrl: string;
};
}
10 changes: 10 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = () => configuration;
Expand Down
10 changes: 10 additions & 0 deletions src/providers/firebase/firebase.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
36 changes: 36 additions & 0 deletions src/providers/firebase/firebase.service.ts
Original file line number Diff line number Diff line change
@@ -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<Configuration['firebase']>(
'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);
}
}

0 comments on commit 835eb27

Please sign in to comment.