@rxdi/firestore 0.7.118

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,161 @@
1
+ # Firebase firestore reactive database mixins
2
+
3
+ - TypeSafe, Reactive
4
+ - Firebase cloud function and AWS Lambda compatability
5
+
6
+ #### Install
7
+ ```bash
8
+ npm i @rxdi/firestore
9
+ ```
10
+
11
+ #### Initialize firebase module
12
+ ```typescript
13
+
14
+ import { Module } from '@rxdi/core';
15
+ import { FirebaseModule } from '@rxdi/firestore';
16
+ import * as functions from 'firebase-functions';
17
+ import * as admin from 'firebase-admin';
18
+
19
+ @Module({
20
+ imports: [
21
+ FirebaseModule.forRoot({
22
+ projectId: 'your-firebase-project-id',
23
+ credential: process.env.IS_NOT_LAMBDA
24
+ ? admin.credential.applicationDefault()
25
+ : functions.config().firebase
26
+ }),
27
+ ],
28
+ })
29
+ export class AppModule {}
30
+ ```
31
+
32
+ #### Set environment variable `GOOGLE_APPLICATION_CREDENTIALS` representing your Firebase configuration
33
+
34
+ > More info can be found here https://firebase.google.com/docs/admin/setup
35
+
36
+ ```bash
37
+ export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
38
+ ```
39
+
40
+ ```json
41
+ {
42
+ "type": "service_account",
43
+ "project_id": "",
44
+ "private_key_id": "",
45
+ "private_key": "-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----\n",
46
+ "client_email": "",
47
+ "client_id": "",
48
+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
49
+ "token_uri": "https://oauth2.googleapis.com/token",
50
+ "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
51
+ "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-eb9yl%40xx-xx-xx.iam.gserviceaccount.com"
52
+ }
53
+ ```
54
+
55
+ When executing `admin.credential.applicationDefault()` firebase will get path from `GOOGLE_APPLICATION_CREDENTIALS` and try to read and load the credentials.
56
+
57
+
58
+
59
+ #### Define your reactive firestore collections
60
+
61
+
62
+ ```typescript
63
+ import { Injectable, Inject } from '@rxdi/core';
64
+ import { GenericFirebaseModel, Firestore } from '@rxdi/firestore';
65
+
66
+ interface IUserType {
67
+ id: string;
68
+ displayName: string;
69
+ email: string;
70
+ }
71
+
72
+ @Injectable()
73
+ export class UserCollection extends GenericFirebaseModel<IUserType> {
74
+ constructor(@Inject(Firestore) firestore: Firestore) {
75
+ super('users', firestore);
76
+ }
77
+ }
78
+
79
+ ```
80
+
81
+ #### import `UserCollection` inside `AppModule` as a provider/service
82
+
83
+ ```typescript
84
+ import { Module } from '@rxdi/core';
85
+ import { FirebaseModule } from '@rxdi/firestore';
86
+ import { ModelsModule } from '../database/models.module';
87
+ import * as functions from 'firebase-functions';
88
+ import * as admin from 'firebase-admin';
89
+ import { UserCollection } from './models/user';
90
+
91
+ @Module({
92
+ imports: [
93
+ FirebaseModule.forRoot({
94
+ projectId: 'your-firebase-project-id',
95
+ credential: process.env.IS_NOT_LAMBDA
96
+ ? admin.credential.applicationDefault()
97
+ : functions.config().firebase
98
+ }),
99
+ ],
100
+ providers: [UserCollection]
101
+ })
102
+ export class AppModule {}
103
+
104
+ ```
105
+
106
+
107
+ #### Use created mixins
108
+
109
+
110
+ ```typescript
111
+ import { Injectable } from '@rxdi/core';
112
+ import { UserCollection } from '../models/user';
113
+
114
+ @Injectable()
115
+ export class UserCollectionService {
116
+ constructor(
117
+ private userCollection: UserCollection
118
+ ) {}
119
+ }
120
+
121
+ ```
122
+
123
+ #### Mixins provide the following instance methods
124
+
125
+ ```typescript
126
+ import { CollectionReference, Firestore } from '@google-cloud/firestore';
127
+ import { StaticMethods } from './static-mixins';
128
+ export declare class FirestoreCollection<T> extends StaticMethods {
129
+ private collection;
130
+ constructor(collectionName: string, firestore: Firestore);
131
+ getCollectionRef(): CollectionReference;
132
+ getFirestoreRef(): Firestore;
133
+ getRef(doc: string): FirebaseFirestore.DocumentReference;
134
+ create(payload: T, doc?: string): Promise<T>;
135
+ get(doc: string): Promise<T>;
136
+ delete(doc: string): Promise<FirebaseFirestore.WriteResult>;
137
+ update(doc: string, payload: T): Promise<T>;
138
+ findAll(where?: T): Promise<T[]>;
139
+ find(payload: T): Promise<T>;
140
+ build<T>(payload: T): T;
141
+ }
142
+ ```
143
+
144
+
145
+ #### Mixins provide also static methods
146
+
147
+ ```typescript
148
+ import { FirestoreCollection } from './mixins';
149
+ export declare class StaticMethods {
150
+ static create<T>(payload: T, doc?: string): Promise<T>;
151
+ static getCollectionRef(): FirebaseFirestore.CollectionReference;
152
+ static getFirestoreRef(): FirebaseFirestore.Firestore;
153
+ static getRef(doc: string): FirebaseFirestore.DocumentReference;
154
+ static get<T>(doc: string): Promise<T>;
155
+ static delete(doc: string): Promise<FirebaseFirestore.WriteResult>;
156
+ static update<T>(doc: string, payload: T): Promise<T>;
157
+ static findAll<T>(where?: T): Promise<T[]>;
158
+ static find<T>(payload: T): Promise<T>;
159
+ static build<T>(payload: T): T;
160
+ }
161
+ ```
@@ -0,0 +1,4 @@
1
+ import { InjectionToken } from '@rxdi/core';
2
+ export declare const Firestore: InjectionToken<FirebaseFirestore.Firestore>;
3
+ export interface Firestore extends FirebaseFirestore.Firestore {
4
+ }
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Firestore = void 0;
4
+ const core_1 = require("@rxdi/core");
5
+ exports.Firestore = new core_1.InjectionToken('firestore-database');
@@ -0,0 +1,7 @@
1
+ import { ModuleWithProviders } from '@rxdi/core';
2
+ import * as admin from 'firebase-admin';
3
+ export declare class FirebaseModule {
4
+ static forRoot(config: admin.AppOptions): ModuleWithProviders;
5
+ }
6
+ export * from './firebase.tokens';
7
+ export * from './mixins';
package/dist/index.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
10
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
11
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
12
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
13
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
14
+ };
15
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
+ for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
17
+ };
18
+ var FirebaseModule_1;
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.FirebaseModule = void 0;
21
+ const core_1 = require("@rxdi/core");
22
+ const admin = require("firebase-admin");
23
+ const firebase_tokens_1 = require("./firebase.tokens");
24
+ let FirebaseModule = FirebaseModule_1 = class FirebaseModule {
25
+ static forRoot(config) {
26
+ return {
27
+ module: FirebaseModule_1,
28
+ providers: [
29
+ {
30
+ provide: firebase_tokens_1.Firestore,
31
+ useFactory: () => {
32
+ admin.initializeApp(config);
33
+ return admin.firestore();
34
+ }
35
+ }
36
+ ]
37
+ };
38
+ }
39
+ };
40
+ FirebaseModule = FirebaseModule_1 = __decorate([
41
+ core_1.Module()
42
+ ], FirebaseModule);
43
+ exports.FirebaseModule = FirebaseModule;
44
+ __exportStar(require("./firebase.tokens"), exports);
45
+ __exportStar(require("./mixins"), exports);
@@ -0,0 +1,17 @@
1
+ /// <reference types="firebase-admin/node_modules/@google-cloud/firestore" />
2
+ import { CollectionReference, Firestore } from '@google-cloud/firestore';
3
+ import { StaticMethods } from './static-mixins';
4
+ export declare class FirestoreCollection<T> extends StaticMethods {
5
+ private collection;
6
+ constructor(collectionName: string, firestore: Firestore);
7
+ getCollectionRef(): CollectionReference<FirebaseFirestore.DocumentData>;
8
+ getFirestoreRef(): Firestore;
9
+ getRef(doc: string): FirebaseFirestore.DocumentReference<FirebaseFirestore.DocumentData>;
10
+ create(payload: T, doc?: string): Promise<T>;
11
+ get(doc: string): Promise<T>;
12
+ delete(doc: string): Promise<FirebaseFirestore.WriteResult>;
13
+ update(doc: string, payload: T): Promise<T>;
14
+ findAll(where?: T): Promise<T[]>;
15
+ find(payload: T): Promise<T>;
16
+ build<T>(payload: T): T;
17
+ }
package/dist/mixins.js ADDED
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.FirestoreCollection = void 0;
13
+ const static_mixins_1 = require("./static-mixins");
14
+ class FirestoreCollection extends static_mixins_1.StaticMethods {
15
+ constructor(collectionName, firestore) {
16
+ super();
17
+ this.collection = firestore.collection(collectionName);
18
+ FirestoreCollection.setStaticSelf(this);
19
+ }
20
+ getCollectionRef() {
21
+ return this.collection;
22
+ }
23
+ getFirestoreRef() {
24
+ return this.collection.firestore;
25
+ }
26
+ getRef(doc) {
27
+ return this.collection.doc(doc);
28
+ }
29
+ create(payload, doc) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ let ref = this.collection.doc();
32
+ if (doc) {
33
+ ref = this.getRef(doc);
34
+ }
35
+ payload['id'] = ref.id;
36
+ const document = yield ref.set(payload);
37
+ return Object.assign({ writeTime: document.writeTime }, payload);
38
+ });
39
+ }
40
+ get(doc) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ return (yield this.getRef(doc).get()).data();
43
+ });
44
+ }
45
+ delete(doc) {
46
+ return this.getRef(doc).delete();
47
+ }
48
+ update(doc, payload) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ yield this.getRef(doc).update(payload);
51
+ return this.get(doc);
52
+ });
53
+ }
54
+ findAll(where) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ if (!where) {
57
+ const snapshot = yield this.collection.get();
58
+ return snapshot.docs.map(doc => doc.data());
59
+ }
60
+ let query;
61
+ Object.keys(where).forEach(k => {
62
+ if (!query) {
63
+ query = this.collection.where(k, '==', where[k]);
64
+ }
65
+ query.where(k, '==', where[k]);
66
+ });
67
+ return (yield query.get()).docs.map(doc => doc.data());
68
+ });
69
+ }
70
+ find(payload) {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ const docs = yield this.findAll(payload);
73
+ if (docs.length > 1) {
74
+ throw new Error('More than one documents found for this query');
75
+ }
76
+ if (docs.length) {
77
+ return docs[0];
78
+ }
79
+ return null;
80
+ });
81
+ }
82
+ build(payload) {
83
+ return payload;
84
+ }
85
+ }
86
+ exports.FirestoreCollection = FirestoreCollection;
@@ -0,0 +1,15 @@
1
+ import { FirestoreCollection } from './mixins';
2
+ export declare class StaticMethods {
3
+ static model: FirestoreCollection<any>;
4
+ static setStaticSelf<T>(self: FirestoreCollection<T>): void;
5
+ static create<T>(payload: T, doc?: string): Promise<T>;
6
+ static getCollectionRef(): FirebaseFirestore.CollectionReference<FirebaseFirestore.DocumentData>;
7
+ static getFirestoreRef(): FirebaseFirestore.Firestore;
8
+ static getRef(doc: string): FirebaseFirestore.DocumentReference<FirebaseFirestore.DocumentData>;
9
+ static get<T>(doc: string): Promise<T>;
10
+ static delete(doc: string): Promise<FirebaseFirestore.WriteResult>;
11
+ static update<T>(doc: string, payload: T): Promise<T>;
12
+ static findAll<T>(where?: T): Promise<T[]>;
13
+ static find<T>(payload: T): Promise<T>;
14
+ static build<T>(payload: T): T;
15
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.StaticMethods = void 0;
13
+ class StaticMethods {
14
+ static setStaticSelf(self) {
15
+ StaticMethods.model = self;
16
+ }
17
+ static create(payload, doc) {
18
+ return StaticMethods.model.create(payload, doc);
19
+ }
20
+ static getCollectionRef() {
21
+ return StaticMethods.model.getCollectionRef();
22
+ }
23
+ static getFirestoreRef() {
24
+ return StaticMethods.model.getFirestoreRef();
25
+ }
26
+ static getRef(doc) {
27
+ return StaticMethods.model.getRef(doc);
28
+ }
29
+ static get(doc) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ return StaticMethods.model.get(doc);
32
+ });
33
+ }
34
+ static delete(doc) {
35
+ return StaticMethods.model.delete(doc);
36
+ }
37
+ static update(doc, payload) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ return StaticMethods.model.update(doc, payload);
40
+ });
41
+ }
42
+ static findAll(where) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ return StaticMethods.model.findAll(where);
45
+ });
46
+ }
47
+ static find(payload) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ return StaticMethods.model.find(payload);
50
+ });
51
+ }
52
+ static build(payload) {
53
+ return payload;
54
+ }
55
+ }
56
+ exports.StaticMethods = StaticMethods;
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@rxdi/firestore",
3
+ "version": "0.7.118",
4
+ "main": "./dist/index.js",
5
+ "author": "Kristiyan Tachev",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/rxdi/firestore"
10
+ },
11
+ "scripts": {
12
+ "build": "tsc || true"
13
+ },
14
+ "types": "./dist/index.d.ts",
15
+ "module": "./dist/index.js",
16
+ "typings": "./dist/index.d.ts",
17
+ "dependencies": {
18
+ "firebase-admin": "^8.2.0",
19
+ "@google-cloud/firestore": "2.2.1",
20
+ "firebase-functions": "^3.0.0"
21
+ },
22
+ "devDependencies": {
23
+ "typescript": "^3.9.3",
24
+ "@rxdi/core": "^0.7.117"
25
+ }
26
+ }