[英]Get merged observable from id with rxjs Angular Firestore
我有一个包含人和宠物的 Firestore 数据库,宠物有所有者 ID。
// 集合 / DocumentID: {data}
persons / 'person1': { name: 'Romea' }
persons / 'person2': { name: 'Julieto' }
pets / 'pet1': { name: 'Forky', ownerID: 'person1' }
pets / 'pet2': { name: 'Rasky', ownerID: 'person1' }
pets / 'pet3': { name: 'Tursy', ownerID: 'person2' }
因此,当 Julieto 拥有 Tursky 时,Romea 拥有 Forky 和 Rasky。
我正在使用@angular/fire ,并且我想使用最新版本的 rxjs (6.5.x)来获取用户可观察到的每个宠物的列表,请参见以下结构:
[
0: {
name: 'Romea',
pets: [ 0: { name: 'Forky', ownerID: 'person1' }, 1: { name: 'Rasky', ownerID: 'person1' } ]
},
1: {
name: 'Julieto',
pets: [ 0: { name: 'Tursky', ownerID: 'person2' } ]
}
]
毕竟,我想调用person.pets ,就像这样:
this.personService.getPersonsWithPets().subscribe(persons => {
console.log(persons[0].pets)
// result: [{ name: 'Forky', ownerID: 'person1' }, { name: 'Rasky', ownerID: 'person1' }]
persons.forEach(person => person.pets.forEach(pet => console.log(pet.name)))
// result: 'Forky', 'Rasky', 'Tursky'
})
解决方案
根据@bryan60的解决方案,这就是我所做的(rxjs 加上一个在构造函数中接收人和宠物的 class ):
服务:
private personsCollection: AngularFirestoreCollection<Person> = this.db.collection<Person>('persons')
private _getPersonsWithoutPets(): Observable<Person[]> {
return this.personsCollection.valueChanges()
}
getPersons(): Observable<(Person)[]> {
return this._getPersonsWithoutPets().pipe(
switchMap(persons => {
return combineLatest(persons.map(person => this.getPersonPets(person.id).pipe(
map(pets => new Person(person, pets))
)))
})
)
}
getPersonPets(personId: string): Observable<Pet[]> {
return this.db.collection<Pet>('pets', ref => ref.where('personId', '==', personId)).valueChanges()
}
楷模:
export class Person {
id?: string
name: string
public constructor(person: Person = null, private pets: Pet[] = null) {
Object.assign(this, person)
this.pets = pets
}
model?() {
return {
id: this.id,
name: this.name
}
}
}
export class Pet {
id?: string
name: string
constructor(pet: Pet = null) {
Object.assign(this, pet)
}
model?() {
return {
id: this.id,
name: this.name
}
}
}
零件:
personsWithPets: Person[] = []
constructor(private personService: PersonService) {
this.getPersons()
}
getPersons() {
this.personService.getPersons().subscribe(p => this.personsWithPets = p)
}
人员有多个引用(宠物、汽车)的情况的解决方案:
getPersonsWithReferences(): Observable<Person[]> {
return this.getPersons().pipe(switchMap(persons =>
combineLatest(persons.map(person =>
combineLatest(this.getPersonPets(person.id), this.getPersonCars(person.id))
.pipe(map(([pets, cars]) => new Environment(person, pets, cars)))))))
}
这是一个 switchMap 用例。
假设你有函数getPersons()
和getPersonsPets(ownerID)
,它们按照他们的名字暗示,这样做:
getPersonsWithPets() {
return this.getPersons().pipe(
switchMap(persons => {
return combineLatest(persons.map(person => this.getPersonsPets(person.id).pipe(
map(pets => Object.assign(person, {pets}))
)))
})
)
}
首先获取persons,switchMap 到combineLatest of persons 映射到他们宠物的stream 并将宠物分配给该人,并返回带有宠物的人的列表。
有更多的任务:
getPersonsWithPets() {
return this.getPersons().pipe(
switchMap(persons => {
return combineLatest(persons.map(person =>
combineLatest(
this.getPersonsPets(person.id),
this.getPersonsCars(person.id),
this.getPersonsHouses(person.id)
).pipe(
map(([pets, cars, houses]) => Object.assign(person, {pets, cars, houses}))
)
))
})
)
}
您可以使用forkJoin
获取您的两个 collections ,您可以在其中获得一个数据数组,包括persons
和pets
。 然后你可以修改你的数据,并以你喜欢的方式组合这些数据,即所有者x
的所有宠物都应该添加到person x
的 object 中。 如果您正在寻找从 db 观看实时更新,我建议您改用combineLatest
。
这是一个示例,但您需要使用 angularfire 将其应用于您的用例并相应地使用文档参考:)
allData = [];
// added an id property here for this sample, you will use the doc ref
persons = [{ id: 1, name: "Romea" }, { id: 2, name: "Julieto" }];
pets = [
{ name: "Forky", ownerID: 1 },
{ name: "Rasky", ownerID: 1 },
{ name: "Tursy", ownerID: 2 }
];
ngOnInit() {
// in your usecase, combine your two queries
forkJoin(of(this.persons).pipe( /** here map accordingly **/),
of(this.pets).pipe(/** here map accordingly **/)).pipe(
// DON'T USE ANY, type your data!
map((data: [any, any]) => {
const persons = data[0];
const pets = data[1];
// here combine the data
return persons.map(person => {
const personPets = pets.filter(p => p.ownerID === person.id);
return { ...person, pets: [...personPets] };
});
})
)
.subscribe(d => this.allData = d);
}
here map accordingly
仅意味着在您的代码中,您需要根据您进行查询的方式来 map 值,假设使用snapshotChanges
。
上述代码的STACKBLITZ
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.