-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathfirestore.ts
241 lines (228 loc) · 9.36 KB
/
firestore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { isPlatformServer } from '@angular/common';
import { Inject, Injectable, InjectionToken, NgZone, Optional, PLATFORM_ID, inject } from '@angular/core';
import { ɵAngularFireSchedulers } from '@angular/fire';
import { AppCheckInstances } from '@angular/fire/app-check';
import { FIREBASE_APP_NAME, FIREBASE_OPTIONS, ɵcacheInstance, ɵfirebaseAppFactory } from '@angular/fire/compat';
import {
SETTINGS as AUTH_SETTINGS,
AngularFireAuth,
LANGUAGE_CODE,
PERSISTENCE,
TENANT_ID,
USE_EMULATOR as USE_AUTH_EMULATOR,
USE_DEVICE_LANGUAGE,
ɵauthFactory,
} from '@angular/fire/compat/auth';
import { FirebaseOptions } from 'firebase/app';
import firebase from 'firebase/compat/app';
import { Observable, from, of } from 'rxjs';
import { AngularFirestoreCollection } from './collection/collection';
import { AngularFirestoreCollectionGroup } from './collection-group/collection-group';
import { AngularFirestoreDocument } from './document/document';
import {
AssociatedReference,
CollectionReference,
DocumentReference,
PersistenceSettings,
Query,
QueryFn,
QueryGroupFn,
Settings
} from './interfaces';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
/**
* The value of this token determines whether or not the firestore will have persistance enabled
*/
export const ENABLE_PERSISTENCE = new InjectionToken<boolean>('angularfire2.enableFirestorePersistence');
export const PERSISTENCE_SETTINGS = new InjectionToken<PersistenceSettings | undefined>('angularfire2.firestore.persistenceSettings');
export const SETTINGS = new InjectionToken<Settings>('angularfire2.firestore.settings');
type UseEmulatorArguments = Parameters<firebase.firestore.Firestore['useEmulator']>;
export const USE_EMULATOR = new InjectionToken<UseEmulatorArguments>('angularfire2.firestore.use-emulator');
/**
* A utility methods for associating a collection reference with
* a query.
*
* @param collectionRef - A collection reference to query
* @param queryFn - The callback to create a query
*
* Example:
* const { query, ref } = associateQuery(docRef.collection('items'), ref => {
* return ref.where('age', '<', 200);
* });
*/
export function associateQuery<T>(collectionRef: CollectionReference<T>, queryFn = ref => ref): AssociatedReference<T> {
const query = queryFn(collectionRef);
const ref = collectionRef;
return { query, ref };
}
/**
* AngularFirestore Service
*
* This service is the main entry point for this feature module. It provides
* an API for creating Collection and Reference services. These services can
* then be used to do data updates and observable streams of the data.
*
* Example:
*
* import { Component } from '@angular/core';
* import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from '@angular/fire/firestore';
* import { Observable } from 'rxjs/Observable';
* import { from } from 'rxjs/observable';
*
* @Component({
* selector: 'app-my-component',
* template: `
* <h2>Items for {{ (profile | async)?.name }}
* <ul>
* <li *ngFor="let item of items | async">{{ item.name }}</li>
* </ul>
* <div class="control-input">
* <input type="text" #itemname />
* <button (click)="addItem(itemname.value)">Add Item</button>
* </div>
* `
* })
* export class MyComponent implements OnInit {
*
* // services for data operations and data streaming
* private readonly itemsRef: AngularFirestoreCollection<Item>;
* private readonly profileRef: AngularFirestoreDocument<Profile>;
*
* // observables for template
* items: Observable<Item[]>;
* profile: Observable<Profile>;
*
* // inject main service
* constructor(private readonly afs: AngularFirestore) {}
*
* ngOnInit() {
* this.itemsRef = afs.collection('items', ref => ref.where('user', '==', 'davideast').limit(10));
* this.items = this.itemsRef.valueChanges().map(snap => snap.docs.map(data => doc.data()));
* // this.items = from(this.itemsRef); // you can also do this with no mapping
*
* this.profileRef = afs.doc('users/davideast');
* this.profile = this.profileRef.valueChanges();
* }
*
* addItem(name: string) {
* const user = 'davideast';
* this.itemsRef.add({ name, user });
* }
* }
*/
@Injectable({
providedIn: 'any'
})
export class AngularFirestore {
public readonly firestore: firebase.firestore.Firestore;
public readonly persistenceEnabled$: Observable<boolean>;
private readonly ngZone = inject(NgZone);
/**
* Each Feature of AngularFire has a FirebaseApp injected. This way we
* don't rely on the main Firebase App instance and we can create named
* apps and use multiple apps.
*/
constructor(
@Inject(FIREBASE_OPTIONS) options: FirebaseOptions,
@Optional() @Inject(FIREBASE_APP_NAME) name: string | null | undefined,
@Optional() @Inject(ENABLE_PERSISTENCE) shouldEnablePersistence: boolean | null,
@Optional() @Inject(SETTINGS) settings: Settings | null,
// eslint-disable-next-line @typescript-eslint/ban-types
@Inject(PLATFORM_ID) platformId: Object,
zone: NgZone,
public schedulers: ɵAngularFireSchedulers,
@Optional() @Inject(PERSISTENCE_SETTINGS) persistenceSettings: PersistenceSettings | null,
@Optional() @Inject(USE_EMULATOR) _useEmulator: any,
@Optional() auth: AngularFireAuth,
@Optional() @Inject(USE_AUTH_EMULATOR) useAuthEmulator: any,
@Optional() @Inject(AUTH_SETTINGS) authSettings: any, // can't use firebase.auth.AuthSettings here
@Optional() @Inject(TENANT_ID) tenantId: string | null,
@Optional() @Inject(LANGUAGE_CODE) languageCode: string | null,
@Optional() @Inject(USE_DEVICE_LANGUAGE) useDeviceLanguage: boolean | null,
@Optional() @Inject(PERSISTENCE) persistence: string | null,
@Optional() _appCheckInstances: AppCheckInstances,
) {
const app = ɵfirebaseAppFactory(options, zone, name);
const useEmulator: UseEmulatorArguments | null = _useEmulator;
if (auth) {
ɵauthFactory(app, zone, useAuthEmulator, tenantId, languageCode, useDeviceLanguage, authSettings, persistence);
}
[this.firestore, this.persistenceEnabled$] = ɵcacheInstance(`${app.name}.firestore`, 'AngularFirestore', app.name, () => {
const firestore = zone.runOutsideAngular(() => app.firestore());
if (settings) {
firestore.settings(settings);
}
if (useEmulator) {
firestore.useEmulator(...useEmulator);
}
if (shouldEnablePersistence && !isPlatformServer(platformId)) {
// We need to try/catch here because not all enablePersistence() failures are caught
// https://github.com/firebase/firebase-js-sdk/issues/608
const enablePersistence = () => {
try {
return from(firestore.enablePersistence(persistenceSettings || undefined).then(() => true, () => false));
} catch (e) {
if (typeof console !== 'undefined') { console.warn(e); }
return of(false);
}
};
return [firestore, zone.runOutsideAngular(enablePersistence)];
} else {
return [firestore, of(false)];
}
}, [settings, useEmulator, shouldEnablePersistence]);
}
/**
* Create a reference to a Firestore Collection based on a path or
* CollectionReference and an optional query function to narrow the result
* set.
*/
collection<T>(path: string, queryFn?: QueryFn): AngularFirestoreCollection<T>;
collection<T>(ref: CollectionReference, queryFn?: QueryFn): AngularFirestoreCollection<T>;
collection<T>(pathOrRef: string | CollectionReference<T>, queryFn?: QueryFn): AngularFirestoreCollection<T> {
let collectionRef: CollectionReference<T>;
if (typeof pathOrRef === 'string') {
collectionRef = this.firestore.collection(pathOrRef) as firebase.firestore.CollectionReference<T>;
} else {
collectionRef = pathOrRef;
}
const { ref, query } = associateQuery<T>(collectionRef, queryFn);
const refInZone = this.ngZone.run(() => ref);
return new AngularFirestoreCollection<T>(refInZone, query, this);
}
/**
* Create a reference to a Firestore Collection Group based on a collectionId
* and an optional query function to narrow the result
* set.
*/
collectionGroup<T>(collectionId: string, queryGroupFn?: QueryGroupFn<T>): AngularFirestoreCollectionGroup<T> {
const queryFn = queryGroupFn || (ref => ref);
const collectionGroup: Query<T> = this.firestore.collectionGroup(collectionId) as firebase.firestore.Query<T>;
return new AngularFirestoreCollectionGroup<T>(queryFn(collectionGroup), this);
}
/**
* Create a reference to a Firestore Document based on a path or
* DocumentReference. Note that documents are not queryable because they are
* simply objects. However, documents have sub-collections that return a
* Collection reference and can be queried.
*/
doc<T>(path: string): AngularFirestoreDocument<T>;
doc<T>(ref: DocumentReference): AngularFirestoreDocument<T>;
doc<T>(pathOrRef: string | DocumentReference<T>): AngularFirestoreDocument<T> {
let ref: DocumentReference<T>;
if (typeof pathOrRef === 'string') {
ref = this.firestore.doc(pathOrRef) as firebase.firestore.DocumentReference<T>;
} else {
ref = pathOrRef;
}
const refInZone = this.ngZone.run(() => ref);
return new AngularFirestoreDocument<T>(refInZone, this);
}
/**
* Returns a generated Firestore Document Id.
*/
createId() {
return this.firestore.collection('_').doc().id;
}
}