firebase-tools
Version:
Command-Line Interface for Firebase
51 lines (50 loc) • 2.48 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.delete_document = void 0;
const zod_1 = require("zod");
const tool_js_1 = require("../../tool.js");
const util_js_1 = require("../../util.js");
const firestore_js_1 = require("../../../gcp/firestore.js");
const delete_js_1 = require("../../../firestore/delete.js");
const types_js_1 = require("../../../emulator/types.js");
exports.delete_document = (0, tool_js_1.tool)({
name: "delete_document",
description: "Deletes a Firestore documents from a database in the current project by full document paths. Use this if you know the exact path of a document.",
inputSchema: zod_1.z.object({
database: zod_1.z
.string()
.optional()
.describe("Database id to use. Defaults to `(default)` if unspecified."),
path: zod_1.z
.string()
.describe("A document path (e.g. `collectionName/documentId` or `parentCollection/parentDocument/collectionName/documentId`)"),
use_emulator: zod_1.z.boolean().default(false).describe("Target the Firestore emulator if true."),
}),
annotations: {
title: "Delete Firestore document",
destructiveHint: true,
},
_meta: {
requiresAuth: true,
requiresProject: true,
},
}, async ({ path, database, use_emulator }, { projectId, host }) => {
let emulatorUrl;
if (use_emulator) {
emulatorUrl = await host.getEmulatorUrl(types_js_1.Emulators.FIRESTORE);
}
const { documents, missing } = await (0, firestore_js_1.getDocuments)(projectId, [path], database, emulatorUrl);
if (missing.length > 0 && documents && documents.length === 0) {
return (0, util_js_1.mcpError)(`None of the specified documents were found in project '${projectId}'`);
}
const firestoreDelete = new delete_js_1.FirestoreDelete(projectId, path, {
databaseId: database !== null && database !== void 0 ? database : "(default)",
urlPrefix: emulatorUrl,
});
await firestoreDelete.execute();
const { documents: postDeleteDocuments, missing: postDeleteMissing } = await (0, firestore_js_1.getDocuments)(projectId, [path], emulatorUrl);
if (postDeleteMissing.length > 0 && postDeleteDocuments.length === 0) {
return (0, util_js_1.toContent)(`Successfully removed document located at : ${path}`);
}
return (0, util_js_1.mcpError)(`Failed to remove document located at : ${path}`);
});
;