Skip to content

Commit b03a9b6

Browse files
committed
update delete with recursion
1 parent 9213796 commit b03a9b6

File tree

1 file changed

+121
-111
lines changed

1 file changed

+121
-111
lines changed

functions/src/spaces.ts

Lines changed: 121 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -18,148 +18,158 @@ import { FieldValue, UpdateData } from 'firebase-admin/firestore';
1818

1919
// Firestore events
2020
const onSpaceDelete = onDocumentDeleted('spaces/{spaceId}', async event => {
21-
logger.info(`[Space::onDelete] eventId='${event.id}'`);
22-
logger.info(`[Space::onDelete] params='${JSON.stringify(event.params)}'`);
23-
const { spaceId } = event.params;
24-
await bucket.deleteFiles({
25-
prefix: `spaces/${spaceId}/`,
26-
});
27-
let batch = firestoreService.batch();
28-
let count = 0;
29-
// Assets
30-
const assetsSnapshot = await findAssets(spaceId).get();
31-
logger.info(`[Space::onDelete] Assets size='${assetsSnapshot.docs.length}'`);
32-
for (const item of assetsSnapshot.docs) {
33-
batch.delete(item.ref);
34-
count++;
35-
if (count === BATCH_MAX) {
21+
const { id, params, data } = event;
22+
logger.info(`[Space::onDelete] eventId='${id}'`);
23+
logger.info(`[Space::onDelete] params='${JSON.stringify(params)}'`);
24+
const { spaceId } = params;
25+
26+
if (data) {
27+
// Recursive delete
28+
logger.info(`[Space::onDelete] data='${JSON.stringify(data)}'`);
29+
await firestoreService.recursiveDelete(data.ref);
30+
} else {
31+
// Batch delete
32+
let batch = firestoreService.batch();
33+
let count = 0;
34+
// Assets
35+
const assetsSnapshot = await findAssets(spaceId).get();
36+
logger.info(`[Space::onDelete] Assets size='${assetsSnapshot.docs.length}'`);
37+
for (const item of assetsSnapshot.docs) {
38+
batch.delete(item.ref);
39+
count++;
40+
if (count === BATCH_MAX) {
41+
await batch.commit();
42+
batch = firestoreService.batch();
43+
count = 0;
44+
}
45+
}
46+
if (count > 0) {
3647
await batch.commit();
3748
batch = firestoreService.batch();
3849
count = 0;
3950
}
40-
}
41-
if (count > 0) {
42-
await batch.commit();
43-
batch = firestoreService.batch();
44-
count = 0;
45-
}
46-
// Contents
47-
const contentsSnapshot = await findContents(spaceId).get();
48-
logger.info(`[Space::onDelete] Contents size='${contentsSnapshot.docs.length}'`);
49-
for (const item of contentsSnapshot.docs) {
50-
batch.delete(item.ref);
51-
count++;
52-
if (count === BATCH_MAX) {
51+
// Contents
52+
const contentsSnapshot = await findContents(spaceId).get();
53+
logger.info(`[Space::onDelete] Contents size='${contentsSnapshot.docs.length}'`);
54+
for (const item of contentsSnapshot.docs) {
55+
batch.delete(item.ref);
56+
count++;
57+
if (count === BATCH_MAX) {
58+
await batch.commit();
59+
batch = firestoreService.batch();
60+
count = 0;
61+
}
62+
}
63+
if (count > 0) {
5364
await batch.commit();
5465
batch = firestoreService.batch();
5566
count = 0;
5667
}
57-
}
58-
if (count > 0) {
59-
await batch.commit();
60-
batch = firestoreService.batch();
61-
count = 0;
62-
}
63-
// Plugins
64-
const pluginsSnapshot = await findPlugins(spaceId).get();
65-
logger.info(`[Space::onDelete] plugins size='${pluginsSnapshot.docs.length}'`);
66-
for (const item of pluginsSnapshot.docs) {
67-
batch.delete(item.ref);
68-
count++;
69-
if (count === BATCH_MAX) {
68+
// Plugins
69+
const pluginsSnapshot = await findPlugins(spaceId).get();
70+
logger.info(`[Space::onDelete] plugins size='${pluginsSnapshot.docs.length}'`);
71+
for (const item of pluginsSnapshot.docs) {
72+
batch.delete(item.ref);
73+
count++;
74+
if (count === BATCH_MAX) {
75+
await batch.commit();
76+
batch = firestoreService.batch();
77+
count = 0;
78+
}
79+
}
80+
if (count > 0) {
7081
await batch.commit();
7182
batch = firestoreService.batch();
7283
count = 0;
7384
}
74-
}
75-
if (count > 0) {
76-
await batch.commit();
77-
batch = firestoreService.batch();
78-
count = 0;
79-
}
80-
// Schemas
81-
const schemasSnapshot = await findSchemas(spaceId).get();
82-
logger.info(`[Space::onDelete] Schemas size='${schemasSnapshot.docs.length}'`);
83-
for (const item of schemasSnapshot.docs) {
84-
batch.delete(item.ref);
85-
count++;
86-
if (count === BATCH_MAX) {
85+
// Schemas
86+
const schemasSnapshot = await findSchemas(spaceId).get();
87+
logger.info(`[Space::onDelete] Schemas size='${schemasSnapshot.docs.length}'`);
88+
for (const item of schemasSnapshot.docs) {
89+
batch.delete(item.ref);
90+
count++;
91+
if (count === BATCH_MAX) {
92+
await batch.commit();
93+
batch = firestoreService.batch();
94+
count = 0;
95+
}
96+
}
97+
if (count > 0) {
8798
await batch.commit();
8899
batch = firestoreService.batch();
89100
count = 0;
90101
}
91-
}
92-
if (count > 0) {
93-
await batch.commit();
94-
batch = firestoreService.batch();
95-
count = 0;
96-
}
97-
// Tasks
98-
const tasksSnapshot = await findTasks(spaceId).get();
99-
logger.info(`[Space::onDelete] Tasks size='${tasksSnapshot.docs.length}'`);
100-
for (const item of tasksSnapshot.docs) {
101-
batch.delete(item.ref);
102-
count++;
103-
if (count === BATCH_MAX) {
102+
// Tasks
103+
const tasksSnapshot = await findTasks(spaceId).get();
104+
logger.info(`[Space::onDelete] Tasks size='${tasksSnapshot.docs.length}'`);
105+
for (const item of tasksSnapshot.docs) {
106+
batch.delete(item.ref);
107+
count++;
108+
if (count === BATCH_MAX) {
109+
await batch.commit();
110+
batch = firestoreService.batch();
111+
count = 0;
112+
}
113+
}
114+
if (count > 0) {
104115
await batch.commit();
105116
batch = firestoreService.batch();
106117
count = 0;
107118
}
108-
}
109-
if (count > 0) {
110-
await batch.commit();
111-
batch = firestoreService.batch();
112-
count = 0;
113-
}
114-
// Tokens
115-
const tokensSnapshot = await findTokens(spaceId).get();
116-
logger.info(`[Space::onDelete] Tokens size='${tokensSnapshot.docs.length}'`);
117-
for (const item of tokensSnapshot.docs) {
118-
batch.delete(item.ref);
119-
count++;
120-
if (count === BATCH_MAX) {
119+
// Tokens
120+
const tokensSnapshot = await findTokens(spaceId).get();
121+
logger.info(`[Space::onDelete] Tokens size='${tokensSnapshot.docs.length}'`);
122+
for (const item of tokensSnapshot.docs) {
123+
batch.delete(item.ref);
124+
count++;
125+
if (count === BATCH_MAX) {
126+
await batch.commit();
127+
batch = firestoreService.batch();
128+
count = 0;
129+
}
130+
}
131+
if (count > 0) {
121132
await batch.commit();
122133
batch = firestoreService.batch();
123134
count = 0;
124135
}
125-
}
126-
if (count > 0) {
127-
await batch.commit();
128-
batch = firestoreService.batch();
129-
count = 0;
130-
}
131-
// Translations
132-
const translationsSnapshot = await findTranslations(spaceId).get();
133-
logger.info(`[Space::onDelete] Translations size='${translationsSnapshot.docs.length}'`);
134-
for (const item of translationsSnapshot.docs) {
135-
batch.delete(item.ref);
136-
count++;
137-
if (count === BATCH_MAX) {
136+
// Translations
137+
const translationsSnapshot = await findTranslations(spaceId).get();
138+
logger.info(`[Space::onDelete] Translations size='${translationsSnapshot.docs.length}'`);
139+
for (const item of translationsSnapshot.docs) {
140+
batch.delete(item.ref);
141+
count++;
142+
if (count === BATCH_MAX) {
143+
await batch.commit();
144+
batch = firestoreService.batch();
145+
count = 0;
146+
}
147+
}
148+
if (count > 0) {
138149
await batch.commit();
139150
batch = firestoreService.batch();
140151
count = 0;
141152
}
142-
}
143-
if (count > 0) {
144-
await batch.commit();
145-
batch = firestoreService.batch();
146-
count = 0;
147-
}
148-
// Translations History
149-
const translationsHistorySnapshot = await findTranslationsHistory(spaceId).get();
150-
logger.info(`[Space::onDelete] Translations History size='${translationsHistorySnapshot.docs.length}'`);
151-
for (const item of translationsHistorySnapshot.docs) {
152-
batch.delete(item.ref);
153-
count++;
154-
if (count === BATCH_MAX) {
153+
// Translations History
154+
const translationsHistorySnapshot = await findTranslationsHistory(spaceId).get();
155+
logger.info(`[Space::onDelete] Translations History size='${translationsHistorySnapshot.docs.length}'`);
156+
for (const item of translationsHistorySnapshot.docs) {
157+
batch.delete(item.ref);
158+
count++;
159+
if (count === BATCH_MAX) {
160+
await batch.commit();
161+
batch = firestoreService.batch();
162+
count = 0;
163+
}
164+
}
165+
if (count > 0) {
155166
await batch.commit();
156-
batch = firestoreService.batch();
157-
count = 0;
158167
}
159168
}
160-
if (count > 0) {
161-
await batch.commit();
162-
}
169+
// delete files
170+
await bucket.deleteFiles({
171+
prefix: `spaces/${spaceId}/`,
172+
});
163173
return;
164174
});
165175

0 commit comments

Comments
 (0)