npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rxdi/firestore

v0.7.178

Published

- TypeSafe, Reactive - Firebase cloud function and AWS Lambda compatability

Downloads

165

Readme

Firebase firestore reactive database mixins

  • TypeSafe, Reactive
  • Firebase cloud function and AWS Lambda compatability

Install

npm i @rxdi/firestore

Initialize firebase module


import { Module } from '@rxdi/core';
import { FirebaseModule } from '@rxdi/firestore';
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

@Module({
  imports: [
    FirebaseModule.forRoot({
      projectId: 'your-firebase-project-id',
      credential: process.env.IS_NOT_LAMBDA
        ? admin.credential.applicationDefault()
        : functions.config().firebase
    }),
  ],
})
export class AppModule {}

Set environment variable GOOGLE_APPLICATION_CREDENTIALS representing your Firebase configuration

More info can be found here https://firebase.google.com/docs/admin/setup

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
{
    "type": "service_account",
    "project_id": "",
    "private_key_id": "",
    "private_key": "-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----\n",
    "client_email": "",
    "client_id": "",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-eb9yl%40xx-xx-xx.iam.gserviceaccount.com"
  }

When executing admin.credential.applicationDefault() firebase will get path from GOOGLE_APPLICATION_CREDENTIALS and try to read and load the credentials.

Define your reactive firestore collections

import { Injectable, Inject } from '@rxdi/core';
import { GenericFirebaseModel, Firestore } from '@rxdi/firestore';

interface IUserType {
  id: string;
  displayName: string;
  email: string;
}

@Injectable()
export class UserCollection extends GenericFirebaseModel<IUserType> {
  constructor(@Inject(Firestore) firestore: Firestore) {
    super('users', firestore);
  }
}

import UserCollection inside AppModule as a provider/service

import { Module } from '@rxdi/core';
import { FirebaseModule } from '@rxdi/firestore';
import { ModelsModule } from '../database/models.module';
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { UserCollection } from './models/user';

@Module({
  imports: [
    FirebaseModule.forRoot({
      projectId: 'your-firebase-project-id',
      credential: process.env.IS_NOT_LAMBDA
        ? admin.credential.applicationDefault()
        : functions.config().firebase
    }),
  ],
  providers: [UserCollection]
})
export class AppModule {}

Use created mixins

import { Injectable } from '@rxdi/core';
import { UserCollection } from '../models/user';

@Injectable()
export class UserCollectionService {
  constructor(
    private userCollection: UserCollection
  ) {}
}

Mixins provide the following instance methods

import { CollectionReference, Firestore } from '@google-cloud/firestore';
import { StaticMethods } from './static-mixins';
export declare class FirestoreCollection<T> extends StaticMethods {
    private collection;
    constructor(collectionName: string, firestore: Firestore);
    getCollectionRef(): CollectionReference;
    getFirestoreRef(): Firestore;
    getRef(doc: string): FirebaseFirestore.DocumentReference;
    create(payload: T, doc?: string): Promise<T>;
    get(doc: string): Promise<T>;
    delete(doc: string): Promise<FirebaseFirestore.WriteResult>;
    update(doc: string, payload: T): Promise<T>;
    findAll(where?: T): Promise<T[]>;
    find(payload: T): Promise<T>;
    build<T>(payload: T): T;
}

Mixins provide also static methods

import { FirestoreCollection } from './mixins';
export declare class StaticMethods {
    static create<T>(payload: T, doc?: string): Promise<T>;
    static getCollectionRef(): FirebaseFirestore.CollectionReference;
    static getFirestoreRef(): FirebaseFirestore.Firestore;
    static getRef(doc: string): FirebaseFirestore.DocumentReference;
    static get<T>(doc: string): Promise<T>;
    static delete(doc: string): Promise<FirebaseFirestore.WriteResult>;
    static update<T>(doc: string, payload: T): Promise<T>;
    static findAll<T>(where?: T): Promise<T[]>;
    static find<T>(payload: T): Promise<T>;
    static build<T>(payload: T): T;
}