Skip to content

Commit 7c10258

Browse files
committed
feat: documentID 기준으로 오름차순 정렬하여 페이지네이션 구현
1 parent 6db4fa5 commit 7c10258

4 files changed

Lines changed: 42 additions & 7 deletions

File tree

Firebase/firestore.index.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
{
1212
"fieldPath": "dueDate",
1313
"order": "ASCENDING"
14+
},
15+
{
16+
"fieldPath": "__name__",
17+
"order": "ASCENDING"
1418
}
1519
]
1620
},

Firebase/functions/src/notification/cleanup.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,20 @@ export const cleanupSoftDeletedNotifications = onSchedule({
8181
},
8282
async () => {
8383
try {
84+
let lastDocument:
85+
FirebaseFirestore.QueryDocumentSnapshot<FirebaseFirestore.DocumentData> | undefined;
86+
8487
while (true) {
85-
const snapshot = await admin.firestore()
88+
let query = admin.firestore()
8689
.collectionGroup("notifications")
8790
.where("isDeleted", "==", true)
91+
.orderBy(admin.firestore.FieldPath.documentId())
8892
.limit(CLEANUP_BATCH_SIZE)
89-
.get();
93+
if (lastDocument) {
94+
query = query.startAfter(lastDocument);
95+
}
96+
97+
const snapshot = await query.get();
9098

9199
if (snapshot.empty) { return; }
92100

@@ -97,6 +105,7 @@ export const cleanupSoftDeletedNotifications = onSchedule({
97105
await batch.commit();
98106

99107
if (snapshot.size < CLEANUP_BATCH_SIZE) { return; }
108+
lastDocument = snapshot.docs[snapshot.docs.length - 1];
100109
}
101110
} catch (error) {
102111
logger.error(
@@ -105,6 +114,7 @@ export const cleanupSoftDeletedNotifications = onSchedule({
105114
{
106115
collectionGroup: "notifications",
107116
filter: "isDeleted == true",
117+
orderBy: "documentId",
108118
cleanupBatchSize: CLEANUP_BATCH_SIZE
109119
}
110120
);
@@ -127,6 +137,7 @@ export const cleanupUnusedTodoNotificationRecords = onSchedule({
127137
.where("isCompleted", "==", true)
128138
.where("dueDate", "<", admin.firestore.Timestamp.now())
129139
.orderBy("dueDate")
140+
.orderBy(admin.firestore.FieldPath.documentId())
130141
.limit(QUERY_BATCH_SIZE);
131142

132143
if (lastDocument) {
@@ -142,7 +153,7 @@ export const cleanupUnusedTodoNotificationRecords = onSchedule({
142153
{
143154
collectionGroup: "todoLists",
144155
filter: "isCompleted == true && dueDate < now",
145-
orderBy: "dueDate",
156+
orderBy: ["dueDate", "documentId"],
146157
queryBatchSize: QUERY_BATCH_SIZE
147158
}
148159
);

Firebase/functions/src/todo/cleanup.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@ export const cleanupSoftDeletedTodos = onSchedule({
1515
},
1616
async () => {
1717
try {
18+
let lastDocument:
19+
FirebaseFirestore.QueryDocumentSnapshot<FirebaseFirestore.DocumentData> | undefined;
20+
1821
while (true) {
19-
const snapshot = await admin.firestore()
22+
let query = admin.firestore()
2023
.collectionGroup("todoLists")
2124
.where("isDeleted", "==", true)
25+
.orderBy(admin.firestore.FieldPath.documentId())
2226
.limit(QUERY_BATCH_SIZE)
23-
.get();
27+
if (lastDocument) {
28+
query = query.startAfter(lastDocument);
29+
}
30+
31+
const snapshot = await query.get();
2432
if (snapshot.empty) { return; }
2533

2634
const batch = admin.firestore().batch();
@@ -30,6 +38,7 @@ export const cleanupSoftDeletedTodos = onSchedule({
3038
await batch.commit();
3139

3240
if (snapshot.size < QUERY_BATCH_SIZE) { return; }
41+
lastDocument = snapshot.docs[snapshot.docs.length - 1];
3342
}
3443
} catch (error) {
3544
logger.error(
@@ -38,6 +47,7 @@ export const cleanupSoftDeletedTodos = onSchedule({
3847
{
3948
collectionGroup: "todoLists",
4049
filter: "isDeleted == true",
50+
orderBy: "documentId",
4151
queryBatchSize: QUERY_BATCH_SIZE
4252
}
4353
);

Firebase/functions/src/webPage/cleanup.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ export const cleanupSoftDeletedWebPages = onSchedule({
1414
},
1515
async () => {
1616
try {
17+
let lastDocument:
18+
FirebaseFirestore.QueryDocumentSnapshot<FirebaseFirestore.DocumentData> | undefined;
19+
1720
while (true) {
18-
const snapshot = await admin.firestore()
21+
let query = admin.firestore()
1922
.collectionGroup("webPages")
2023
.where("isDeleted", "==", true)
24+
.orderBy(admin.firestore.FieldPath.documentId())
2125
.limit(CLEANUP_BATCH_SIZE)
22-
.get();
26+
if (lastDocument) {
27+
query = query.startAfter(lastDocument);
28+
}
29+
30+
const snapshot = await query.get();
2331

2432
if (snapshot.empty) { return; }
2533

@@ -30,11 +38,13 @@ export const cleanupSoftDeletedWebPages = onSchedule({
3038
await batch.commit();
3139

3240
if (snapshot.size < CLEANUP_BATCH_SIZE) { return; }
41+
lastDocument = snapshot.docs[snapshot.docs.length - 1];
3342
}
3443
} catch (error) {
3544
logger.error("soft delete WebPage cleanup 실패", toError(error), {
3645
collectionGroup: "webPages",
3746
filter: "isDeleted == true",
47+
orderBy: "documentId",
3848
cleanupBatchSize: CLEANUP_BATCH_SIZE
3949
});
4050
}

0 commit comments

Comments
 (0)