-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
14 changed files
with
196 additions
and
57 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,10 +1,23 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
import { FirestoreModule } from './firestore/firestore.module'; | ||
import { PostsModule } from './posts/module'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [AppController], | ||
providers: [AppService], | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
isGlobal: true, | ||
}), | ||
FirestoreModule.forRoot({ | ||
imports: [ConfigModule], | ||
useFactory: (configService: ConfigService) => ({ | ||
keyFilename: configService.get<string>('SA_KEY'), | ||
}), | ||
inject: [ConfigService], | ||
}), | ||
PostsModule, | ||
], | ||
controllers: [], | ||
providers: [], | ||
}) | ||
export class AppModule {} |
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
import { Module, DynamicModule } from '@nestjs/common'; | ||
import { Firestore, Settings } from '@google-cloud/firestore'; | ||
import { | ||
FirestoreDatabaseProvider, | ||
FirestoreOptionsProvider, | ||
FirestoreCollectionProviders, | ||
} from './firestore.providers'; | ||
|
||
type FirestoreModuleOptions = { | ||
imports: any[]; | ||
useFactory: (...args: any[]) => Settings; | ||
inject: any[]; | ||
}; | ||
|
||
@Module({}) | ||
export class FirestoreModule { | ||
static forRoot(options: FirestoreModuleOptions): DynamicModule { | ||
const optionsProvider = { | ||
provide: FirestoreOptionsProvider, | ||
useFactory: options.useFactory, | ||
inject: options.inject, | ||
}; | ||
const dbProvider = { | ||
provide: FirestoreDatabaseProvider, | ||
useFactory: (config) => new Firestore(config), | ||
inject: [FirestoreOptionsProvider], | ||
}; | ||
const collectionProviders = FirestoreCollectionProviders.map( | ||
(providerName) => ({ | ||
provide: providerName, | ||
useFactory: (db) => db.collection(providerName), | ||
inject: [FirestoreDatabaseProvider], | ||
}), | ||
); | ||
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,7 @@ | ||
import { PostDocument } from '../posts/document'; | ||
|
||
export const FirestoreDatabaseProvider = 'firestoredb'; | ||
export const FirestoreOptionsProvider = 'firestoreOptions'; | ||
export const FirestoreCollectionProviders: string[] = [ | ||
PostDocument.collectionName, | ||
]; |
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,17 @@ | ||
import { Body, Controller as BaseController, Get, Post } from '@nestjs/common'; | ||
import { PostDocument } from './document'; | ||
import { Service } from './service'; | ||
|
||
@BaseController('posts') | ||
export class Controller { | ||
constructor(private readonly service: Service) {} | ||
|
||
@Get() | ||
findAll(): Promise<PostDocument[]> { | ||
return this.service.findAll(); | ||
} | ||
@Post() | ||
public create(@Body() post: PostDocument): Promise<PostDocument> { | ||
return this.service.create(post); | ||
} | ||
} |
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,9 @@ | ||
import { Timestamp } from '@google-cloud/firestore'; | ||
|
||
export class PostDocument { | ||
static collectionName = 'posts'; | ||
|
||
title: string; | ||
message: string; | ||
date: Timestamp; | ||
} |
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 { Controller } from './controller'; | ||
import { Service } from './service'; | ||
|
||
@Module({ | ||
controllers: [Controller], | ||
providers: [Service], | ||
exports: [Service], | ||
}) | ||
export class PostsModule {} |
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 { | ||
Injectable, | ||
Inject, | ||
Logger, | ||
InternalServerErrorException, | ||
} from '@nestjs/common'; | ||
import * as dayjs from 'dayjs'; | ||
import { CollectionReference, Timestamp } from '@google-cloud/firestore'; | ||
import { PostDocument } from './document'; | ||
|
||
@Injectable() | ||
export class Service { | ||
private logger: Logger = new Logger(Service.name); | ||
|
||
constructor( | ||
@Inject(PostDocument.collectionName) | ||
private postsCollection: CollectionReference<PostDocument>, | ||
) {} | ||
|
||
async create({ title, message }): Promise<PostDocument> { | ||
const t = dayjs(new Date()).valueOf(); | ||
const date = Timestamp.fromMillis(t); | ||
const docRef = this.postsCollection.doc(t.toString()); | ||
await docRef.set({ | ||
title, | ||
message, | ||
date, | ||
}); | ||
const postDoc = await docRef.get(); | ||
const post = postDoc.data(); | ||
return post; | ||
} | ||
|
||
async findAll(): Promise<PostDocument[]> { | ||
const snapshot = await this.postsCollection.get(); | ||
const posts: PostDocument[] = []; | ||
snapshot.forEach((doc) => posts.push(doc.data())); | ||
return posts; | ||
} | ||
} |
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