|
| 1 | +import { Command } from "../command"; |
| 2 | +import * as fsi from "../firestore/api"; |
| 3 | +import { logger } from "../logger"; |
| 4 | +import { requirePermissions } from "../requirePermissions"; |
| 5 | +import { Emulators } from "../emulator/types"; |
| 6 | +import { warnEmulatorNotSupported } from "../emulator/commandUtils"; |
| 7 | +import { FirestoreOptions } from "../firestore/options"; |
| 8 | +import { confirm } from "../prompt"; |
| 9 | +import * as utils from "../utils"; |
| 10 | +import * as clc from "colorette"; |
| 11 | +import { logBullet, logLabeledError, logSuccess } from "../utils"; |
| 12 | +import { FirebaseError } from "../error"; |
| 13 | + |
| 14 | +function confirmationMessage( |
| 15 | + options: FirestoreOptions, |
| 16 | + databaseId: string, |
| 17 | + collectionIds: string[], |
| 18 | +): string { |
| 19 | + const root = `projects/${options.project}/databases/${databaseId}/documents`; |
| 20 | + return ( |
| 21 | + "You are about to delete all documents in the following collection groups: " + |
| 22 | + clc.cyan(collectionIds.map((item) => `"${item}"`).join(", ")) + |
| 23 | + " in " + |
| 24 | + clc.cyan(`"${root}"`) + |
| 25 | + ". Are you sure?" |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +export const command = new Command("firestore:bulkdelete") |
| 30 | + .description("managed bulk delete service to delete data from one or more collection groups") |
| 31 | + .option( |
| 32 | + "--database <databaseName>", |
| 33 | + 'Database ID for database to delete from. "(default)" if none is provided.', |
| 34 | + ) |
| 35 | + .option( |
| 36 | + "--collection-ids <collectionIds>", |
| 37 | + "A comma-separated list of collection group IDs to delete. Deletes all documents in the specified collection groups.", |
| 38 | + ) |
| 39 | + .before(requirePermissions, ["datastore.databases.bulkDeleteDocuments"]) |
| 40 | + .before(warnEmulatorNotSupported, Emulators.FIRESTORE) |
| 41 | + .action(async (options: FirestoreOptions) => { |
| 42 | + if (!options.collectionIds) { |
| 43 | + throw new FirebaseError( |
| 44 | + "Missing required flag --collection-ids=[comma separated list of collection groups]", |
| 45 | + ); |
| 46 | + } |
| 47 | + let collectionIds: string[] = []; |
| 48 | + try { |
| 49 | + collectionIds = (options.collectionIds as string) |
| 50 | + .split(",") |
| 51 | + .filter((id: string) => id.trim() !== ""); |
| 52 | + } catch (e) { |
| 53 | + throw new FirebaseError( |
| 54 | + "The value for --collection-ids must a list of comma separated collection group names", |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + if (collectionIds.length === 0) { |
| 59 | + throw new FirebaseError("Must specify at least one collection ID in --collection-ids."); |
| 60 | + } |
| 61 | + |
| 62 | + const databaseId = options.database || "(default)"; |
| 63 | + |
| 64 | + const api = new fsi.FirestoreApi(); |
| 65 | + |
| 66 | + const confirmed = await confirm({ |
| 67 | + message: confirmationMessage(options, databaseId, collectionIds), |
| 68 | + default: false, |
| 69 | + force: options.force, |
| 70 | + nonInteractive: options.nonInteractive, |
| 71 | + }); |
| 72 | + if (!confirmed) { |
| 73 | + return utils.reject("Command aborted.", { exit: 1 }); |
| 74 | + } |
| 75 | + |
| 76 | + const op = await api.bulkDeleteDocuments(options.project, databaseId, collectionIds); |
| 77 | + |
| 78 | + if (options.json) { |
| 79 | + logger.info(JSON.stringify(op, undefined, 2)); |
| 80 | + } else { |
| 81 | + if (op.name) { |
| 82 | + logSuccess(`Successfully started bulk delete operation.`); |
| 83 | + logBullet(`Operation name: ` + clc.cyan(op.name)); |
| 84 | + // TODO: Update this message to 'firebase firestore:operations:describe' command once it's implemented. |
| 85 | + logBullet( |
| 86 | + "You can monitor the operation's progress using the " + |
| 87 | + clc.cyan(`gcloud firestore operations describe`) + |
| 88 | + ` command.`, |
| 89 | + ); |
| 90 | + } else { |
| 91 | + logLabeledError(`Bulk Delete:`, `Failed to start a bulk delete operation.`); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return op; |
| 96 | + }); |
0 commit comments