Skip to content

Commit

Permalink
feat: add firestore module connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedorrychkov committed Jul 28, 2024
1 parent ba9bf25 commit 2163b22
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@google-cloud/firestore": "^7.9.0",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.3",
"@nestjs/core": "^10.0.0",
Expand Down
10 changes: 9 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ import { Module } from '@nestjs/common'

import { AppController } from './app.controller'
import { AppService } from './app.service'
import { ConfigModule } from '@nestjs/config'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { getEnvFile } from './env'
import { FirestoreModule } from './providers'

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: getEnvFile(),
}),
FirestoreModule.forRoot({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
keyFilename: configService.get<string>('SA_KEY'),
}),
inject: [ConfigService],
}),
],
controllers: [AppController],
providers: [AppService],
Expand Down
40 changes: 40 additions & 0 deletions src/providers/firestore/firestore.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Firestore } from '@google-cloud/firestore'
import { DynamicModule, Module } from '@nestjs/common'

import {
FirestoreCollectionProviders,
FirestoreDatabaseProvider,
FirestoreOptionsProvider,
} from './firestore.providers'
import { FirestoreModuleOptions } from './types'

@Module({})
export class FirestoreModule {
static forRoot(options: FirestoreModuleOptions): DynamicModule {
const collectionProviders = FirestoreCollectionProviders.map((providerName) => ({
provide: providerName,
useFactory: (db) => db.collection(providerName),
inject: [FirestoreDatabaseProvider],
}))

const optionsProvider = {
provide: FirestoreOptionsProvider,
useFactory: options.useFactory,
inject: options.inject,
}

const dbProvider = {
provide: FirestoreDatabaseProvider,
useFactory: (config) => new Firestore(config),
inject: [FirestoreOptionsProvider],
}

return {
global: true,
module: FirestoreModule,
imports: options.imports,
providers: [optionsProvider, dbProvider, ...collectionProviders],
exports: [dbProvider, ...collectionProviders],
}
}
}
3 changes: 3 additions & 0 deletions src/providers/firestore/firestore.providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const FirestoreDatabaseProvider = 'firestoredb'
export const FirestoreOptionsProvider = 'firestoreOptions'
export const FirestoreCollectionProviders: string[] = []
1 change: 1 addition & 0 deletions src/providers/firestore/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './firestore.module'
7 changes: 7 additions & 0 deletions src/providers/firestore/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Settings } from '@google-cloud/firestore'

export type FirestoreModuleOptions = {
imports: any[]
useFactory: (...args: any[]) => Settings
inject: any[]
}
1 change: 1 addition & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './firestore'

0 comments on commit 2163b22

Please sign in to comment.