diff --git a/src/angularfire2.spec.ts b/src/angularfire2.spec.ts index b6bdfd39a..dbce47e89 100644 --- a/src/angularfire2.spec.ts +++ b/src/angularfire2.spec.ts @@ -6,10 +6,8 @@ import { import { ReflectiveInjector, Provider } from '@angular/core'; import { AngularFire, - FirebaseObjectObservable, FIREBASE_PROVIDERS, FirebaseApp, - AngularFireDatabase, FirebaseAppConfig, AngularFireModule, AngularFireAuth @@ -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'); diff --git a/src/angularfire2.ts b/src/angularfire2.ts index 7040f3673..87d5ac85a 100644 --- a/src/angularfire2.ts +++ b/src/angularfire2.ts @@ -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) { @@ -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, @@ -72,7 +60,7 @@ export const AngularFireAuthProvider = { export const AngularFireProvider = { provide: AngularFire, useFactory: _getAngularFire, - deps: [ AngularFireDatabase, AngularFireAuth ] + deps: [ AngularFireAuth ] }; export const FIREBASE_PROVIDERS:any[] = [ @@ -80,7 +68,6 @@ export const FIREBASE_PROVIDERS:any[] = [ FirebaseAuthBackendProvider, AuthBackendProvder, WindowLocationProvider, - AngularFireDatabaseProvider, AngularFireAuthProvider, AngularFireProvider ]; @@ -88,12 +75,7 @@ export const FIREBASE_PROVIDERS:any[] = [ export { FirebaseApp, FirebaseAppConfigToken, - AngularFireDatabase, AngularFireAuth, - FirebaseListObservable, - FirebaseObjectObservable, - FirebaseListFactory, - FirebaseObjectFactory, WindowLocation } diff --git a/src/database/database.module.ts b/src/database/database.module.ts new file mode 100644 index 000000000..45521b4a7 --- /dev/null +++ b/src/database/database.module.ts @@ -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 { } diff --git a/src/database/firebase_list_factory.spec.ts b/src/database/firebase_list_factory.spec.ts index 9b3674dff..ba6e64135 100644 --- a/src/database/firebase_list_factory.spec.ts +++ b/src/database/firebase_list_factory.spec.ts @@ -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'; @@ -32,16 +32,20 @@ function queryTest(observable: Observable, subject: Subject, 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; })(); }); @@ -52,6 +56,37 @@ describe('FirebaseListFactory', () => { describe('', () => { 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('', () => { + + it('should accept a Firebase db url in the constructor', () => { + debugger; const list = FirebaseListFactory(`questions`); expect(list instanceof FirebaseListObservable).toBe(true); }); @@ -338,15 +373,15 @@ describe('FirebaseListFactory', () => { describe('methods', () => { - var toKey; - var val1: any; - var val2: any; - var val3: any; - var questions: FirebaseListObservable; - var questionsSnapshotted: FirebaseListObservable; - var ref: any; - var refSnapshotted: any; - var subscription: Subscription; + let toKey; + let val1: any; + let val2: any; + let val3: any; + let questions: FirebaseListObservable; + let questionsSnapshotted: FirebaseListObservable; + let ref: any; + let refSnapshotted: any; + let subscription: Subscription; beforeEach((done: any) => { toKey = (val) => val.key; @@ -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') }); @@ -502,7 +537,7 @@ describe('FirebaseListFactory', () => { }) .then(() => { - var query = FirebaseListFactory(questions.$ref.ref, { + let query = FirebaseListFactory(questions.$ref.ref, { query: { orderByChild: "extra", equalTo: null @@ -530,7 +565,7 @@ describe('FirebaseListFactory', () => { }) .then(() => { - var query = FirebaseListFactory(questions.$ref.ref, { + let query = FirebaseListFactory(questions.$ref.ref, { query: { orderByChild: "extra", startAt: null, @@ -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(); @@ -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); }); }); @@ -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); }); @@ -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 }; diff --git a/src/database/firebase_list_observable.spec.ts b/src/database/firebase_list_observable.spec.ts index b28d7b483..272f90040 100644 --- a/src/database/firebase_list_observable.spec.ts +++ b/src/database/firebase_list_observable.spec.ts @@ -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'; @@ -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; - let ref:firebase.database.Reference; + let O: FirebaseListObservable; + 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) => { }); diff --git a/src/database/firebase_object_factory.spec.ts b/src/database/firebase_object_factory.spec.ts index 3d289a383..fbc7ac7b1 100644 --- a/src/database/firebase_object_factory.spec.ts +++ b/src/database/firebase_object_factory.spec.ts @@ -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'; @@ -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; })(); }); diff --git a/src/database/firebase_object_observable.spec.ts b/src/database/firebase_object_observable.spec.ts index 36bd7c6db..50a27b79e 100644 --- a/src/database/firebase_object_observable.spec.ts +++ b/src/database/firebase_object_observable.spec.ts @@ -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'; @@ -11,13 +11,18 @@ describe('FirebaseObjectObservable', () => { let O: FirebaseObjectObservable; 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) => { }, ref); diff --git a/src/database/index.ts b/src/database/index.ts index c9ac3ddba..c1e95b11e 100644 --- a/src/database/index.ts +++ b/src/database/index.ts @@ -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'; \ No newline at end of file +export * from './query_observable'; +export * from './database.module';