Giter Club home page Giter Club logo

nest-firebase's Introduction

nest-firebase

nest.js' wrapper for firebase-admin-sdk

Installation

# pnpm & npm whatever you want
$ pnpm i nest-firebase

Usage

Register Module

// app.module.ts
import { Module } from '@nestjs/common';
import { FirebaseModule } from 'nest-firebase';

@Module({
  imports: [FirebaseModule.forRoot({
    /// firebase configuration see https://firebase.google.com/docs/admin/setup#initialize-sdk
  })]
})

Inject into service

Register to module

// <your-module>.module.ts
import { Module } from '@nestjs/common';
import { FirebaseModule } from 'nest-firebase';
@Module({
  imports: [
    FirebaseModule.forFeature({
      auth: true,
      // has other options like firestore, database etc.
    }),

  ],
  ...
})
export class UserModule {}

Inject into service

// <your-module>.service.ts
import { Injectable } from '@nestjs/common'
import { Auth } from 'firebase-admin/auth'

import { InjectFirebaseService } from 'nest-firebase'

@Injectable()
export class UserService {
  constructor(
    @InjectFirebaseService('auth') private readonly firebaseAuthService: Auth,
  ) {}

  // your services
}

Typing injected service

import { Auth } from 'firebase-admin/auth'
import { Database } from 'firebase-admin/database'
import { Firestore } from 'firebase-admin/firestore'
import { InjectFirebaseService } from 'nest-firebase'
import { Injectable } from '@nestjs/common'

@Injectable()
export class ExampleService {
  constructor(
    // Firebase Authentication
    @InjectFirebaseService('auth')
    private readonly firebaseAuthService: Auth,
    // Firebase Realtime Database
    @InjectFirebaseService('database')
    private readonly firebaseDatabaseService: Database,
    // Firebase Cloud Firestore
    @InjectFirebaseService('firestore')
    private readonly firebaseFirestoreService: Firestore,
  ) {}

  // your services
}

Testing

In an integration test, I provide a test case that use in my firebase project (uid,email) if you want to test it, you can provide it as a environment variable

$ UID_TEST=<your-uid> EMAIL_TEST=<your-email> pnpm run test:e2e

Don't forget to provide your firebase configuration in tests/src/firebase.config.ts And don't push it to this repository.

BTW, now the integration test is only for auth (Firebase Authentication) I can't not guarantee that other services will work properly.

Contributing

Sure, you can submit pull request or open an issue for requesting a feature.

Environment Variables (for testing)

FIREBASE_PROJECT_ID=
FIREBASE_PRIVATE_KEY=
FIREBASE_CLIENT_EMAIL=
UID_TEST=
EMAIL_TEST=

nest-firebase's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.