7.0.1 • Published 11 months ago

akita-ng-fire v7.0.1

Weekly downloads
600
License
MIT
Repository
github
Last release
11 months ago

akita-ng-fire build status

Looking for maintainers/contributors

If you are interested maintaining this library or want to contribute to it, please contact us creating an issue or write an email to: fguezengar@cascade8.com

Akita Angular Firebase

Simplify connection between Akita and Firebase inside an Angular project

Connect Firebase and Akita :

  • Firestore Collection
  • Firestore Document
  • Firestore Collection Group
  • Akita Array with Subcollections
  • Authentication
  • Real Time Database (beta)

Schematics :

  • ng generate collection-service
  • ng generate collection-guard

Compatibility

akita-ng-fireAngularFirebaseAngularFire
7129^7.0
69-128^6.1.5

Installation

Create an Angular project:

ng new project-name
cd project-name

Add @angular/fire:

ng add @angular/fire

Setup your environment with AngularFirestoreModule.

You can use the akita-cli to instantiate an akita store.

Setup your environment

Getting Started

In your component you can now start listening on Firebase :

@Component({
  selector: 'app-root',
  template: `
    <ul>
      <li *ngFor="let movie of movies$ | async">{{ movie.title }}</li>
    </ul>
  `,
})
export class AppComponent implements OnInit {
  public movies$: Observable<Movie[]>;

  constructor(private service: MovieService, private query: MovieQuery) {}

  ngOnInit() {
    // Subscribe to the collection
    this.service.syncCollection().subscribe();
    // Get the list from the store
    this.movies$ = this.query.selectAll();
  }
}

The MovieService should looks like that :

@Injectable({ providedIn: 'root' })
@CollectionConfig({ path: 'movies' })
export class MovieService extends CollectionService<MovieState> {
  constructor(store: MovieStore) {
    super(store);
  }
}

⚠️: If you use Akita's router store, don't forget to import RouterModule.forRoot()

Collection

Documentation for Collection can be found here :

Authentication

Documentation to manage authentication can be found here :

Cookbook 📚

Examples of what you can do with akita-ng-fire

Real time database

How to use the real time database service.

Document

You can subscribe to a specific document :

In Component :

ngOnInit() {
  this.router.params.pipe(
    switchMap(params => this.service.syncDoc({ id: params.id })),
    takeUntil(this.destroyed$)
  );
}

Or with the Guard :

@Injectable({ providedIn: 'root' })
export class MovieGuard extends CollectionGuard<Movie> {
  constructor(service: MovieService) {
    super(service);
  }

  // Override the default `sync` method
  protected sync(next: ActivatedRouteSnapshot) {
    return this.service.syncDoc({ id: next.params.id });
  }
}

Akita array with subcollection

import {
  CollectionService,
  CollectionConfig,
  Query,
  syncQuery,
} from 'akita-ng-fire';

// A query that fetch all the articles with 5 comments
const articleQuery: Query<Article> = {
  path: 'articles',
  comments: (article: Article) => ({
    path: `articles/${article.id}/comments`,
    queryConstraints: [limit(5)]
  })
};

@Injectable({ providedIn: 'root' })
@CollectionConfig({ path: 'articles' })
export class MoviesService extends CollectionService<MoviesState> {
  // syncQuery needs to be bind to the service and takes a Query as second argument
  syncQuery = syncQuery.bind(this, articleQuery);
  constructor(store: MoviesStore) {
    super(store);
  }
}

Here we use bind() to link the syncQuery to the service. This design helps you to only import what you need.

To take advantage of types, add "strictBindCallApply": true inside your tsconfig.json file.

Now in Component:

ngOnInit() {
  this.service.syncQuery()
    .pipe(takeUntil(this.destroyed$))
    .subscribe();
}

Or in the Guard :

@Injectable({ providedIn: 'root' })
export class MovieGuard extends CollectionGuard<Movie> {
  // Note: Here service has to be protected to access syncQuery
  constructor(protected service: MovieService) {
    super(service);
  }

  // Override the default `sync` method
  protected sync(next: ActivatedRouteSnapshot) {
    return this.service.syncQuery();
  }
}

Credits

Many thanks to :

  • Netanel Basal for building, maintaining, and sharing his knowledge about Akita
  • Loïc Marie for all his feedbacks and contribution.
  • Eduard (ex37) for all his feedbacks and contribution.
  • Ariel Gueta for his great article about Akita and Firebase.
7.0.1

11 months ago

7.0.1-beta.1

2 years ago

7.0.0

2 years ago

7.0.0-beta.2

3 years ago

7.0.0-beta.3

3 years ago

7.0.0-beta.1

3 years ago

6.0.5

3 years ago

6.0.4

3 years ago

6.0.1

3 years ago

6.0.3

3 years ago

6.0.2

3 years ago

5.0.2

3 years ago

6.0.0

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.3.3

3 years ago

4.3.2

3 years ago

4.3.1

3 years ago

4.3.0

3 years ago

4.2.0

3 years ago

4.1.1

3 years ago

4.1.0

3 years ago

3.1.8

3 years ago

4.0.1

3 years ago

4.1.0-beta.1

3 years ago

4.0.0

3 years ago

3.1.7

4 years ago

3.1.6

4 years ago

3.1.5

4 years ago

3.1.4

4 years ago

3.1.3

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2-test.1

4 years ago

2.0.2-test

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

2.0.0-rc.5

4 years ago

2.0.0-rc.4-test

4 years ago

2.0.0-rc.5-test

4 years ago

2.0.0-rc.4

4 years ago

2.0.0-rc.3

4 years ago

2.0.0-rc.2

4 years ago

2.0.0-rc.2-test

4 years ago

2.0.0-rc.1

4 years ago

2.0.0-alpha.29

4 years ago

2.0.0-alpha.28

4 years ago

2.0.0-alpha.27

4 years ago

2.0.0-alpha.26

4 years ago

2.0.0-alpha.25

4 years ago

2.0.0-alpha.24

4 years ago

2.0.0-alpha.23

4 years ago

2.0.0-alpha.22

4 years ago

2.0.0-alpha.21

4 years ago

2.0.0-alpha.20

4 years ago

2.0.0-alpha.19

4 years ago

2.0.0-alpha.18

4 years ago

2.0.0-alpha.17

4 years ago

2.0.0-alpha.16

4 years ago

2.0.0-alpha.15

4 years ago

2.0.0-alpha.14

4 years ago

2.0.0-alpha.13

4 years ago

2.0.0-alpha.12

4 years ago

2.0.0-alpha.11

4 years ago

2.0.0-alpha.10

4 years ago

2.0.0-alpha.9

4 years ago

2.0.0-alpha.8

4 years ago

2.0.0-alpha.7

4 years ago

2.0.0-alpha.6

4 years ago

2.0.0-alpha.5

4 years ago

2.0.0-alpha.4

5 years ago

2.0.0-alpha.3

5 years ago

2.0.0-alpha.2

5 years ago

2.0.0-alpha.1

5 years ago

1.5.13

5 years ago

1.5.12

5 years ago

1.5.11

5 years ago

1.5.10

5 years ago

1.5.9

5 years ago

1.5.8

5 years ago

1.5.7

5 years ago

1.5.6

5 years ago

1.5.5

5 years ago

1.5.4

5 years ago

1.5.3

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago