0.7.178 • Published 26 days ago

@rxdi/firestore v0.7.178

Weekly downloads
11
License
MIT
Repository
github
Last release
26 days ago

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;
}
0.7.176

26 days ago

0.7.175

26 days ago

0.7.178

26 days ago

0.7.177

26 days ago

0.7.174

8 months ago

0.7.173

8 months ago

0.7.170

8 months ago

0.7.172

8 months ago

0.7.171

8 months ago

0.7.169

10 months ago

0.7.167

1 year ago

0.7.168

1 year ago

0.7.163

1 year ago

0.7.162

1 year ago

0.7.165

1 year ago

0.7.164

1 year ago

0.7.166

1 year ago

0.7.161

1 year ago

0.7.160

1 year ago

0.7.159

1 year ago

0.7.158

1 year ago

0.7.157

1 year ago

0.7.152

1 year ago

0.7.154

1 year ago

0.7.153

1 year ago

0.7.156

1 year ago

0.7.155

1 year ago

0.7.151

2 years ago

0.7.150

2 years ago

0.7.149

2 years ago

0.7.148

2 years ago

0.7.147

2 years ago

0.7.141

2 years ago

0.7.140

2 years ago

0.7.143

2 years ago

0.7.142

2 years ago

0.7.145

2 years ago

0.7.144

2 years ago

0.7.146

2 years ago

0.7.138

2 years ago

0.7.137

2 years ago

0.7.139

2 years ago

0.7.136

2 years ago

0.7.135

2 years ago

0.7.121

2 years ago

0.7.120

2 years ago

0.7.123

2 years ago

0.7.122

2 years ago

0.7.125

2 years ago

0.7.124

2 years ago

0.7.130

2 years ago

0.7.132

2 years ago

0.7.131

2 years ago

0.7.134

2 years ago

0.7.133

2 years ago

0.7.127

2 years ago

0.7.126

2 years ago

0.7.129

2 years ago

0.7.128

2 years ago

0.7.118

3 years ago

0.7.119

3 years ago

0.7.116

3 years ago

0.7.117

3 years ago

0.7.115

3 years ago

0.7.114

3 years ago

0.7.113

3 years ago

0.7.112

3 years ago

0.7.111

3 years ago

0.7.110

3 years ago

0.7.109

3 years ago

0.7.108

3 years ago

0.7.107

3 years ago

0.7.105

3 years ago

0.7.104

3 years ago

0.7.106

3 years ago

0.7.101

3 years ago

0.7.103

3 years ago

0.7.102

3 years ago

0.7.100

3 years ago

0.7.99

3 years ago

0.7.98

3 years ago

0.7.97

3 years ago

0.7.95

3 years ago

0.7.94

3 years ago

0.7.96

3 years ago

0.7.93

3 years ago

0.7.91

3 years ago

0.7.92

3 years ago

0.7.90

3 years ago

0.7.88

3 years ago

0.7.89

3 years ago

0.7.87

3 years ago

0.7.86

3 years ago

0.7.85

3 years ago

0.7.84

3 years ago

0.7.83

3 years ago

0.7.82

3 years ago

0.7.81

4 years ago

0.7.80

4 years ago

0.7.79

4 years ago

0.7.78

4 years ago

0.7.77

4 years ago

0.7.76

4 years ago

0.7.75

4 years ago

0.7.74

4 years ago

0.7.73

4 years ago

0.7.72

4 years ago

0.7.71

4 years ago

0.7.70

4 years ago

0.7.69

4 years ago

0.7.68

4 years ago

0.7.67

4 years ago

0.7.66

4 years ago

0.7.63

4 years ago

0.7.62

4 years ago

0.7.61

4 years ago

0.7.57

4 years ago

0.7.56

4 years ago

0.7.59

4 years ago

0.7.58

4 years ago

0.7.60

4 years ago

0.7.53

4 years ago

0.7.52

4 years ago

0.7.51

4 years ago

0.7.50

4 years ago

0.7.46

4 years ago

0.7.48

4 years ago

0.7.47

4 years ago

0.7.49

4 years ago

0.6.12

5 years ago

0.6.11

5 years ago

0.6.10

5 years ago

0.6.9

5 years ago

0.6.8

5 years ago

0.6.7

5 years ago

0.6.6

5 years ago

0.6.5

5 years ago