firebase-tools
Version:
Command-Line Interface for Firebase
59 lines (58 loc) • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.get_documents = void 0;
const zod_1 = require("zod");
const tool_1 = require("../../tool");
const util_1 = require("../../util");
const firestore_1 = require("../../../gcp/firestore");
const converter_1 = require("./converter");
const types_1 = require("../../../emulator/types");
exports.get_documents = (0, tool_1.tool)("firestore", {
name: "get_documents",
description: "Use this to retrieve 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_1.mcpError)("Must supply at least one document path.");
let emulatorUrl;
if (use_emulator) {
emulatorUrl = await host.getEmulatorUrl(types_1.Emulators.FIRESTORE);
}
const { documents, missing } = await (0, firestore_1.getDocuments)(projectId, paths, database, emulatorUrl);
if (missing.length > 0 && documents && documents.length === 0) {
return (0, util_1.mcpError)(`None of the specified documents were found in project '${projectId}'`);
}
const docs = documents.map(converter_1.firestoreDocumentToJson);
if (documents.length === 1 && missing.length === 0) {
return (0, util_1.toContent)(docs[0]);
}
const docsContent = (0, util_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;
});