firebase-tools
Version:
Command-Line Interface for Firebase
59 lines (58 loc) • 2.55 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.get_documents = 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 converter_js_1 = require("./converter.js");
const types_js_1 = require("../../../emulator/types.js");
exports.get_documents = (0, tool_js_1.tool)({
name: "get_documents",
description: "Retrieves one or more 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."),
paths: zod_1.z
.array(zod_1.z.string())
.describe("One or more document paths (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: "Get Firestore documents",
readOnlyHint: true,
},
_meta: {
requiresAuth: true,
requiresProject: true,
},
}, async ({ paths, database, use_emulator }, { projectId, host }) => {
if (!paths || !paths.length)
return (0, util_js_1.mcpError)("Must supply at least one document path.");
let emulatorUrl;
if (use_emulator) {
emulatorUrl = await host.getEmulatorUrl(types_js_1.Emulators.FIRESTORE);
}
const { documents, missing } = await (0, firestore_js_1.getDocuments)(projectId, paths, 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 docs = documents.map(converter_js_1.firestoreDocumentToJson);
if (documents.length === 1 && missing.length === 0) {
return (0, util_js_1.toContent)(docs[0]);
}
const docsContent = (0, util_js_1.toContent)(docs);
if (missing.length) {
docsContent.content = [
{ type: "text", text: "Retrieved documents:\n\n" },
...docsContent.content,
{
type: "text",
text: `The following documents do not exist: ${missing.join(", ")}`,
},
];
}
return docsContent;
});
;