Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.

Commit bdc482f

Browse files
feat(database): support Angular 4.x and AngularFire2 4.x
BREAKING CHANGE: database has moved to the /database directory. Follow the upgrade guide at: https://github.com/angular/angularfire2/blob/master/docs/version-4-upgrade.md Fixes: #11, #21
1 parent f30963f commit bdc482f

File tree

119 files changed

+3351
-2628
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+3351
-2628
lines changed

README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
[![Example Gif](https://raw.githubusercontent.com/adriancarriger/angularfire2-offline/master/images/example.gif)](https://angularfire2-offline.firebaseapp.com/write-list)
1818

19+
## Legacy Versions
20+
21+
- Upgrading from `2.x` to `4.x` for AngularFire2? Try the [upgrade tutorial](https://github.com/adriancarriger/angularfire2-offline/blob/master/docs/version-4-upgrade.md)
22+
- To support **`AngularFire2 2.x`** use the [@two branch of this repo](https://github.com/adriancarriger/angularfire2-offline/tree/two) for [install instructions](https://github.com/adriancarriger/angularfire2-offline/tree/two#install) and tutorials ([Angular](https://github.com/adriancarriger/angularfire2-offline/tree/two/examples/angular-cli#angular-cli-offline-tutorial-)/[Ionic](https://github.com/adriancarriger/angularfire2-offline/tree/two/examples/ionic#ionic-offline-tutorial-)).
23+
- This branch (master) is a wrapper for the latest version of AngularFire2 (4.x)
24+
1925
## Install
2026

2127
```bash
@@ -29,10 +35,10 @@ import { NgModule } from '@angular/core';
2935
import { BrowserModule } from '@angular/platform-browser';
3036
import { AngularFireModule } from 'angularfire2';
3137
import { AngularFireOfflineModule } from 'angularfire2-offline';
38+
import { AngularFireDatabaseModule } from 'angularfire2/database';
3239

3340
import { AppComponent } from './app.component';
3441

35-
// Must export the config
3642
export const firebaseConfig = {
3743
apiKey: '<your-key>',
3844
authDomain: '<your-project-authdomain>',
@@ -43,6 +49,7 @@ export const firebaseConfig = {
4349
@NgModule({
4450
declarations: [AppComponent],
4551
imports: [
52+
AngularFireDatabaseModule,
4653
AngularFireModule.initializeApp(firebaseConfig),
4754
AngularFireOfflineModule,
4855
BrowserModule
@@ -65,7 +72,7 @@ import { Component } from '@angular/core';
6572
import {
6673
AfoListObservable,
6774
AfoObjectObservable,
68-
AngularFireOffline } from 'angularfire2-offline';
75+
AngularFireOfflineDatabase } from 'angularfire2-offline/database';
6976

7077
@Component({
7178
selector: 'project-name-app',
@@ -81,9 +88,9 @@ import {
8188
export class MyApp {
8289
info: AfoObjectObservable<any>;
8390
items: AfoListObservable<any[]>;
84-
constructor(afo: AngularFireOffline) {
85-
this.info = afo.database.object('/info');
86-
this.items = afo.database.list('/items');
91+
constructor(afoDatabase: AngularFireOfflineDatabase) {
92+
this.info = afoDatabase.object('/info');
93+
this.items = afoDatabase.list('/items');
8794
}
8895
}
8996
```
@@ -98,10 +105,10 @@ If writes are made offline followed by a page refresh, the writes will be sent w
98105

99106
## How it works
100107

101-
- While online, Firebase data is stored locally (as data changes the local store is updated)
102-
- While offline, local data is served if available, and writes are stored locally
103-
- On reconnect, app updates with new Firebase data, and writes are sent to Firebase
104-
- Even while online, local data is used first when available which results in a faster load
108+
- While online, Firebase data is stored locally (as data changes the local store is updated)
109+
- While offline, local data is served if available, and writes are stored locally
110+
- On reconnect, app updates with new Firebase data, and writes are sent to Firebase
111+
- Even while online, local data is used first when available which results in a faster load
105112

106113
## Contributing to AngularFire2 Offline
107114

config/spec-bundle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ testing.TestBed.initTestEnvironment(
4343
* any file that ends with spec.ts and get its path. By passing in true
4444
* we say do this recursively
4545
*/
46-
var testContext = require.context('../tests', true, /\.spec\.ts/);
46+
var testContext = require.context('../src', true, /\.spec\.ts/);
4747

4848
/*
4949
* get all the files, for each file, call the context function

docs/version-4-upgrade.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Upgrading to AngularFire2 4.0
2+
3+
This tutorial will help if you are upgrading an existing AngularFire2 Offline app that is already using AngularFire2 version 2.x.
4+
5+
## Not upgrading?
6+
7+
Checkout a full install tutorial for AngularFire2 Offline:
8+
9+
* [Angular Cli Tutorial 📗](https://github.com/adriancarriger/angularfire2-offline/tree/master/examples/angular-cli#angular-cli-offline-tutorial-)
10+
* [Ionic Tutorial 📘](https://github.com/adriancarriger/angularfire2-offline/tree/master/examples/ionic#ionic-offline-tutorial-)
11+
12+
## 1. Upgrade @NgModule
13+
14+
Open [`/src/app/app.module.ts`](https://github.com/adriancarriger/angularfire2-offline/blob/master/examples/angular-cli/src/app/app.module.ts) and add `AngularFireDatabaseModule` to your imports:
15+
16+
```ts
17+
import { NgModule } from '@angular/core';
18+
import { BrowserModule } from '@angular/platform-browser';
19+
import { AngularFireModule } from 'angularfire2';
20+
import { AngularFireOfflineModule } from 'angularfire2-offline';
21+
import { AngularFireDatabaseModule } from 'angularfire2/database';
22+
23+
import { AppComponent } from './app.component';
24+
25+
@NgModule({
26+
declarations: [
27+
AppComponent,
28+
...OtherComponents
29+
],
30+
imports: [
31+
AngularFireDatabaseModule,
32+
AngularFireModule.initializeApp(environment.firebase),
33+
AngularFireOfflineModule,
34+
BrowserModule,
35+
...OtherModules
36+
],
37+
providers: [],
38+
bootstrap: [AppComponent]
39+
})
40+
export class AppModule { }
41+
```
42+
43+
## 2. Upgrade components
44+
45+
### Change your import
46+
47+
Add `/database` to your AngularFire2 Offline import
48+
49+
```ts
50+
import { AfoObjectObservable, AngularFireOfflineDatabase } from 'angularfire2-offline/database';
51+
```
52+
53+
Replace calls to `AngularFireOffline.database` with AngularFireOfflineDatabase
54+
55+
#### Before
56+
57+
```ts
58+
constructor(afo: AngularFireOffline) {
59+
afo.database.list('foo');
60+
}
61+
```
62+
63+
#### After
64+
65+
```ts
66+
constructor(afoDatabase: AngularFireOfflineDatabase) {
67+
afoDatabase.list('foo');
68+
}
69+
```
70+
71+
#### Full Component
72+
73+
```ts
74+
import { Component } from '@angular/core';
75+
import {
76+
AfoListObservable,
77+
AfoObjectObservable,
78+
AngularFireOfflineDatabase } from 'angularfire2-offline/database';
79+
80+
@Component({
81+
selector: 'project-name-app',
82+
template: `
83+
<h1>{{ (info | async)?.name }}</h1>
84+
<ul>
85+
<li *ngFor="let item of items | async">
86+
{{ item?.name }}
87+
</li>
88+
</ul>
89+
`
90+
})
91+
export class MyApp {
92+
info: AfoObjectObservable<any>;
93+
items: AfoListObservable<any[]>;
94+
constructor(afoDatabase: AngularFireOfflineDatabase) {
95+
this.info = afoDatabase.object('/info');
96+
this.items = afoDatabase.list('/items');
97+
}
98+
}
99+
```
100+
101+
## AngularFire2 upgrade guide
102+
103+
Also, checkout the [AngularFire2 upgrade guide](https://github.com/angular/angularfire2/blob/master/docs/version-4-upgrade.md).

examples/angular-cli/.angular-cli.json

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
33
"project": {
4-
"version": "1.0.0-beta.31",
54
"name": "angular-cli"
65
},
76
"apps": [
@@ -17,7 +16,8 @@
1716
"main": "main.ts",
1817
"polyfills": "polyfills.ts",
1918
"test": "test.ts",
20-
"tsconfig": "tsconfig.json",
19+
"tsconfig": "tsconfig.app.json",
20+
"testTsconfig": "tsconfig.spec.json",
2121
"prefix": "app",
2222
"styles": [
2323
"styles.scss"
@@ -37,12 +37,13 @@
3737
},
3838
"lint": [
3939
{
40-
"files": "src/**/*.ts",
41-
"project": "src/tsconfig.json"
40+
"project": "src/tsconfig.app.json"
4241
},
4342
{
44-
"files": "e2e/**/*.ts",
45-
"project": "e2e/tsconfig.json"
43+
"project": "src/tsconfig.spec.json"
44+
},
45+
{
46+
"project": "e2e/tsconfig.e2e.json"
4647
}
4748
],
4849
"test": {
@@ -52,18 +53,6 @@
5253
},
5354
"defaults": {
5455
"styleExt": "scss",
55-
"prefixInterfaces": false,
56-
"inline": {
57-
"style": false,
58-
"template": false
59-
},
60-
"spec": {
61-
"class": false,
62-
"component": true,
63-
"directive": true,
64-
"module": false,
65-
"pipe": true,
66-
"service": true
67-
}
56+
"component": {}
6857
}
6958
}

examples/angular-cli/.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# compiled output
44
/dist
55
/tmp
6+
/out-tsc
67

78
# dependencies
89
/node_modules
@@ -14,6 +15,7 @@
1415
.c9/
1516
*.launch
1617
.settings/
18+
*.sublime-workspace
1719

1820
# IDE - VSCode
1921
.vscode/*
@@ -25,7 +27,7 @@
2527
# misc
2628
/.sass-cache
2729
/connect.lock
28-
/coverage/*
30+
/coverage
2931
/libpeerconnection.log
3032
npm-debug.log
3133
testem.log
@@ -35,6 +37,6 @@ testem.log
3537
/e2e/*.js
3638
/e2e/*.map
3739

38-
#System Files
40+
# System Files
3941
.DS_Store
4042
Thumbs.db

0 commit comments

Comments
 (0)