UNPKG

convex

Version:

Client for the Convex Cloud

214 lines (213 loc) 7.48 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var data_exports = {}; __export(data_exports, { data: () => data }); module.exports = __toCommonJS(data_exports); var import_extra_typings = require("@commander-js/extra-typings"); var import_chalk = __toESM(require("chalk"), 1); var import_context = require("../bundler/context.js"); var import_values = require("../values/index.js"); var import_api = require("./lib/api.js"); var import_run = require("./lib/run.js"); var import_utils = require("./lib/utils/utils.js"); var import_extra_typings2 = require("@commander-js/extra-typings"); var import_command = require("./lib/command.js"); const data = new import_extra_typings2.Command("data").summary("List tables and print data from your database").description( "Inspect your Convex deployment's database.\n\n List tables: `npx convex data`\n List documents in a table: `npx convex data tableName`\n\nBy default, this inspects your dev deployment." ).argument("[table]", "If specified, list documents in this table.").addOption( new import_extra_typings.Option( "--limit <n>", "List only the `n` the most recently created documents." ).default(100).argParser(import_utils.parsePositiveInteger) ).addOption( new import_extra_typings.Option( "--order <choice>", "Order the documents by their `_creationTime`." ).choices(["asc", "desc"]).default("desc") ).addDeploymentSelectionOptions((0, import_command.actionDescription)("Inspect the database in")).showHelpAfterError().action(async (tableName, options) => { const ctx = import_context.oneoffContext; const deploymentSelection = (0, import_api.deploymentSelectionFromOptions)(options); const { adminKey, url: deploymentUrl, deploymentName } = await (0, import_api.fetchDeploymentCredentialsProvisionProd)(ctx, deploymentSelection); if (tableName !== void 0) { await listDocuments(ctx, deploymentUrl, adminKey, tableName, { ...options, order: options.order }); } else { await listTables(ctx, deploymentUrl, adminKey, deploymentName); } }); async function listTables(ctx, deploymentUrl, adminKey, deploymentName) { const tables = await (0, import_run.runPaginatedQuery)( ctx, deploymentUrl, adminKey, "_system/cli/tables", {} ); if (tables.length === 0) { (0, import_context.logError)( ctx, `There are no tables in the ${deploymentName ? `${import_chalk.default.bold(deploymentName)} deployment's ` : ""}database.` ); return; } const tableNames = tables.map((table) => table.name); tableNames.sort(); (0, import_context.logOutput)(ctx, tableNames.join("\n")); } async function listDocuments(ctx, deploymentUrl, adminKey, tableName, options) { const data2 = await (0, import_run.runPaginatedQuery)( ctx, deploymentUrl, adminKey, "_system/cli/tableData", { table: tableName, order: options.order ?? "desc" }, options.limit + 1 ); if (data2.length === 0) { (0, import_context.logError)(ctx, "There are no documents in this table."); return; } logDocumentsTable( ctx, data2.slice(0, options.limit).map((document) => { const printed = {}; for (const key in document) { printed[key] = stringify(document[key]); } return printed; }) ); if (data2.length > options.limit) { (0, import_context.logWarning)( ctx, import_chalk.default.yellow( `Showing the ${options.limit} ${options.order === "desc" ? "most recently" : "oldest"} created document${options.limit > 1 ? "s" : ""}. Use the --limit option to see more.` ) ); } } function logDocumentsTable(ctx, rows) { const columnsToWidths = {}; for (const row of rows) { for (const column in row) { const value = row[column]; columnsToWidths[column] = Math.max( value.length, columnsToWidths[column] ?? 0 ); } } const unsortedFields = Object.keys(columnsToWidths); unsortedFields.sort(); const fields = Array.from( /* @__PURE__ */ new Set(["_id", "_creationTime", ...unsortedFields]) ); const columnWidths = fields.map((field) => columnsToWidths[field]); const lineLimit = process.stdout.isTTY ? process.stdout.columns : void 0; let didTruncate = false; function limitLine(line, limit) { if (limit === void 0) { return line; } const limitWithBufferForUnicode = limit - 10; if (line.length > limitWithBufferForUnicode) { didTruncate = true; } return line.slice(0, limitWithBufferForUnicode); } (0, import_context.logOutput)( ctx, limitLine( fields.map((field, i) => field.padEnd(columnWidths[i])).join(" | "), lineLimit ) ); (0, import_context.logOutput)( ctx, limitLine( columnWidths.map((width) => "-".repeat(width)).join("-|-"), lineLimit ) ); for (const row of rows) { (0, import_context.logOutput)( ctx, limitLine( fields.map((field, i) => (row[field] ?? "").padEnd(columnWidths[i])).join(" | "), lineLimit ) ); } if (didTruncate) { (0, import_context.logWarning)( ctx, import_chalk.default.yellow( "Lines were truncated to fit the terminal width. Pipe the command to see the full output, such as:\n `npx convex data tableName | less -S`" ) ); } } function stringify(value) { if (value === null) { return "null"; } if (typeof value === "bigint") { return `${value.toString()}n`; } if (typeof value === "number") { return value.toString(); } if (typeof value === "boolean") { return value.toString(); } if (typeof value === "string") { return JSON.stringify(value); } if (value instanceof ArrayBuffer) { const base64Encoded = import_values.Base64.fromByteArray(new Uint8Array(value)); return `Bytes("${base64Encoded}")`; } if (value instanceof Array) { return `[${value.map(stringify).join(", ")}]`; } const pairs = Object.entries(value).map(([k, v]) => `"${k}": ${stringify(v)}`).join(", "); return `{ ${pairs} }`; } //# sourceMappingURL=data.js.map