convex
Version:
Client for the Convex Cloud
103 lines (102 loc) • 4.45 kB
JavaScript
;
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(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var convexImport_exports = {};
__export(convexImport_exports, {
convexImport: () => convexImport
});
module.exports = __toCommonJS(convexImport_exports);
var import_commander = require("commander");
var import_chalk = __toESM(require("chalk"), 1);
var import_config = require("./lib/config");
var import_utils = require("./lib/utils");
var import_axios = __toESM(require("axios"), 1);
var import__ = require("../index.js");
var import_api = require("./lib/api");
var import_context = require("./lib/context");
const convexImport = new import_commander.Command("import").description("Import a file into a Convex table (EXPERIMENTAL)").addOption(
new import_commander.Option(
"--format <format>",
"Input file format. CSV files must have a header, and each rows' entries are interpreted either as a (floating point) number or a string. JSON files must have a JSON object per line."
).choices(["csv", "json"]).default("csv")
).option(
"--prod",
"Import data into this project's production deployment. Defaults to your dev deployment without this flag."
).addOption(new import_commander.Option("--url <url>").hideHelp()).addOption(new import_commander.Option("--admin-key <adminKey>").hideHelp()).argument("<tableName>", "Destination table name (must be empty)").argument("<path>", "Path to the input file").action(async (tableName, path, options) => {
const ctx = import_context.oneoffContext;
const format = options.format;
if (format !== "csv" && format !== "json") {
throw new Error(`Invalid format; ${format}`);
}
const { projectConfig } = await (0, import_config.readProjectConfig)(ctx);
const deploymentType = options.prod ? "prod" : "dev";
let deploymentUrl, adminKey;
if (!options.url || !options.adminKey) {
let url;
({ url, adminKey } = await (0, import_api.getUrlAndAdminKey)(
ctx,
projectConfig.project,
projectConfig.team,
deploymentType
));
deploymentUrl = url;
}
adminKey = options.adminKey ?? adminKey;
deploymentUrl = options.url ?? deploymentUrl;
await (0, import_utils.ensureHasConvexDependency)(ctx, "import");
if (!ctx.fs.exists(path)) {
console.error(import_chalk.default.gray(`Error: Path ${path} does not exist.`));
return await ctx.fatalError(1, "fs");
}
const data = ctx.fs.readUtf8File(path);
console.log(
import_chalk.default.yellow(
`The \`npx convex import\` command is experimental and hasn't been fully polished yet.
Please report any issues on the Convex Community Discord (https://convex.dev/community).
`
)
);
console.log(
import_chalk.default.gray(`Importing ${path} (${(0, import_utils.formatSize)(data.length)})...`)
);
const urlName = encodeURIComponent(tableName);
const urlFormat = encodeURIComponent(format);
const client = import_axios.default.create();
let resp;
try {
const url = `${deploymentUrl}/api/${import__.version}/import?table_name=${urlName}&format=${urlFormat}`;
resp = await client.post(url, data, {
headers: {
Authorization: `Convex ${adminKey}`,
"Content-Type": "text/plain"
}
});
} catch (e) {
return await (0, import_utils.fatalServerErr)(ctx, e);
}
console.log(
import_chalk.default.green(`Wrote ${resp.data.numWritten} rows to ${tableName}.`)
);
});
//# sourceMappingURL=convexImport.js.map