This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b7a4ad
commit 835eb27
Showing
5 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |