-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
database.ts
61 lines (53 loc) · 2.04 KB
/
database.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
import { Injectable, Inject, Optional, NgZone, PLATFORM_ID } from '@angular/core';
import { DatabaseQuery, PathReference, DatabaseSnapshot, ChildEvent, ListenEvent, QueryFn, AngularFireList, AngularFireObject } from './interfaces';
import { getRef } from './utils';
import { InjectionToken } from '@angular/core';
import { createListReference } from './list/create-reference';
import { createObjectReference } from './object/create-reference';
import { FirebaseDatabase, FirebaseOptions, FirebaseAppConfig, FirebaseOptionsToken, FirebaseNameOrConfigToken, RealtimeDatabaseURL, _firebaseAppFactory, FirebaseZoneScheduler } from '@angular/fire';
@Injectable()
export class AngularFireDatabase {
public readonly database: FirebaseDatabase;
public readonly scheduler: FirebaseZoneScheduler;
constructor(
@Inject(FirebaseOptionsToken) options:FirebaseOptions,
@Optional() @Inject(FirebaseNameOrConfigToken) nameOrConfig:string|FirebaseAppConfig|null|undefined,
@Optional() @Inject(RealtimeDatabaseURL) databaseURL:string|null,
@Inject(PLATFORM_ID) platformId: Object,
zone: NgZone
) {
this.scheduler = new FirebaseZoneScheduler(zone, platformId);
this.database = zone.runOutsideAngular(() => {
const app = _firebaseAppFactory(options, zone, nameOrConfig);
return app.database(databaseURL || undefined);
});
}
list<T>(pathOrRef: PathReference, queryFn?: QueryFn): AngularFireList<T> {
const ref = getRef(this.database, pathOrRef);
let query: DatabaseQuery = ref;
if(queryFn) {
query = queryFn(ref);
}
return createListReference<T>(query, this);
}
object<T>(pathOrRef: PathReference): AngularFireObject<T> {
const ref = getRef(this.database, pathOrRef);
return createObjectReference<T>(ref, this);
}
createPushId() {
return this.database.ref().push().key;
}
}
export {
PathReference,
DatabaseSnapshot,
ChildEvent,
ListenEvent,
QueryFn,
AngularFireList,
AngularFireObject,
AngularFireAction,
Action,
SnapshotAction
} from './interfaces';
export { RealtimeDatabaseURL };