-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add firestore module connection
- Loading branch information
1 parent
ba9bf25
commit 2163b22
Showing
7 changed files
with
62 additions
and
1 deletion.
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
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], | ||
} | ||
} | ||
} |
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,3 @@ | ||
export const FirestoreDatabaseProvider = 'firestoredb' | ||
export const FirestoreOptionsProvider = 'firestoreOptions' | ||
export const FirestoreCollectionProviders: string[] = [] |
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 @@ | ||
export * from './firestore.module' |
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,7 @@ | ||
import { Settings } from '@google-cloud/firestore' | ||
|
||
export type FirestoreModuleOptions = { | ||
imports: any[] | ||
useFactory: (...args: any[]) => Settings | ||
inject: any[] | ||
} |
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 @@ | ||
export * from './firestore' |