/
scheduled-firestore-export.ts
38 lines (33 loc) · 1.1 KB
/
scheduled-firestore-export.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import * as firestore from "@google-cloud/firestore";
import * as functions from "firebase-functions";
import { StringParam } from "firebase-functions/lib/params/types";
export function buildScheduledFirestoreExport(
region: string,
projectIdParams: StringParam,
bucketNameParams: StringParam
): functions.CloudFunction<void> {
const schedule = "0 0 * * *";
const client = new firestore.v1.FirestoreAdminClient();
return functions
.region(region)
.pubsub.schedule(schedule)
.timeZone("Etc/UTC")
.onRun(async (): Promise<void> => {
const databaseName = client.databasePath(
projectIdParams.value(),
"(default)"
);
try {
const responses = await client.exportDocuments({
name: databaseName,
outputUriPrefix: `gs://${bucketNameParams.value()}`,
collectionIds: [], // export all collections
});
const response = responses[0];
console.log(`Operation Name: ${response.name ?? "(none)"}`);
} catch (err) {
console.error(err);
throw new Error("Export operation failed");
}
});
}