【发布时间】:2018-07-13 08:26:08
【问题描述】:
我在 Firestore 上有如下所示的删除操作。它工作正常。但是你能告诉我如何使用transaction 来做到这一点吗?此时如果有任何故障,它会部分删除。
async delete(project: DtoProject): Promise<void> {
await this.fireStore.doc<Project>(`projects/${project.id}/`).delete();
const budgetGroupsQuerySnapshot = await this.authenticationProvider.firestoreDb.collection(`projects/${project.id}/budgetGroups`).get();//budgetGroups
budgetGroupsQuerySnapshot.forEach(async (doc: any) => {
await this.fireStore.doc<BudgetGroup>(`projects/${project.id}/budgetGroups/${doc.id}`).delete();
});
const budgetsQuerySnapshot = await this.authenticationProvider.firestoreDb.collection(`projects/${project.id}/budgets`).get();//budgets
budgetsQuerySnapshot.forEach(async (doc: any) => {
await this.fireStore.doc<Budget>(`projects/${project.id}/budgets/${doc.id}`).delete();
});
const categoriesQuerySnapshot = await this.authenticationProvider.firestoreDb.collection(`projects/${project.id}/categories`).get();//categories
categoriesQuerySnapshot.forEach(async (doc: any) => {
await this.fireStore.doc<Category>(`projects/${project.id}/categories/${doc.id}`).delete();
});
const transactionsQuerySnapshot = await this.authenticationProvider.firestoreDb.collection(`projects/${project.id}/transactions`).get();//transactions
transactionsQuerySnapshot.forEach(async (doc: any) => {
const notesQuerySnapshot = await this.authenticationProvider.firestoreDb.collection(`projects/${project.id}/transactions/${doc.id}/notes`).get();//notes
notesQuerySnapshot.forEach(async (n: any) => {
await this.fireStore.doc<Note>(`projects/${project.id}/transactions/${doc.id}/notes/${n.id}`).delete();
});
const photosQuerySnapshot = await this.authenticationProvider.firestoreDb.collection(`projects/${project.id}/transactions/${doc.id}/photos`).get();//photos
photosQuerySnapshot.forEach(async (p: any) => {
await this.fireStore.doc<Photo>(`projects/${project.id}/transactions/${doc.id}/photos/${p.id}`).delete();
});
await this.fireStore.doc<Transaction>(`projects/${project.id}/transactions/${doc.id}`).delete();
});
}
【问题讨论】:
标签: angular typescript firebase ionic3 google-cloud-firestore