Skip to content

Commit 881be94

Browse files
committed
feat(firestore): add firestore collection and doc observables
1 parent b0d40c0 commit 881be94

17 files changed

+351
-22
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ npm-debug.log
99
angularfire2-*.tgz
1010
*.ngfactory.ts
1111
*.ngsummary.json
12+
firestore-module.tgz
13+
.DS_Store
14+
yarn-error.log

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angularfire2",
3-
"version": "4.0.0-rc.2",
3+
"version": "4.0.0-rc.3",
44
"description": "The official library of Firebase and Angular.",
55
"private": true,
66
"scripts": {
@@ -33,6 +33,7 @@
3333
"@angular/platform-browser": "^4.0.0",
3434
"@angular/platform-browser-dynamic": "^4.0.0",
3535
"firebase": "^4.0.0",
36+
"firestore": "file:firestore-module.tgz",
3637
"rxjs": "^5.0.1",
3738
"zone.js": "^0.8.0"
3839
},
@@ -72,7 +73,7 @@
7273
"systemjs": "^0.19.16",
7374
"systemjs-builder": "^0.15.7",
7475
"traceur": "0.0.96",
75-
"typescript": "^2.3.4"
76+
"typescript": "^2.4.2"
7677
},
7778
"typings": "index.d.ts"
7879
}

src/core/firebase.app.module.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { InjectionToken, } from '@angular/core';
22
import { FirebaseAppConfig } from './';
33
import * as firebase from 'firebase/app';
4+
// TODO(davideast): Replace once Firestore is merged in firebase.d.ts
5+
import { Firestore } from 'firestore';
46

57
export const FirebaseAppConfigToken = new InjectionToken<FirebaseAppConfig>('FirebaseAppConfigToken');
68

@@ -12,21 +14,22 @@ export class FirebaseApp implements firebase.app.App {
1214
messaging: () => firebase.messaging.Messaging;
1315
storage: () => firebase.storage.Storage;
1416
delete: () => firebase.Promise<any>;
17+
firestore: () => Firestore;
1518
}
1619

1720
export function _firebaseAppFactory(config: FirebaseAppConfig, appName?: string): FirebaseApp {
1821
try {
1922
if (appName) {
20-
return firebase.initializeApp(config, appName);
23+
return firebase.initializeApp(config, appName) as FirebaseApp;
2124
} else {
22-
return firebase.initializeApp(config);
25+
return firebase.initializeApp(config) as FirebaseApp;
2326
}
2427
}
2528
catch (e) {
2629
if (e.code === "app/duplicate-app") {
27-
return firebase.app(e.name);
30+
return firebase.app(e.name) as FirebaseApp;
2831
}
2932

30-
return firebase.app(null!);
33+
return firebase.app(null!) as FirebaseApp;
3134
}
3235
}

src/firestore/firestore.module.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { NgModule, NgZone } from '@angular/core';
2+
import { FirebaseApp, AngularFireModule } from 'angularfire2';
3+
import { AngularFirestore } from './firestore';
4+
import * as firestore from 'firestore';
5+
6+
export function _getAngularFirestore(app: FirebaseApp) {
7+
return new AngularFirestore(app);
8+
}
9+
10+
export const AngularFirestoreProvider = {
11+
provide: AngularFirestore,
12+
useFactory: _getAngularFirestore,
13+
deps: [ FirebaseApp ]
14+
};
15+
16+
export const FIRESTORE_PROVIDERS = [
17+
AngularFirestoreProvider,
18+
];
19+
20+
@NgModule({
21+
imports: [ AngularFireModule ],
22+
providers: [ FIRESTORE_PROVIDERS ]
23+
})
24+
export class AngularFirestoreModule { }

src/firestore/firestore.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { FirebaseApp, FirebaseAppConfig, AngularFireModule} from 'angularfire2';
2+
import { AngularFirestore, AngularFirestoreModule } from 'angularfire2/firestore';
3+
import * as firebase from 'firebase/app';
4+
5+
import { TestBed, inject } from '@angular/core/testing';
6+
import { COMMON_CONFIG } from './test-config';
7+
8+
describe('AngularFirestore', () => {
9+
let app: firebase.app.App;
10+
let db: AngularFirestore;
11+
12+
beforeEach(() => {
13+
TestBed.configureTestingModule({
14+
imports: [
15+
AngularFireModule.initializeApp(COMMON_CONFIG),
16+
AngularFirestoreModule
17+
]
18+
});
19+
inject([FirebaseApp, AngularFirestore], (_app: firebase.app.App, _db: AngularFirestore) => {
20+
app = _app;
21+
db = _db;
22+
})();
23+
});
24+
25+
afterEach(done => {
26+
app.delete().then(done, done.fail);
27+
// ref.off();
28+
// ref.remove(done);
29+
});
30+
31+
});

src/firestore/firestore.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import * as firebase from 'firebase/app';
2+
import * as firestore from 'firestore';
3+
import 'firestore';
4+
import { Firestore, CollectionReference, Query } from 'firestore';
5+
import { Observable } from 'rxjs/Observable';
6+
import { Observer } from 'rxjs/Observer';
7+
import { Operator } from 'rxjs/Operator';
8+
import { Subscriber } from 'rxjs/Subscriber';
9+
import { Subscription } from 'rxjs/Subscription';
10+
11+
import { Injectable } from '@angular/core';
12+
import { FirebaseApp } from 'angularfire2';
13+
14+
@Injectable()
15+
export class AngularFirestore {
16+
firestore: Firestore;
17+
18+
constructor(public app: FirebaseApp) {
19+
this.firestore = app.firestore();
20+
}
21+
22+
collection(path: string, queryFn: (ref: CollectionReference) => Query = ref => ref): Observable<firestore.QuerySnapshot> {
23+
const ref = this.firestore.collection(path);
24+
const query = queryFn(ref);
25+
return new Observable(subscriber => {
26+
const unsubscribe = query.onSnapshot(subscriber);
27+
return new Subscription(unsubscribe);
28+
});
29+
}
30+
31+
doc(path: string): Observable<firestore.DocumentSnapshot> {
32+
const ref = this.firestore.doc(path);
33+
return new Observable(subscriber => {
34+
const unsubscribe = ref.onSnapshot(subscriber);
35+
return new Subscription(unsubscribe);
36+
});
37+
}
38+
39+
}
40+
41+
export class FirestoreDocumentReferenceObservable<T> extends Observable<T> {
42+
constructor(public originalRef: firestore.DocumentReference, subscribe?: <R>(subscriber: Subscriber<R>) => Subscription) {
43+
super(subscribe);
44+
}
45+
46+
lift<T, R>(operator: Operator<T, R>): Observable<R> {
47+
const observable = new FirestoreDocumentReferenceObservable<R>(this.originalRef);
48+
observable.source = this;
49+
observable.operator = operator;
50+
observable.originalRef = this.originalRef;
51+
return observable;
52+
}
53+
}
54+
55+
export class FirestoreCollectionReferenceObservable<T> extends Observable<T> {
56+
constructor(public originalRef: firestore.CollectionReference, public query: firestore.Query, subscribe?: <R>(subscriber: Subscriber<R>) => Subscription) {
57+
super(subscribe);
58+
}
59+
60+
lift<T, R>(operator: Operator<T, R>): Observable<R> {
61+
const observable = new FirestoreCollectionReferenceObservable<R>(this.originalRef, this.query);
62+
observable.source = this;
63+
observable.operator = operator;
64+
observable.query = this.query;
65+
observable.originalRef = this.originalRef;
66+
return observable;
67+
}
68+
}
69+
70+
/**
71+
* collection(path: string, queryFn: (ref: CollectionReference) => Query = ref => ref): Observable<firestore.QuerySnapshot> {
72+
const ref = this.firestore.collection(path);
73+
const query = queryFn(ref);
74+
return new FirestoreCollectionReferenceObservable(ref, query, subscriber => {
75+
const unsubscribe = query.onSnapshot(subscriber.next, subscriber.error, subscriber.complete);
76+
return new Subscription(unsubscribe);
77+
});
78+
}
79+
80+
doc(path: string): Observable<firestore.DocumentSnapshot> {
81+
const ref = this.firestore.doc(path);
82+
return new FirestoreDocumentReferenceObservable(ref, subscriber => {
83+
const unsubscribe = ref.onSnapshot(subscriber.next, subscriber.error, subscriber.complete);
84+
return new Subscription(unsubscribe);
85+
});
86+
}
87+
*/

src/firestore/index.spec.ts

Whitespace-only changes.

src/firestore/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './public_api';

src/firestore/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "angularfire2/auth",
3+
"version": "ANGULARFIRE2_VERSION",
4+
"description": "The auth module",
5+
"main": "../bundles/auth.umd.js",
6+
"module": "index.js",
7+
"es2015": "./es2015/index.js",
8+
"keywords": [
9+
"angular",
10+
"firebase",
11+
"rxjs"
12+
],
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/angular/angularfire2.git"
16+
},
17+
"author": "angular,firebase",
18+
"license": "MIT",
19+
"peerDependencies": {
20+
"angularfire2": "ANGULARFIRE2_VERSION",
21+
"@angular/common": "ANGULAR_VERSION",
22+
"@angular/core": "ANGULAR_VERSION",
23+
"@angular/platform-browser": "ANGULAR_VERSION",
24+
"@angular/platform-browser-dynamic": "ANGULAR_VERSION",
25+
"firebase": "FIREBASE_VERSION",
26+
"firestore": "FIRESTORE_VERSION",
27+
"rxjs": "RXJS_VERSION",
28+
"zone.js": "ZONEJS_VERSION"
29+
},
30+
"typings": "index.d.ts"
31+
}

src/firestore/public_api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './firestore';
2+
export * from './firestore.module';

0 commit comments

Comments
 (0)