If you use Angular and AngularFire2, you can use AngularFirestype. This module is intended to replace AngularFirestore and allows you to receive and install data in Firestore directly with user objects.
To do this, you need to perform 3 steps:
1. Install angular-firestype
`npm install angular-firestype
2. Initialize the AngularFirestype module with the mapping object
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AngularFireModule } from 'angularfire2'; import { AngularFireAuthModule } from 'angularfire2/auth'; import { AngularFirestypeModule, ModelType } from 'angular-firestype'; import { environment } from '../environments/environment'; import { User } from './user.ts'; import { Address } from './address.ts'; import { Message } from './message.ts'; const model: {[key: string]: ModelType<any>} = { users: { type: User, arguments: ['username', 'image'], structure: { adress: Address }, subcollections: { messages: Message } } }; @NgModule({ imports: [ AngularFireModule.initializeApp(environment.firebase), AngularFireAuthModule, AngularFirestypeModule.forRoot(model),
3. Inject AngularFirestype Service
import { Component } from '@angular/core'; import { AngularFirestype, Collection, Document } from 'angular-firestype'; import { User } from './user.ts'; @Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrls: ['app.component.css'] }) export class AppComponent { const users: Observable<User[]>; const user: User; constructor(db: AngularFirestype) { const usersCollection: Collection<User> = db.collection<User>('users'); usersCollection.valueChanges().subscribe(users => this.users = users); const userDoc: Document<User> = usersCollection.doc('user1'); userDoc.valueChanges().subscribe(user => this.user = user); userDoc.set(this.user); } }
Basically you can use AngularFirestype as you use Angularfirestore.
For more information, see the Homepage here: https://github.com/bricepepin/angular-firestype .
source share