Skip to content

Commit

Permalink
feat(database): Add AngularFireDatabaseModule
Browse files Browse the repository at this point in the history
  • Loading branch information
davideast committed Mar 30, 2017
1 parent 72dab0d commit b388627
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 72 deletions.
8 changes: 0 additions & 8 deletions src/angularfire2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import {
import { ReflectiveInjector, Provider } from '@angular/core';
import {
AngularFire,
FirebaseObjectObservable,
FIREBASE_PROVIDERS,
FirebaseApp,
AngularFireDatabase,
FirebaseAppConfig,
AngularFireModule,
AngularFireAuth
Expand Down Expand Up @@ -61,12 +59,6 @@ describe('angularfire', () => {
}));
});

describe('.database', () => {
it('should be an instance of Database', inject([AngularFire], (af:AngularFire) => {
expect(af.database instanceof AngularFireDatabase).toBe(true);
}));
});

describe('FirebaseApp', () => {
it('should provide a FirebaseApp for the FirebaseApp binding', () => {
expect(typeof app.delete).toBe('function');
Expand Down
26 changes: 4 additions & 22 deletions src/angularfire2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@ import { FirebaseAppConfigToken, FirebaseApp, _firebaseAppFactory } from './app/
import { FirebaseListFactoryOpts, FirebaseObjectFactoryOpts, FirebaseAppConfig } from './interfaces';
import { FirebaseAppName, WindowLocation, FirebaseAuthConfig } from './tokens';
import { Injectable, OpaqueToken, NgModule } from '@angular/core';
import { FirebaseListObservable, FirebaseObjectObservable, FirebaseListFactory, FirebaseObjectFactory, AngularFireDatabase } from './database/index';
import { FirebaseSdkAuthBackend, AuthBackend, AngularFireAuth, AuthConfiguration } from './auth/index';

@Injectable()
export class AngularFire {
constructor(
public auth: AngularFireAuth,
public database: AngularFireDatabase) {}
public auth: AngularFireAuth) {}
}

export function _getAngularFire(db: AngularFireDatabase, auth: AngularFireAuth) {
return new AngularFire(auth, db);
}

export function _getAngularFireDatabase(app: FirebaseApp) {
return new AngularFireDatabase(app);
export function _getAngularFire(auth: AngularFireAuth) {
return new AngularFire(auth);
}

export function _getAngularFireAuth(backend: AuthBackend, location: any, config: any) {
Expand Down Expand Up @@ -51,12 +45,6 @@ export const FirebaseAppProvider = {
deps: [ FirebaseAppConfigToken, FirebaseAppName ]
};

export const AngularFireDatabaseProvider = {
provide: AngularFireDatabase,
useFactory: _getAngularFireDatabase,
deps: [ FirebaseApp ]
};

export const FirebaseAuthBackendProvider = {
provide: FirebaseSdkAuthBackend,
useFactory: _getFirebaseAuthBackend,
Expand All @@ -72,28 +60,22 @@ export const AngularFireAuthProvider = {
export const AngularFireProvider = {
provide: AngularFire,
useFactory: _getAngularFire,
deps: [ AngularFireDatabase, AngularFireAuth ]
deps: [ AngularFireAuth ]
};

export const FIREBASE_PROVIDERS:any[] = [
FirebaseAppProvider,
FirebaseAuthBackendProvider,
AuthBackendProvder,
WindowLocationProvider,
AngularFireDatabaseProvider,
AngularFireAuthProvider,
AngularFireProvider
];

export {
FirebaseApp,
FirebaseAppConfigToken,
AngularFireDatabase,
AngularFireAuth,
FirebaseListObservable,
FirebaseObjectObservable,
FirebaseListFactory,
FirebaseObjectFactory,
WindowLocation
}

Expand Down
26 changes: 26 additions & 0 deletions src/database/database.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NgModule } from '@angular/core';
import * as firebase from 'firebase/app';
import 'firebase/database';
import { FirebaseApp } from '../app/index';
import { AngularFireModule } from '../angularfire2';
import { AngularFireDatabase } from './index';

export function _getAngularFireDatabase(app: FirebaseApp) {
return new AngularFireDatabase(app);
}

export const AngularFireDatabaseProvider = {
provide: AngularFireDatabase,
useFactory: _getAngularFireDatabase,
deps: [ FirebaseApp ]
};

export const DATABASE_PROVIDERS = [
AngularFireDatabaseProvider,
];

@NgModule({
imports: [ AngularFireModule ],
providers: [ DATABASE_PROVIDERS ]
})
export class AngularFireDatabaseModule { }
87 changes: 61 additions & 26 deletions src/database/firebase_list_factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as firebase from 'firebase/app';
import { FirebaseListFactory, FirebaseListObservable, FirebaseObjectFactory, onChildAdded, onChildChanged, onChildRemoved, onChildUpdated } from './index';
import { FIREBASE_PROVIDERS, FirebaseApp, FirebaseAppConfig, AngularFire, AngularFireModule} from '../angularfire2';
import { FirebaseListFactory, FirebaseListObservable, FirebaseObjectFactory, onChildAdded, onChildChanged, onChildRemoved, onChildUpdated, AngularFireDatabase, AngularFireDatabaseModule } from './index';
import { FIREBASE_PROVIDERS, FirebaseApp, FirebaseAppConfig, AngularFireModule} from '../angularfire2';
import { TestBed, inject } from '@angular/core/testing';
import * as utils from '../utils';
import { Query, AFUnwrappedDataSnapshot } from '../interfaces';
Expand Down Expand Up @@ -32,16 +32,20 @@ function queryTest(observable: Observable<any>, subject: Subject<any>, done: any
subject.next('20');
}

describe('FirebaseListFactory', () => {

var app: firebase.app.App;
describe('AngularFireDatabase', () => {
let app: FirebaseApp;
let db: AngularFireDatabase;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG, '[DEFAULT]')]
});
inject([FirebaseApp, AngularFire], (firebaseApp: firebase.app.App, _af: AngularFire) => {
app = firebaseApp;
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG, '[DEFAULT]'),
AngularFireDatabaseModule
]
});
inject([FirebaseApp, AngularFireDatabase], (app_: FirebaseApp, _db: AngularFireDatabase) => {
app = app_;
db = _db;
})();
});

Expand All @@ -52,6 +56,37 @@ describe('FirebaseListFactory', () => {
describe('<constructor>', () => {

it('should accept a Firebase db url in the constructor', () => {
debugger;
expect(db instanceof AngularFireDatabase).toBe(true);
});

});

});

describe('FirebaseListFactory', () => {

let app: FirebaseApp;
let db: AngularFireDatabase;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG, '[DEFAULT]'),
AngularFireDatabaseModule
]
});
inject([FirebaseApp, AngularFireDatabase], (app_: FirebaseApp, _db: AngularFireDatabase) => {
app = app_;
db = _db;
})();
});


describe('<constructor>', () => {

it('should accept a Firebase db url in the constructor', () => {
debugger;
const list = FirebaseListFactory(`questions`);
expect(list instanceof FirebaseListObservable).toBe(true);
});
Expand Down Expand Up @@ -338,15 +373,15 @@ describe('FirebaseListFactory', () => {

describe('methods', () => {

var toKey;
var val1: any;
var val2: any;
var val3: any;
var questions: FirebaseListObservable<any>;
var questionsSnapshotted: FirebaseListObservable<any>;
var ref: any;
var refSnapshotted: any;
var subscription: Subscription;
let toKey;
let val1: any;
let val2: any;
let val3: any;
let questions: FirebaseListObservable<any>;
let questionsSnapshotted: FirebaseListObservable<any>;
let ref: any;
let refSnapshotted: any;
let subscription: Subscription;

beforeEach((done: any) => {
toKey = (val) => val.key;
Expand Down Expand Up @@ -418,7 +453,7 @@ describe('FirebaseListFactory', () => {
done();
}, done.fail);

var child1 = ref.push({ push1: true }, () => {
let child1 = ref.push({ push1: true }, () => {
ref.push({ push2: true }, () => {
child1.setPriority('ZZZZ')
});
Expand Down Expand Up @@ -502,7 +537,7 @@ describe('FirebaseListFactory', () => {
})
.then(() => {

var query = FirebaseListFactory(questions.$ref.ref, {
let query = FirebaseListFactory(questions.$ref.ref, {
query: {
orderByChild: "extra",
equalTo: null
Expand Down Expand Up @@ -530,7 +565,7 @@ describe('FirebaseListFactory', () => {
})
.then(() => {

var query = FirebaseListFactory(questions.$ref.ref, {
let query = FirebaseListFactory(questions.$ref.ref, {
query: {
orderByChild: "extra",
startAt: null,
Expand All @@ -552,7 +587,7 @@ describe('FirebaseListFactory', () => {

it('should call off on all events when disposed', (done: any) => {
const questionRef = firebase.database().ref().child('questions');
var firebaseSpy = spyOn(questionRef, 'off').and.callThrough();
let firebaseSpy = spyOn(questionRef, 'off').and.callThrough();
subscription = FirebaseListFactory(questionRef).subscribe(_ => {
expect(firebaseSpy).not.toHaveBeenCalled();
subscription.unsubscribe();
Expand All @@ -570,7 +605,7 @@ describe('FirebaseListFactory', () => {


it('should not mutate the input array', () => {
var inputArr = [val1];
let inputArr = [val1];
expect(onChildAdded(inputArr, val2, toKey, 'key1')).not.toEqual(inputArr);
});
});
Expand All @@ -596,7 +631,7 @@ describe('FirebaseListFactory', () => {
});

it('should not mutate the input array', () => {
var inputArr = [val1, val2];
let inputArr = [val1, val2];
expect(onChildChanged(inputArr, val1, toKey, 'key2')).not.toEqual(inputArr);
});

Expand All @@ -620,8 +655,8 @@ describe('FirebaseListFactory', () => {


describe('utils.unwrapMapFn', () => {
var val = { unwrapped: true };
var snapshot = {
let val = { unwrapped: true };
let snapshot = {
ref: { key: 'key' },
val: () => val
};
Expand Down
19 changes: 12 additions & 7 deletions src/database/firebase_list_observable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FirebaseListObservable } from './index';
import { FirebaseListObservable, AngularFireDatabase, AngularFireDatabaseModule } from './index';
import { Observer } from 'rxjs/Observer';
import { map } from 'rxjs/operator/map';
import * as firebase from 'firebase/app';
Expand All @@ -8,16 +8,21 @@ import { TestBed, inject } from '@angular/core/testing';
import { COMMON_CONFIG, ANON_AUTH_CONFIG } from '../test-config';

describe('FirebaseListObservable', () => {
let O:FirebaseListObservable<any>;
let ref:firebase.database.Reference;
let O: FirebaseListObservable<any>;
let ref: firebase.database.Reference;
let app: firebase.app.App;
let db: AngularFireDatabase;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG)]
});
inject([FirebaseApp, AngularFire], (firebaseApp: firebase.app.App, _af: AngularFire) => {
app = firebaseApp;
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG),
AngularFireDatabaseModule
]
});
inject([FirebaseApp, AngularFireDatabase], (_app: firebase.app.App, _db: AngularFireDatabase) => {
app = _app;
db = _db;
ref = firebase.database().ref();
O = new FirebaseListObservable(ref, (observer:Observer<any>) => {
});
Expand Down
13 changes: 9 additions & 4 deletions src/database/firebase_object_factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as firebase from 'firebase/app';
import { Subscription } from 'rxjs';
import { FirebaseObjectFactory, FirebaseObjectObservable } from './index';
import { FirebaseObjectFactory, FirebaseObjectObservable, AngularFireDatabaseModule, AngularFireDatabase } from './index';
import { TestBed, inject } from '@angular/core/testing';
import { FIREBASE_PROVIDERS, FirebaseApp, FirebaseAppConfig, AngularFire, AngularFireModule } from '../angularfire2';
import { COMMON_CONFIG, ANON_AUTH_CONFIG } from '../test-config';
Expand All @@ -12,13 +12,18 @@ describe('FirebaseObjectFactory', () => {
let subscription: Subscription;
let nextSpy: jasmine.Spy;
let app: firebase.app.App;
let db: AngularFireDatabase;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG)]
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG, '[DEFAULT]'),
AngularFireDatabaseModule
]
});
inject([FirebaseApp, AngularFire], (firebaseApp: firebase.app.App, _af: AngularFire) => {
app = firebaseApp;
inject([FirebaseApp, AngularFireDatabase], (app_: FirebaseApp, _db: AngularFireDatabase) => {
app = app_;
db = _db;
})();
});

Expand Down
13 changes: 9 additions & 4 deletions src/database/firebase_object_observable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestBed, inject } from '@angular/core/testing';
import { FIREBASE_PROVIDERS, FirebaseApp, FirebaseAppConfig, AngularFire, AngularFireModule } from '../angularfire2';
import { COMMON_CONFIG, ANON_AUTH_CONFIG } from '../test-config';
import { FirebaseObjectObservable } from './index';
import { FirebaseObjectObservable, AngularFireDatabaseModule, AngularFireDatabase } from './index';
import { Observer } from 'rxjs/Observer';
import { map } from 'rxjs/operator/map';
import * as firebase from 'firebase/app';
Expand All @@ -11,13 +11,18 @@ describe('FirebaseObjectObservable', () => {
let O: FirebaseObjectObservable<any>;
let ref: firebase.database.Reference;
let app: firebase.app.App;
let db: AngularFireDatabase;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG)]
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG, ANON_AUTH_CONFIG),
AngularFireDatabaseModule
]
});
inject([FirebaseApp, AngularFire], (firebaseApp: firebase.app.App, _af: AngularFire) => {
app = firebaseApp;
inject([FirebaseApp, AngularFireDatabase], (_app: firebase.app.App, _db: AngularFireDatabase) => {
app = _app;
db = _db;
ref = firebase.database().ref();
O = new FirebaseObjectObservable((observer: Observer<any>) => {
}, ref);
Expand Down
3 changes: 2 additions & 1 deletion src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from './firebase_list_factory';
export * from './firebase_list_observable';
export * from './firebase_object_factory';
export * from './firebase_object_observable';
export * from './query_observable';
export * from './query_observable';
export * from './database.module';

0 comments on commit b388627

Please sign in to comment.