Coder Social home page Coder Social logo

nestjs-firebase's Introduction

nhogs-logo

@nhogs/nestjs-firebase

npm peer dependency version Firebase npm peer dependency version NestJS)

Firebase module for NestJS.

CI Maintainability Test Coverage

Continuous Integration tests with Github Action on Firebase Emulator Suite

Installation

npm

$ npm i --save @nhogs/nestjs-firebase

First you need to have a running firebase project. See Add Firebase to your JavaScript project

โš ๏ธ This module only uses Firebase client API

Module

Add the module in app imports with project settings

@Module({
  imports: [
    FirebaseModule.forRoot({
      apiKey: "API_KEY",
      authDomain: "PROJECT_ID.firebaseapp.com",
      projectId: "PROJECT_ID",
      storageBucket: "PROJECT_ID.appspot.com",
      // Optional name of the app to initialize. Custom name for the Firebase App. The default value is "[DEFAULT]"
      // appName: "APP_NAME",
    }),
    CatsModule,
  ],
})
export class AppModule {}

Check here for an e2e test with real example on Cat module

This module is a simple wrapper on Firebase API and export 3 services.

  • Cloud Firestore
  • Storage
  • Authentication

Cloud Firestore

Exposes most of Firebase firestore web api in the service.

// Add document example

it("should addDoc", async () => {
  const users =
    firestoreService.collection("users").withConverter < User > userConverter;

  const doc = (await firestoreService.addDoc) < User > (users, u1);

  const snapshot = await firestoreService.getDoc(doc);
  expect(snapshot.data()).toMatchInlineSnapshot(`
    User {
      "age": 1,
      "firstName": "fn1",
      "lastName": "ln1",
    }
  `);
});

See firestore.service.spec.ts for detailed usage.

Storage

Exposes most of Firebase storage web api in the service.

// Upload string and get download url example
it(`should upload string`, async () => {
  const fileName = "file.txt";
  await storageService
    .uploadString(fileName, "text content", "raw", {
      contentType: "text/plain",
    })
    .then((snapshot) => {
      expect(snapshot.metadata.contentType).toMatchInlineSnapshot(
        `"text/plain"`
      );
    });

  const downloadUrl = await storageService.getDownloadURL(fileName);
  expect(downloadUrl).toMatchInlineSnapshot(
    `"http://localhost:9199/v0/b/default-bucket/o/file.txt?alt=media&token=86739ce5-a96e-41ad-b807-e05b12e36516"`
  );
});

See storage.service.spec.ts for detailed usage.

Authentication

See auth.service.spec.ts for detailed usage.

License

MIT license

nestjs-firebase's People

Contributors

ciminf avatar popotojs avatar gokcer 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.