A simple Firebase Full-Stack Starter template with firebase functions as backend with NestJS. As frontend it has a simple react template that is deployable to Firebase Hosting. All in the same project to get you off the ground quickly.
This is a simple Full-Stack Template that is using NestJS with Firebase Functions. It is configured as a monorepo (https://docs.nestjs.com/cli/monorepo) so that it is easily extenable with more apps (look at Adding Apps to see how to). Furthermore, the template has a react frontend that is also configured to be easily deploable to Firebase Hosting (https://firebase.google.com/docs/hosting). In both the above, it has been configured to use eslint and prettifer and it also has a pre-commit hook using husky to ensure that linting is run on each commit.
Test the api here: https://europe-west1-firebase-full-stack-starter.cloudfunctions.net/appDomainApi/
.
├── functions # NestJS backend for Firebase functions
│ ├── src # All source code
│ ├── apps # All NestJS app modules
├── config # Configuration for the backend
├── libs # All NestJS libs
└── ... # etc.
└── react-frontend
Since this is just a NestJS project one can simply use the Nest CLI to generate new libs and apps according to the NestJS Docs.
Adding a library can be done with:
$ cd functions/src
$ nest g library my-library
To add a new module then simply rujn the following:
$ cd functions/src
$ nest generate app my-app-domain
In this project, there are three examples of firebase functions.
- Https On Request (hosting the NestJS api)
- Pubsub Topic On Publish Listener
- Pubsub Cloud Scheduler
To then wire it up with firebase use (look at apps/firebase-full-stack-starter/src/index.ts):
const expressServer = express();
const createFunction = async (expressInstance): Promise<void> => {
const app = await NestFactory.create(
AppDomainModule,
new ExpressAdapter(expressInstance),
);
await app.init();
};
export const appDomainApi = functions
.region('europe-west1')
.https.onRequest(async (request, response) => {
await createFunction(expressServer);
expressServer(request, response);
});
Use the PubsubEventsService to register your topic. Provide the topic to listener for and then also the domain from where it should find the actual handler. The Pubsub Service will look for classes decorated with OnMessage with the topic:
// Pubsub listening on 'my-event'
export const onMyEvent = PubsubEventsService.topic('my-event', AppDomainModule);
......
@Injectable()
export class MyEventListener {
constructor(private readonly logging: FirebaseLoggingService) {}
@OnMessage({
topic: 'my-event',
})
async onMyEventMessage(message: Message): Promise<void> {
this.logging.log(
`Handling my event and received message ${JSON.stringify(message)}`,
);
}
}
To initiate the cloud schedule then first create a class with the PubsubScheduleInterface interface and instantiate it as seen here:
// Pubsub scheduler
export const mySchedule = PubsubEventsService.schedule(
`every monday 06:00`,
AppDomainModule,
MyScheduleScheduler,
);
......
@Injectable()
export class MyScheduleScheduler implements PubsubScheduleInterface {
constructor(private readonly logging: FirebaseLoggingService) {}
schedule(context: EventContext): Promise<any> {
this.logging.log(`We are now handling stuff in ${MyScheduleScheduler}`);
return null;
}
}
$ npm run install-all
$ npm run build
Remember to fetch your own service account for firebase admin and add it to to a dev-config.ts. Then you can run the following command which also injects it correctly to the config.ts
$ npm run deploy:dev
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
# unit tests
$ npm run test
# e2e tests (it runs only for one domain so add more if needed)
$ test:app-domain::e2e
# test coverage
$ npm run test:cov
This project was bootstrapped with Create React App, using the Redux and Redux Toolkit template.
In the project directory, you can run:
Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.
The page will reload if you make edits.
You will also see any lint errors in the console.
Launches the test runner in the interactive watch mode.
See the section about running tests for more information.
Builds the app for production to the build
folder.
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.
Your app is ready to be deployed!
See the section about deployment for more information.
Note: this is a one-way operation. Once you eject
, you can’t go back!
If you aren’t satisfied with the build tool and configuration choices, you can eject
at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except eject
will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
You don’t have to ever use eject
. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
# Run linting on the react frontend
$ npm run lint-react
# Build and deploy
$ npm run deploy:react-frontend:dev
- Author - Brian Bak Laursen